Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'classes.': 0.07; 'python': 0.09; 'name):': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'lambda': 0.16; 'other:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'tweak': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'thanks,': 0.18; 'skip:" 40': 0.20; 'sort': 0.21; 'made.': 0.22; 'academic': 0.23; 'raise': 0.24; 'header:User-Agent:1': 0.26; '(most': 0.27; 'realize': 0.27; 'header:X-Complaints-To:1': 0.28; 'initial': 0.28; 'question:': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'file': 0.32; 'could': 0.32; 'getting': 0.33; 'traceback': 0.33; 'to:addr :python-list': 0.33; 'decisions': 0.35; 'received:org': 0.36; 'but': 0.36; "wasn't": 0.36; 'does': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'kind': 0.61; 'want,': 0.65; 'special': 0.73; 'x):': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: __getattr__ Confusion Date: Mon, 04 Feb 2013 19:23:55 +0100 Organization: None References: <095a0432-f8a4-40b4-96ed-1588896fba66@googlegroups.com> <510f3a91$0$29866$c3e8da3$5496439d@news.astraweb.com> <863c89b0-2125-4525-8ad8-1111b0a6beae@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b0cb.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360002178 news.xs4all.nl 6979 [2001:888:2000:d::a6]:39569 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38135 Saul Spatz wrote: > Thanks, Peter. I realize this is getting sort of academic now, as I know > how to do exactly what I want, but I'm still confused. Is __getattr__ a > special case then, even for classic classes? Well, it never occured to me to try a per-instance __getattr__(), but you are about to answer your own question: > class Adder(): # python 2.7, classic class > def __init__(self, x): > self.x = x > self.__add__= lambda other: Adder(self.x+other.x) > self.__getattr__ = lambda name: self.test(name) > > def __str__(self): > return str(self.x) > > def test(self, name): > print("Hello from test") > raise AttributeError > > x = Adder(3) > y = Adder(4) > print(x+y) > x.junk() > > 7 > Traceback (most recent call last): > File "C:\Users\Saul\Documents\PythonProjects\test.py", line 18 > AttributeError: Adder instance has no attribute 'junk' > > Why does this work for __add__ and not for __getattr__? I don't know, I wasn't around when these decisions were made. It could be the initial performance tweak that would lead to a generalisation with newstyle classes. Or it is some kind of bootstrapping issue...