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


Groups > comp.lang.python > #18186

Re: About instance.name look up order

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'attributes': 0.05; 'interpreter': 0.05; 'attribute': 0.07; 'method,': 0.07; 'python': 0.08; 'callable': 0.09; 'it;': 0.09; '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; 'throw': 0.09; 'output': 0.10; 'def': 0.13; 'class,': 0.15; '"x"': 0.16; "'int'": 0.16; 'exception?': 0.16; 'lookup': 0.16; 'stored.': 0.16; 'ubuntu.': 0.16; 'wrote:': 0.18; 'english.': 0.18; 'instance': 0.18; 'cheers': 0.23; 'creating': 0.25; 'far.': 0.29; 'looks': 0.29; 'print': 0.29; 'class': 0.29; 'lines': 0.30; 'subject:skip:i 10': 0.30; 'typeerror:': 0.30; 'thanks': 0.31; 'does': 0.32; 'header :User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'named': 0.33; 'object': 0.33; 'to:addr:python-list': 0.34; 'url:python': 0.36; 'example,': 0.37; 'none': 0.37; 'bound': 0.37; 'but': 0.37; 'hello,': 0.37; 'skip:_ 10': 0.37; 'too,': 0.38; 'received:org': 0.38; 'put': 0.38; 'url:docs': 0.39; 'url:org': 0.39; 'define': 0.39; 'why': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'order': 0.62; 'property': 0.63; 'subject:About': 0.68; 'header :Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'reply- to:addr:gmail.com': 0.78; 'requested,': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; '-->': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Kev Dwyer <kevin.p.dwyer@gmail.com>
Subject Re: About instance.name look up order
Followup-To gmane.comp.python.general
Date Thu, 29 Dec 2011 18:03:13 +0000
References <a0bb7fc6-b5db-4f02-bd3a-348ee0175b0e@r13g2000prr.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host cpc7-hari14-2-0-cust162.20-2.cable.virginmedia.com
Mail-Copies-To kevin.p.dwyer@gmail.com
User-Agent KNode/4.4.10
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To kevin.p.dwyer@gmail.com
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.4228.1325181818.27778.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1325181818 news.xs4all.nl 6988 [2001:888:2000:d::a6]:38341
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18186

Followups directed to: gmane.comp.python.general

Show key headers only | View raw


Prim wrote:

> First, sorry about my poor english.
> Put these in IPython under ubuntu.
> 
-------------------------------------------------------------------------------------
> class C:
>     def __init__(self):
>         self.x = 1
>     def print(self):
>         print self.x
> c = C()
> c.x --> 1, so c.x mean a attr of c named 'x'
> c.print() --> pirnt 1, so c.print mean a method of c named 'print'
> 
-------------------------------------------------------------------------------------
> class C:
>     def __init__(self):
>         self.x = 1
>     def x(self):
>         print 'x method'
>     def y(self):
>         print 'y method'
> c = C()
> c.x --> 1
> c.x() --> TypeError: 'int' object is not callable
> c.y --> bound method C.y
> #Q1: instance.name will get the attr first, than method?
> 
-------------------------------------------------------------------------------------
> class C:
>     def x(self):
>         print 'x method'
>     def __getattr__(self, attr):
>         print 'in __getattr__ method'
>         return attr
> c = 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?
> 
> t = c.x
> t --> print in __getattr__ method, then throw TypeError: 'str' object
> is not callable
> t() --> print x method
> t = c.x() --> print x method, t == None
> #Q3 why t=c.x() and c.x() output different?
> 
> #Q4, if when I define the class use property too, then instance.name
> look up order would be?
> 
> Thanks for your reply.

Hello,

Python always looks for attributes in the instance first, then in the class,
and then in the class's superclasses.  In your first example, by defining
"x" in C.__init__ you are creating an instance attribute named "x".  When 
the attribute c.x is requested, Python finds an attribute "x" in the 
instance and returns it; the method "x" is found in the class, but the 
attribute lookup does not proceed this far.

Try looking at C.__dict__ and c.__dict__ in the interpreter to see how the 
attributes are stored.  

See also 
http://docs.python.org/reference/datamodel.html#customizing-attribute-access

Cheers

Kev

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


Thread

About instance.name look up order Prim <ukim86@gmail.com> - 2011-12-29 08:07 -0800
  Re: About instance.name look up order Kev Dwyer <kevin.p.dwyer@gmail.com> - 2011-12-29 18:03 +0000
  Re: About instance.name look up order Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-29 11:35 -0700

csiph-web