Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15112
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ethan@stoneleaf.us> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'attempting': 0.04; 'attribute': 0.07; 'python': 0.08; 'called.': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'def': 0.15; '2.7.2': 0.16; 'attribute"': 0.16; 'instances.': 0.16; 'new- style': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'wrote:': 0.16; 'thanks,': 0.18; 'to:2**1': 0.20; 'header:In-Reply-To:1': 0.22; 'itself,': 0.23; 'mix': 0.23; 'skip:b 20': 0.26; "i'm": 0.27; 'classes': 0.28; 'skip:_ 20': 0.28; 'import': 0.28; 'pass': 0.29; 'bound': 0.29; 'looks': 0.29; 'print': 0.29; 'script': 0.29; 'class': 0.30; 'subject:?': 0.31; 'seem': 0.31; 'does': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'skip:= 10': 0.34; 'here,': 0.35; 'trouble': 0.35; 'object': 0.35; 'subject:How': 0.35; 'doing': 0.36; 'but': 0.37; 'something': 0.37; 'should': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'received:websitewelcome.com': 0.64; 'here': 0.65; 'ready': 0.65; 'received:184': 0.67; 'received:69.93': 0.67 |
| Date | Fri, 28 Oct 2011 11:20:20 -0700 |
| From | Ethan Furman <ethan@stoneleaf.us> |
| User-Agent | Thunderbird 1.5.0.10 (Windows/20070221) |
| MIME-Version | 1.0 |
| To | dhyams <dhyams@gmail.com>, python-list@python.org |
| Subject | Re: How to mix-in __getattr__ after the fact? |
| References | <4630c4d0-86bc-4295-8bbb-6f58415191c8@v15g2000vbm.googlegroups.com> |
| In-Reply-To | <4630c4d0-86bc-4295-8bbb-6f58415191c8@v15g2000vbm.googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-AntiAbuse | This header was added to track abuse, please include it with any abuse report |
| X-AntiAbuse | Primary Hostname - gator410.hostgator.com |
| X-AntiAbuse | Original Domain - python.org |
| X-AntiAbuse | Originator/Caller UID/GID - [47 12] / [47 12] |
| X-AntiAbuse | Sender Address Domain - stoneleaf.us |
| X-BWhitelist | no |
| X-Source | |
| X-Source-Args | |
| X-Source-Dir | |
| X-Source-Sender | mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:1887 |
| X-Source-Auth | ethan+stoneleaf.us |
| X-Email-Count | 1 |
| X-Source-Cap | dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2290.1319825995.27778.python-list@python.org> (permalink) |
| Lines | 58 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1319825995 news.xs4all.nl 6871 [2001:888:2000:d::a6]:48845 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:15112 |
Show key headers only | View raw
dhyams wrote:
> Python 2.7.2
>
> I'm having trouble in a situation where I need to mix-in the
> functionality of __getattr__ after the object has already been
> created. Here is a small sample script of the situation:
>
> =============snip
>
> import types
>
> class Cow(object):
> pass
> # this __getattr__ works as advertised.
> #def __getattr__(self,a):
> # print "CAUGHT INCLASS: Attempting to get attribute ",a
>
>
> def attrgetter(self,a):
> print "CAUGHT: Attempting to get attribute ",a
>
> bessie = Cow()
>
> bessie.__getattr__ = types.MethodType(attrgetter,bessie,Cow)
>
> # here, I should see my printout "attempting to get attribute"
> # but I get an AttributeException
> print bessie.milk
> ======================snip
>
> 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?
>
> Thanks,
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<----------------------------------------------------------------
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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