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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With