Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does dictproxy change its id?

Tags:

python

I'm trying to understand why the dictproxy __dict__ always changes its id each time it is being accessed.

>>> class A(object):
        pass
>>> A.__dict__ is A.__dict__
False

From what I understand, dictproxy is a special read only dict, but that doesn't explain this behaviour.

like image 954
slallum Avatar asked Feb 04 '26 14:02

slallum


1 Answers

A.__dict__ is not a static object, Every time we access A.__dict__ it internally call a method and that method return what we get. So Every time we get different object.

Now whats that method

When we access A.__dict__, this call is evaluated as meta_type_of_A.__dict__[__dict__], Which means it will call to __getattribute__ of meta class of A.

>>> class mymeta(type):
    def __init__(cls, name, bases,dict):
        print "in my meta"
    def __getattribute__(*args):
        print "in get attribute of meta class"
        return "A test string"


>>> class A(object):
    __metaclass__ = mymeta
    pass

in my meta
>>> d = A.__dict__
in get attribute of meta class
>>> print d
A test string
like image 65
AlokThakur Avatar answered Feb 07 '26 03:02

AlokThakur