Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attempting': 0.04; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; 'def': 0.15; 'instances.': 0.16; 'new-style': 0.16; 'wrote:': 0.16; 'header:In-Reply-To:1': 0.22; 'itself,': 0.23; 'modification': 0.23; 'skip:b 20': 0.26; 'classes': 0.28; 'pass': 0.29; 'looks': 0.29; 'print': 0.29; 'class': 0.30; 'subject:?': 0.31; 'minor': 0.32; 'to:addr:python-list': 0.33; 'header:User- Agent:1': 0.34; 'header:X-Complaints-To:1': 0.35; 'subject:How': 0.35; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'might': 0.40; '05:20': 0.84; 'received:120': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Lie Ryan Subject: Re: How to mix-in __getattr__ after the fact? Date: Sat, 29 Oct 2011 13:26:31 +1100 References: <4630c4d0-86bc-4295-8bbb-6f58415191c8@v15g2000vbm.googlegroups.com> <4EAAF264.1050307@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 120.20.114.150 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: <4EAAF264.1050307@stoneleaf.us> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1319855210 news.xs4all.nl 6842 [2001:888:2000:d::a6]:39391 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15126 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