On Tue, Apr 23, 2019 at 08:22:56PM +0530, Arup Rakshit wrote: > I read today 2 methods regarding the customizing the attribute > access:__getattr__ and __getattribute__ from > https://docs.python.org/3/reference/datamodel.html#special-method-names. > What I understood about them is that __getattr__ is called when the > requested attribute is not found, and an AttributeError is raised. But > later is called everytime unconditionally.
When you overload __getattribute__, your class will be slow because **every** attribute lookup has to go through your method, instead of only the lookups which fail. And unless you are very careful, you will break things: py> class X(object): ... def __init__(self, x): ... self.x = x ... def __getattribute__(self, name): ... if name is 'x': return self.x ... py> obj = X(999) py> obj.x Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in __getattribute__ [ previous line repeats 332 times ] RecursionError: maximum recursion depth exceeded while calling a Python object Normally, overriding __getattribute__ is considered a very advanced and unusual thing to do. -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor