Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
    >>> dir(classAInstance.objectTInstance.__dict__)
    ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__',
 '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
 '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__setitem__', '__str__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 
'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> classAInstance.objectTInstance.attB             # Access Method 1
>>> classAInstance.objectTInstance.__dict__['attB'] # Access Method 2
>>> classAInstance.objectTInstance['attB']          # Access Method 3
Traceback (most recent call last):
  File "<input>", line 1, in ?
AttributeError: ObjectT instance has no attribute '__getitem__'

Claim: classAInstance is an insance of ClassA and objectTInstance is an instance of ObjectT.

Question>where does classAInstance.objectTInstance store the attribute attB? And why the Access Method 3 doesn't work? what exactly does __dict__ store?

 object.__dict__
     A dictionary or other mapping object used to store an object’s (writable) attributes.

Based on my understanding, Access method 1 and 2 are exactly same. Also the attB is stored in the __dict__ of the object of ObjectT. In order to make Access 3 work, ObjectT has to provide a dictionary magic function called __getitem__.

Is this correct?

Thank you

When accessing an attribute via the first method you tried, Python first checks __dict__ to see if the attribute is in there. If it is, that's what it uses. It then checks the __dict__ of the __class__ of the object. If it's still not found, it continues searching superclasses. (If multiple inheritance is involved, it gets more complex.) If it is still not found, it looks for __getattr__ of the class or any superclasses and calls that to get the attribute. If all of those fail, it errors.

The second method directly accesses the __dict__ of the object. It will not check the classes' attributes or call __getattr__ like the first does.

The third method is something else completely; it tries to call __getitem__ (don't confuse that with __getattr__).

The source of your confusion is that you have a dictionary object that has a __dict__ attribute that also points to a dictionary. Your object and its __dict__ attribute are not the same dictionary.

Most Python objects have a __dict__ attribute that is used to store attributes of the object; these attributes are accessed using the . syntax (which searches the __dict__ of your object, along with its class and the classes it is derived from). When your object is a dictionary, it also stores items in itself. These are accessed using the [...] syntax.

It is possible to write __getattr__() and __setattr__() methods on your dict subclass that allow items in the dictionary to also be accessed using dot notation, a la JavaScript. This is generally a bad idea because Python programmers don't expect Python to act like JavaScript.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.