Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python getattr vs getattribute: Key Differences

Understand when Python calls __getattr__ vs __getattribute__, how to override each safely, and how to avoid recursion and performance problems.

~3 min readPython
#Python#Attribute Access#getattr#getattribute
Diagram comparing Python's getattr and getattribute attribute lookup methods

When you access an attribute in Python, the interpreter calls __getattribute__ first. If that lookup fails, Python calls __getattr__ as a fallback if the class defines one. These two dunder hooks are the real methods behind the common shorthand getattr vs getattribute. The built-in getattr(obj, name) function is a separate utility that uses the same lookup process and can return a default value if the attribute is missing. Knowing when each hook runs is essential for writing custom attribute behavior without breaking normal object operations.

How Python Resolves Attribute Access

Every ordinary attribute access on an object calls __getattribute__ first, whether the attribute exists or not. The default object.__getattribute__ implementation looks for the attribute in the instance dictionary and in the class hierarchy, including descriptors. If it cannot find the attribute, it raises AttributeError; Python's attribute access machinery then calls __getattr__ as a fallback. Therefore, __getattr__ is only invoked when the normal lookup fails, not when the attribute is found through the standard mechanism.

python
class Example: def __getattribute__(self, name): print(f'__getattribute__ called for {name}') return super().__getattribute__(name) def __getattr__(self, name): print(f'__getattr__ called for {name}') raise AttributeError(name) e = Example() e.existing = 1 print(e.existing) # __getattribute__ called for existing print(e.missing) # __getattribute__ called for missing, then __getattr__ called, then AttributeError

In the example, __getattribute__ runs for both existing and missing. __getattr__ only runs for missing because the normal lookup failed.

Overriding __getattribute__: When and How

Overriding __getattribute__ gives you control over every ordinary attribute access on an instance. This is useful for logging, access control, or transforming attribute values before they are returned. However, you must be careful to call super().__getattribute__ to avoid infinite recursion.

python
class ReadOnlyProxy: def __init__(self, obj): self._obj = obj def __getattribute__(self, name): if name.startswith('_'): return super().__getattribute__(name) return getattr(self._obj, name)

Here, internal attributes like _obj are accessed normally, while public attributes are delegated to the wrapped object. Without the super() call, any attribute access would recursively call __getattribute__ again, causing a RecursionError.

Overriding __getattr__: When and How

__getattr__ is the right place to handle missing attributes gracefully. Common uses include returning a default value, generating dynamic attributes, or delegating to a fallback object. Because it is only called when the attribute is absent, it does not interfere with normal attribute access.

python
class DynamicConfig: def __init__(self, defaults): self._defaults = defaults def __getattr__(self, name): if name in self._defaults: return self._defaults[name] raise AttributeError(name) config = DynamicConfig({'timeout': 30}) print(config.timeout) # 30 print(config.retries) # AttributeError

This pattern is safe because __getattr__ only runs when the attribute is not found in the normal way. If the attribute exists, the default lookup wins.

Common Pitfalls and Recursion Errors

The most frequent mistake is overriding __getattribute__ without calling super(). Even accessing self.__dict__ inside __getattribute__ triggers another __getattribute__ call, leading to infinite recursion.

python
class Broken: def __getattribute__(self, name): return self.__dict__[name] # RecursionError

The correct way is to use super().__getattribute__(name) or object.__getattribute__(self, name) to bypass the override.

Another pitfall is using hasattr(self, name) inside __getattr__. Since hasattr() calls getattr() internally, this can cause recursive behavior. Instead, check the appropriate internal storage directly (for example, self.__dict__) or use try/except.

Performance and Maintainability Considerations

Overriding __getattribute__ adds overhead to every attribute access, even for attributes that exist. This can slow down hot code paths. __getattr__ only runs when an attribute is missing, so its performance impact is limited to failure cases.

If you are considering broad attribute interception, think about whether a property or another descriptor would meet your needs. Descriptors are more explicit and often easier to maintain than a blanket __getattribute__ override.

When you do override __getattribute__, keep the logic minimal and delegate to super() as early as possible. This reduces the chance of introducing bugs and keeps the runtime cost predictable.

Choosing Between __getattr__ and __getattribute__

Use __getattr__ when you only need to handle missing attributes. It is the safer, more targeted choice and does not affect existing attribute lookups.

Use __getattribute__ when you must intercept every ordinary attribute access, including existing ones. This is necessary for proxies, logging, or dynamic behavior that depends on the attribute name.

python
class Proxy: def __init__(self, target): self._target = target def __getattribute__(self, name): if name.startswith('_'): return super().__getattribute__(name) return getattr(self._target, name)

In this proxy, __getattribute__ is the right tool because it must forward all public attribute accesses. If you only needed a default value for missing attributes, __getattr__ would be simpler and less invasive.

Edge Cases and Compatibility

Implicit calls to special methods such as len(obj) or iter(obj) do not go through the instance's __getattribute__; Python looks them up on the type. If you need to intercept those, you must define them explicitly on the class.

Also, __getattr__ is not called for attributes that are found in the class's method resolution order. If a class defines a method, the normal lookup finds it, so __getattr__ is not invoked for that name. To change that behavior, you would need to modify the class or use a metaclass.

Understanding these boundaries helps you avoid surprising behavior when combining attribute interception with inheritance or special method protocols.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.