Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15126

Re: How to mix-in __getattr__ after the fact?

From Lie Ryan <lie.1296@gmail.com>
Subject Re: How to mix-in __getattr__ after the fact?
Date 2011-10-29 13:26 +1100
References <4630c4d0-86bc-4295-8bbb-6f58415191c8@v15g2000vbm.googlegroups.com> <4EAAF264.1050307@stoneleaf.us>
Newsgroups comp.lang.python
Message-ID <mailman.2298.1319855210.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to mix-in __getattr__ after the fact? dhyams <dhyams@gmail.com> - 2011-10-28 10:34 -0700
  Re: How to mix-in __getattr__ after the fact? Jerry Hill <malaclypse2@gmail.com> - 2011-10-28 14:14 -0400
  Re: How to mix-in __getattr__ after the fact? Ethan Furman <ethan@stoneleaf.us> - 2011-10-28 11:20 -0700
  Re: How to mix-in __getattr__ after the fact? Lie Ryan <lie.1296@gmail.com> - 2011-10-29 13:26 +1100
    Re: How to mix-in __getattr__ after the fact? dhyams <dhyams@gmail.com> - 2011-10-31 05:01 -0700
      Re: How to mix-in __getattr__ after the fact? DevPlayer <devplayer@gmail.com> - 2011-11-01 22:50 -0700
      Re: How to mix-in __getattr__ after the fact? Lie Ryan <lie.1296@gmail.com> - 2011-11-08 15:17 +1100
        Re: How to mix-in __getattr__ after the fact? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-08 05:58 +0000
      Re: How to mix-in __getattr__ after the fact? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-11-08 11:21 +0100

csiph-web