Programming Search⌘K

Featureadvanced

Attribute access customization

Also called __getattr__, __getattribute__, __setattr__, __delattr__, __dir__, module __getattr__, attribute hooks, delegation, proxy object.

__getattr__ runs only when the normal lookup fails. __getattribute__ runs for every read, and __setattr__ and __delattr__ run for every assignment and del. They are used for proxies and checks.

Language
Python

Synopsis

class Settings:
    def __init__(self, **values):
        object.__setattr__(self, "_values", dict(values))

    def __getattr__(self, name):
        try:
            return self._values[name]
        except KeyError:
            raise AttributeError(name) from None

    def __setattr__(self, name, value):
        if name not in self._values:
            raise AttributeError(f"unknown setting {name!r}")
        self._values[name] = value

    def __dir__(self):
        return list(self._values)

s = Settings(debug=False, level=1)
s.level = 3
print(s.debug, s.level, dir(s))
try:
    s.levle = 4
except AttributeError as e:
    print(e)
print(hasattr(s, "color"))
Output
False 3 ['debug', 'level']
unknown setting 'levle'
False

s.debug is not a normal attribute, so __getattr__ gives it. Each assignment goes through __setattr__, which rejects an unknown name.

Semantics

r1. These methods "customize the meaning of attribute access (use of, assignment to, or deletion of x.name) for class instances."1

r2. __getattr__ is "Called when the default attribute access fails with an AttributeError".1 "This method should either return the (computed) attribute value or raise an AttributeError exception."1 "Note that if the attribute is found through the normal mechanism, getattr() is not called. (This is an intentional asymmetry between getattr() and setattr().)"1 Example: delegate.

r3. __getattribute__ is "Called unconditionally to implement attribute accesses for instances of the class."1 "In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, object.getattribute(self, name)."1 Example: every-read.

r4. __setattr__ is "Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary)."1 "If setattr() wants to assign to an instance attribute, it should call the base class method with the same name, for example, object.setattr(self, name, value)."1 Example: recursion.

r5. __delattr__ is "Like setattr() but for attribute deletion instead of assignment."1 __dir__ is "Called when dir() is called on the object. An iterable must be returned. dir() converts the returned iterable to a list and sorts it."1

r6. A module can define __getattr__ too: "If an attribute is not found on a module object through the normal lookup, i.e. object.getattribute(), then getattr is searched in the module dict before raising an AttributeError. If found, it is called with the attribute name and the result is returned."1 Example: module.

r7. hasattr() "is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not."2 Example: lenient.

Examples

class Logged:
    def __init__(self, target):
        self._target = target
        self.calls = []

    def __getattr__(self, name):
        self.calls.append(name)
        return getattr(self._target, name)

items = Logged([3, 1, 2])
items.append(0)
items.sort()
print(items._target, items.calls)
Output
[0, 1, 2, 3] ['append', 'sort']

append and sort are not attributes of Logged, so __getattr__ passes them on to the list. calls and _target are found the normal way, so they are not logged (rule r2).

class Traced:
    def __init__(self):
        self.x = 1

    def __getattribute__(self, name):
        print("get", name)
        return object.__getattribute__(self, name)

t = Traced()
print(t.x + t.x)
Output
get x
get x
2
import types

mod = types.ModuleType("config")
mod.known = 1

def __getattr__(name):
    if name.startswith("legacy_"):
        return f"<{name} is deprecated>"
    raise AttributeError(name)

mod.__getattr__ = __getattr__
print(mod.known, mod.legacy_mode)
Output
1 <legacy_mode is deprecated>

The function is in the module dictionary, so Python calls it for a name that the module does not have (rule r6). In a file, define __getattr__ at the top level of the module.

Pitfalls

An assignment to self.name inside __setattr__ calls __setattr__ again, without end:

This code raises RecursionError, on purpose.

class Loud:
    def __setattr__(self, name, value):
        self.name = value

Loud().x = 1

Call object.__setattr__(self, name, value) instead (rule r4).

A __getattr__ that never raises AttributeError makes every name look present:

This code runs but gives a wrong answer on purpose.

class Lenient:
    def __getattr__(self, name):
        return None

obj = Lenient()
print(hasattr(obj, "anything"), obj.colour)
Output
True None

hasattr() depends on the AttributeError (rule r7), and a misspelt name gives None, not an error. Raise AttributeError for a name that the class does not know (rule r2).

Comparison

Go has no attribute hooks. The type of x decides each selector: "x.f denotes the field or method at the shallowest depth in T where there is such an f."3 "In all other cases, x.f is illegal."3 In Python the lookup happens at run time, and a class can change it (rule r1).

References

  1. The Python Language Reference, 3. Data model, 3.3.2. Customizing attribute access; 3.3.2.1. Customizing module attribute access
  2. Built-in Functions, Python 3.15 documentation, hasattr
  3. The Go Programming Language Specification, Selectors

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source