Next / Previous / Contents / TCC Help System / NM Tech homepage

26.2.2. Attribute access control in new-style classes

New-style classes give you more ways to control what happens when an instance's attribute is accessed.

Here is the general procedure for access to attribute a of instance i, where C is the class of i.

  1. If the instance has a __getattribute__() special method (see Section 26.3.15, “__getattribute__(): Intercept all attribute references”), execute that method, which must either return the attribute value or raise AttributeError.

  2. If the instance has a __slots__ attribute (see Section 26.2.4, “Conserving memory with __slots__), return the value of the slot with name a. If a does not match any of the slot names, or if the named slot has never been set to a value, raise AttributeError.

  3. If a is a key in i.__dict__, return the corresponding value.

  4. Search for attribute a in class C. If that fails, search all the parent classes of C all the way back to object.

  5. If all searches in the preceding step failed and the instance has a .__getattr__() special method, call that method. See Section 26.3.14, “__getattr__(): Handle a reference to an unknown attribute”; please note the differences from Section 26.3.15, “__getattribute__(): Intercept all attribute references”.

  6. If all the above steps fail to produce a value, raise AttributeError.