I am trying to understand when to define __getattr__ or __getattribute__. The python documentation mentions __getattribute__ applies to new-style classes. What are new-style classes?
I am trying to understand when to define __getattr__ or __getattribute__ . The python
Share
A key difference between
__getattr__and__getattribute__is that__getattr__is only invoked if the attribute wasn’t found the usual ways. It’s good for implementing a fallback for missing attributes, and is probably the one of two you want.__getattribute__is invoked before looking at the actual attributes on the object, and so can be tricky to implement correctly. You can end up in infinite recursions very easily.New-style classes derive from
object, old-style classes are those in Python 2.x with no explicit base class. But the distinction between old-style and new-style classes is not the important one when choosing between__getattr__and__getattribute__.You almost certainly want
__getattr__.