Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Classes and Special Methods

__getattr__() and __getattribute__()

Reference for Python __getattr__ and __getattribute__: attribute lookup order, dynamic attributes, proxies, infinite recursion and __setattr__ and __delattr__.

Two special methods let a class take part in attribute lookup. __getattribute__() is called for every attribute access on an instance. __getattr__() is a fallback: Python calls it only when the normal lookup fails and raises AttributeError.

__getattr__() is the one you will normally want: it supports dynamic attributes, proxies and wrappers, and default values. __getattribute__() is powerful but easy to get wrong, because it intercepts even your own self.x accesses.

Python
class Dynamic:
    def __getattr__(self, name):
        return f"<{name}?>"

print(Dynamic().color)   # <color?>

Try it

Python __getattr__ as a fallback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python __getattribute__, proxies and recursion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
class C:
    def __getattr__(self, name): ...              # fallback
    def __getattribute__(self, name): ...         # every access
    def __setattr__(self, name, value): ...       # every assignment
    def __delattr__(self, name): ...              # every deletion
Method Called
__getattribute__(name) On every attribute read, before anything else.
__getattr__(name) Only if the normal lookup (instance dict, class, parents, descriptors) raised AttributeError.
__setattr__(name, value) On every assignment obj.name = value.
__delattr__(name) On del obj.name.

Return value: the attribute value (for the getters). The setter and deleter return None.

Raises: AttributeError when the attribute does not exist. Raising it from __getattr__() is what makes getattr(obj, name, default) and hasattr() work correctly.

Usage notes

  • Order of lookup. Data descriptors (like properties) come first, then the instance __dict__, then class attributes, then __getattr__(). __getattribute__() wraps the whole process.
  • Avoid infinite recursion. Inside __getattribute__() and __setattr__(), reach the real storage through object.__getattribute__(self, name) or super().__setattr__(), never self.name. In __getattr__(), touching a missing attribute (like self.other above) recurses forever.
  • Guard names that may not exist yet. During copy and pickle the object is rebuilt without __init__, so self._target may be missing and __getattr__() is called for _target itself. Raise AttributeError for such names.
  • Raise AttributeError, not KeyError, for unknown names, otherwise hasattr() and getattr() with a default misbehave.
  • Special methods bypass these hooks. Implicit calls like len(obj) or obj + 1 look up methods on the type, so a proxy must define them explicitly.
  • Performance. __getattribute__() slows every access; __getattr__() costs only on misses.
  • Alternatives. @property for a few known computed attributes, __slots__ or dataclasses for structure, __dir__() to advertise dynamic names to dir().
  • Module level. A module may define __getattr__(name) too (Python 3.7+), useful for lazy imports and deprecation warnings.