I am trying to understand the difference between __getattr__ and __getattribute__, however, I am failing at it.
The answer to the Stack Overflow question Difference between __getattr__ vs __getattribute__ says:
__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.
I have absolutely no idea what that means.
Then it goes on to say:
You almost certainly want
__getattr__.
Why?
I read that if __getattribute__ fails, __getattr__ is called. So why are there two different methods doing the same thing? If my code implements the new style classes, what should I use?
I am looking for some code examples to clear this question. I have Googled to best of my ability, but the answers that I found don’t discuss the problem thoroughly.
If there is any documentation, I am ready to read that.
Some basics first.
With objects, you need to deal with their attributes. Ordinarily, we do
instance.attribute. Sometimes we need more control (when we do not know the name of the attribute in advance).For example,
instance.attributewould becomegetattr(instance, attribute_name). Using this model, we can get the attribute by supplying the attribute_name as a string.Use of
__getattr__You can also tell a class how to deal with attributes which it doesn’t explicitly manage and do that via
__getattr__method.Python will call this method whenever you request an attribute that hasn’t already been defined, so you can define what to do with it.
A classic use case:
Caveats and use of
__getattribute__If you need to catch every attribute regardless whether it exists or not, use
__getattribute__instead. The difference is that__getattr__only gets called for attributes that don’t actually exist. If you set an attribute directly, referencing that attribute will retrieve it without calling__getattr__.__getattribute__is called all the times.