Python Forum
what is the __dict__ object?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is the __dict__ object?
#1
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?
Reply
#2
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.
Reply
#3
>>> 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?
Reply
#4
(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.
Reply
#5
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....gProxyType
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(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
Reply
#7
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __dict__ in math library akbarza 6 876 Sep-28-2023, 08:23 AM
Last Post: buran
  dict and __dict__ cls0724 1 2,587 Apr-05-2020, 07:45 PM
Last Post: Mateusz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020