Q48: What is the difference between __getattr__ and __getattribute__?

  • __getattribute__(self, name): Invoked unconditionally for EVERY attribute access attempt on an instance.
  • __getattr__(self, name): Invoked ONLY when the attribute is NOT found in the instance __dict__ or class hierarchy (fallback lookup).
Key Takeaways
  • __getattribute__: Called for all attribute access attempts (risk of infinite recursion). __getattr__: Fallback hook called only for missing attributes.
Knowledge Check

How do you avoid infinite recursion inside a custom __getattribute__ implementation?