Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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; 'classes.': 0.05; 'attribute': 0.07; 'ignored': 0.07; 'method,': 0.07; 'python': 0.08; 'callable': 0.09; 'deprecated': 0.09; 'throw': 0.09; 'am,': 0.12; 'def': 0.13; 'classes,': 0.13; 'class,': 0.15; 'exception?': 0.16; 'lookup': 0.16; 'new-style': 0.16; '\xa0def': 0.16; '\xa0print': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'instance': 0.18; 'cc:no real name:2**0': 0.20; 'dec': 0.22; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; "i'm": 0.26; 'message-id:@mail.gmail.com': 0.28; 'print': 0.29; 'cc:addr:python.org': 0.29; 'class': 0.29; 'lines': 0.30; 'subject:skip:i 10': 0.30; 'typeerror:': 0.30; 'thu,': 0.32; "can't": 0.32; 'object': 0.33; 'rule': 0.34; 'received:209.85.212': 0.34; 'properties': 0.36; 'class.': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'skip:_ 10': 0.37; 'using': 0.38; 'too,': 0.38; 'received:209.85': 0.38; 'should': 0.39; 'define': 0.39; 'why': 0.39; 'called': 0.40; 'received:209': 0.40; '2011': 0.61; 'order': 0.62; 'property': 0.63; 'subject:About': 0.68; '29,': 0.73; 'derive': 0.84; 'mechanism.': 0.84; '-->': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=8Hc61WP+rubNZwGnprJNIQNYWYKx8TnwWRw0GU1W+Cw=; b=I9FklQOJXVYYZKQIEDTietqSP1DlW7V0QE0Im8RCHmyV1DkHdXcWzwofCY+GVfNJyl 33K26lSNJgHIj70O8mgSPybEHiZjz2HxQqsDrLrRhWwpNP6W66eeVvTQMROTebQB2egS WBTfjWos6Y1BOcCSvbSiI3G5wczX+skUt0X6E= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Thu, 29 Dec 2011 11:35:49 -0700 Subject: Re: About instance.name look up order To: Prim Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325183782 news.xs4all.nl 6847 [2001:888:2000:d::a6]:37990 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18187 On Thu, Dec 29, 2011 at 9:07 AM, Prim wrote: > class C: > =A0 =A0def x(self): > =A0 =A0 =A0 =A0print 'x method' > =A0 =A0def __getattr__(self, attr): > =A0 =A0 =A0 =A0print 'in __getattr__ method' > =A0 =A0 =A0 =A0return attr > c =3D C() > c.x --> print in __getattr__ method, then throw TypeError: 'str' > object is not callable > c.x() --> print in __getattr__ method, x method 2 lines > #Q2: why c.x would get a exception? I'm not sure exactly what's going on there, but as a general rule you should use new-style classes, not classic classes, which are deprecated and have been removed in Python 3. In Python 2 if you define a class without any base classes, you get a classic class. To define C as a new-style class in Python 2, derive it from object (or another new-style class): >>> class C(object): ... def x(self): ... print 'x method' ... def __getattr__(self, attr): ... print 'in __getattr__ method' ... return attr ... >>> c =3D C() >>> c.x > >>> c.x() x method Note that in this case __getattr__ never gets called because the name was found using the normal lookup mechanism. You would need to use __getattribute__ if you want it to be called unconditionally. > #Q4, if when I define the class use property too, then instance.name > look up order would be? You can't use properties in classic classes. In a new-style class, if you access a property, then any instance attribute with the same name is ignored and can't be set in the first place.