How to mix-in __getattr__ after the fact?

Lie Ryan lie.1296 at gmail.com
Fri Oct 28 22:26:31 EDT 2011


On 10/29/2011 05:20 AM, Ethan Furman wrote:
>
> Python only looks up __xxx__ methods in new-style classes on the class
> itself, not on the instances.
>
> So this works:
>
> 8<----------------------------------------------------------------
> class Cow(object):
> pass
>
> def attrgetter(self, a):
> print "CAUGHT: Attempting to get attribute", a
>
> bessie = Cow()
>
> Cow.__getattr__ = attrgetter
>
> print bessie.milk
> 8<----------------------------------------------------------------

a minor modification might be useful:

bessie = Cow()
bessie.__class__.__getattr__ = attrgetter





More information about the Python-list mailing list