How to mix-in __getattr__ after the fact?

Jerry Hill malaclypse2 at gmail.com
Fri Oct 28 14:14:34 EDT 2011


On Fri, Oct 28, 2011 at 1:34 PM, dhyams <dhyams at gmail.com> wrote:
> If I call __getattr__ directly, as in bessie.__getattr__('foo'), it
> works as it should obviously; so the method is bound and ready to be
> called.  But Python does not seem to want to call __getattr__
> appropriately if I mix it in after the object is already created.  Is
> there a workaround, or am I doing something wrongly?

Python looks up special methods on the class, not the instance (see
http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes).

It seems like you ought to be able to delegate the special attribute
access from the class to the instance somehow, but I couldn't figure
out how to do it in a few minutes of fiddling at the interpreter.

-- 
Jerry



More information about the Python-list mailing list