23 Jun 22:43
On __findattr__, __getattr__ and __getattribute__
From: Leo Soto M. <leo.soto <at> gmail.com>
Subject: On __findattr__, __getattr__ and __getattribute__
Newsgroups: gmane.comp.lang.jython.devel
Date: 2008-06-23 20:44:11 GMT
Subject: On __findattr__, __getattr__ and __getattribute__
Newsgroups: gmane.comp.lang.jython.devel
Date: 2008-06-23 20:44:11 GMT
Here is a short history of __findattr__, __getattr__ and __getattribute__, and why PyObject#object___getattribute__ can't directly call PyObject#__getattr__. I learned this in the hard way, but that's what you get when you attempt to modify a core component of the runtime without understanding all this: What are __findattr__ and __getattr__? - PyObject#__findattr__: Has the semantics of Python's __getattribute__ (that is, is called before looking on the object dict), but returns null instead of throwing Py.AttributeError when the lookup fails. - PyObject#__getattr__: Same as __findattr__ but *does* throws AttributeError and never returns null. Currently it is implemented in terms of __findattr__, in the obvious way. PyObject subclasses are expected to override __findattr__. When are they used? - From Java code wanting to access attributes of PyObjects. - When you do getattr(foo, bar) on Python code [and I'd bet that when you do foo.bar too, but I've not looked at the exact locations]. And what about __getattribute__? - There are methods *exposed* through the __getattribute__ descriptor which get called when you do foo.__getattribute__(bar) or type(foo).__getattribute__(bar)(Continue reading)
RSS Feed