Attribute Search Summary

This is the long version of the attribute access story, included just for the sake of completeness.

When retrieving an attribute from an object (print objectname.attrname) Python follows these steps:

  1. If attrname is a special (i.e. Python-provided) attribute for objectname, return it.

  2. Check objectname.__class__.__dict__ for attrname. If it exists and is a data-descriptor, return the descriptor result. Search all bases of objectname.__class__ for the same case.

  3. Check objectname.__dict__ for attrname, and return if found. If objectname is a class, search its bases too. If it is a class and a descriptor exists in it or its bases, return the descriptor result.

  4. Check objectname.__class__.__dict__ for attrname. If it exists and is a non-data descriptor, return the descriptor result. If it exists, and is not a descriptor, just return it. If it exists and is a data descriptor, we shouldn't be here because we would have returned at point 2. Search all bases of objectname.__class__ for same case.

  5. Raise AttributeError

Note that Python first checks for a data descriptor in the class (and its bases), then for the attribute in the object __dict__, and then for a non-data descriptor in the class (and its bases). These are points 2, 3 and 4 above.

The descriptor result above implies the result of calling the __get__() method of the descriptor with appropriate arguments. Also, checking a __dict__ for attrname means checking if __dict__["attrname"] exists.

Now, the steps Python follows when setting a user-defined attribute (objectname.attrname = something):

  1. Check objectname.__class__.__dict__ for attrname. If it exists and is a data-descriptor, use the descriptor to set the value. Search all bases of objectname.__class__ for the same case.

  2. Insert something into objectname.__dict__ for key "attrname".

  3. Think "Wow, this was much simpler!"

What happens when setting a Python-provided attribute depends on the attribute. Python may not even allow some attributes to be set. Deletion of attributes is very similar to setting as above.