__getattribute__ is the special method Python calls for every attribute access on an instance, such as obj.name, before it looks anywhere else. Overriding it lets you intercept all reads, for logging, access control or computed values. It is easy to get wrong, because a naive implementation recurses forever.
For most needs, __getattr__ (called only when normal lookup fails), properties or descriptors are safer and simpler.
When you write obj.attr, Python calls type(obj).__getattribute__(obj, "attr"). The default version in object searches data descriptors on the class, then the instance __dict__, then non-data descriptors and class attributes. If it finds nothing, it raises AttributeError, and only then does Python try __getattr__ if you defined it.
| Method | Called | Typical use |
|---|---|---|
__getattribute__ | Every attribute read | Logging, proxying, access control |
__getattr__ | Only when lookup fails | Defaults, dynamic attributes |
__setattr__ | Every attribute write | Validation |
__delattr__ | Every attribute delete | Cleanup hooks |
Notice that __init__ assigned self.value without triggering the message, because assignment goes through __setattr__, not __getattribute__. Every read, including self.value inside other methods, prints the message.
Inside __getattribute__, writing self.something triggers __getattribute__ again.
Always delegate to the parent implementation with super().__getattribute__(name) or object.__getattribute__(self, name) to reach the real value.
You can hide attributes or compute them on the fly:
This is advisory, not real security, since callers can still use object.__getattribute__(acct, "_secret") or vars(acct). Treat it as an API guard.
__getattr__ runs only for missing attributes, so existing ones are untouched and there is no recursion risk with normal lookups. It is the better tool for fallbacks and delegation to a wrapped object.
A Python-level __getattribute__ slows down every attribute access, including those made by methods of the class. It also bypasses the interpreter's fast paths for attribute lookup. Consider these alternatives first:
@property for one computed or validated attribute.__getattr__ for fallbacks.__slots__ to restrict attributes and save memory.Note that implicit special method lookups, such as len(obj) or obj + 1, look methods up on the type and do not go through __getattribute__ of the instance.
A common real use is a proxy that forwards attribute access to another object and records it.
The internal names are fetched through object.__getattribute__ to avoid recursion, and everything else is forwarded to the target. Writes would need a matching __setattr__. For simple forwarding, __getattr__ is enough and has less overhead.
__getattribute__ runs on every access, while __getattr__ is a fallback called only after normal lookup raised AttributeError.
You accessed self.<anything> inside it. Use super().__getattribute__ or object.__getattribute__ instead.
Not securely. It can block ordinary access, but Python has no true private attributes.
Rarely: for proxies, tracing tools or frameworks. Prefer properties or __getattr__ in application code.
__getattribute__ intercepts every attribute read on an instance. Delegate to super().__getattribute__ to avoid recursion, remember it costs performance on every access, and choose __getattr__, properties or descriptors when they are enough.