Python Forum
what is the __dict__ object? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: what is the __dict__ object? (/thread-9224.html)



what is the __dict__ object? - aaron1989041 - Mar-28-2018

CODE TO EXECUTE:
class Foo:
pass
class Foo1:
pass
class Foo2:
pass
print(id(Foo),id(Foo.__dict__),type(Foo.__dict__))
print(id(Foo1),id(Foo1.__dict__),type(Foo1.__dict__))
print(id(Foo2),id(Foo2.__dict__),type(Foo2.__dict__))

RESULT:
15879688 16886808 <class 'mappingproxy'>
15880632 16886808 <class 'mappingproxy'>
15881576 16886808 <class 'mappingproxy'>

all three class.__dict__ point to the same address,so what's this special __dict__ object ?what's in it? is it like a special dict whose keys are all the class names?


RE: what is the __dict__ object? - Larz60+ - Mar-28-2018

since __dict__ returns an objects key, value pairs,
the three classes are actually different, but do point to to the same object, pass.

Therefore, their id's are different, because the class definition for each is stored in different memory locations.
__dict__ returns only one item, the pointer to pass.
and the type of all three are the same
The output is as it should be.


id() function is strange - aaron1989041 - Mar-28-2018

>>> class A:
... pass
...
>>> class B:
... pass
...
>>> print(A.__dict__ is B.__dict__,A.__dict__ == B.__dict__,id(A.__dict__),id(B.__dict__))
False False 139925069297848 139925069297848

why id(A.__dict__) = id(B.__dict__),but A.__dict__ is not B.__dict__?
i think if id() function gives the same address for two objects,they must be the same object.
what is wrong here?


RE: what is the __dict__ object? - snippsat - Mar-28-2018

(Mar-28-2018, 09:29 AM)aaron1989041 Wrote: i think if id() function gives the same address for two objects,they must be the same object.
Use code tags.
There not same object,but something strange happens when making into a tuple object.
>>> class A:
...     pass
...
... class B:
...     pass

>>> id(A.__dict__)
62085776

>>> id(B.__dict__)
62373680
So when use , between object,Python is making a tuple.
This is when the somewhat strange execution happens.
>>> id(A.__dict__), id(B.__dict__)
(136355152, 136355152)
It look like when using special method in tuple creation it call same object.
>>> a = 'foo'
>>> b = 'bar'

>>> id(a)
128252192
>>> id(b)
61876128

>>> id(a), id(b)
(128252192, 61876128)
Using .__add__ the same happens.
>>> id(a.__add__), id(b.__add__)
(62304752, 62304752) 
Nothing to worry about,
this is some internal stuff with id() and special method in tuple creation.


RE: what is the __dict__ object? - wavic - Mar-28-2018

Perhaps this will give us some clue:

>>> class A:
...     pass
... 
>>> class B:
...     pass
... 
>>> for obj in (A.__dict__, B.__dict__):
...     print(type(obj))
... 
<class 'mappingproxy'>
<class 'mappingproxy'>
Now, the question is: what is mappingproxy? Smile


Hm! Didn't get anything.
https://docs.python.org/3/library/types.html#types.MappingProxyType


RE: what is the __dict__ object? - aaron1989041 - Mar-29-2018

(Mar-28-2018, 01:32 PM)snippsat Wrote:
(Mar-28-2018, 09:29 AM)aaron1989041 Wrote: i think if id() function gives the same address for two objects,they must be the same object.
Use code tags. There not same object,but something strange happens when making into a tuple object.
>>> class A: ... pass ... ... class B: ... pass >>> id(A.__dict__) 62085776 >>> id(B.__dict__) 62373680 
So when use , between object,Python is making a tuple. This is when the somewhat strange execution happens.
>>> id(A.__dict__), id(B.__dict__) (136355152, 136355152) 
It look like when using special method in tuple creation it call same object.
>>> a = 'foo' >>> b = 'bar' >>> id(a) 128252192 >>> id(b) 61876128 >>> id(a), id(b) (128252192, 61876128)
Using .__add__ the same happens.
>>> id(a.__add__), id(b.__add__) (62304752, 62304752) 
Nothing to worry about, this is some internal stuff with id() and special method in tuple creation.

I try to spit the print statements a few times,and their address is not always different,maybe 5 of 10 of them still give me same address:
class A:
    pass
class B:
    pass

print(id(A.__dict__))
print(id(B.__dict__))
print(A.__dict__ is B.__dict__)
Output:
7908376 7908376 False



RE: what is the __dict__ object? - Gribouillis - Mar-29-2018

Obviously the call id(A.__dict__) allocates a new object of type mappingproxy, that is to say a smart pointer. You are printing the id of these smart pointers. In the examples above, when B.__dict__ is accessed, the mappingproxy created for A.__dict__ is already destroyed, so the same address is reused. If you keep a reference to the proxy, two different addresses are used
>>> x = A.__dict__
>>> y = B.__dict__
>>> id(x), id(y)
(140355706316360, 140355683451672)