Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'ideally': 0.04; 'attribute': 0.07; 'found,': 0.07; 'memory.': 0.07; 'suppose': 0.07; 'method,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'a()': 0.16; 'attributes.': 0.16; 'called.': 0.16; 'clear.': 0.16; 'dictionary.': 0.16; 'example?': 0.16; 'gpg': 0.16; 'subject:loss': 0.16; 'underlying': 0.16; '\xa0def': 0.16; 'subject:python': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'print': 0.22; '\xa0if': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'references': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'combination': 0.36; 'in.': 0.36; 'words,': 0.36; 'url:listinfo': 0.36; 'shows': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'sometimes': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'url:mail': 0.40; 'dave': 0.60; 'url:about': 0.61; 'name': 0.63; 'such': 0.63; 'more': 0.64; 'between': 0.67; 'mar': 0.68; '26,': 0.68; 'containing': 0.69; 'url:me': 0.69; 'angel': 0.91; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:x-originating-ip:in-reply-to:references :date:message-id:subject:from:to:cc:content-type:x-gm-message-state; bh=jHQdsr+ZrwSzoz4sAuz+TEBuaWbg5bS9S+N8aM2d6K0=; b=o3V5u1dVYn8aD+sf/nIRODdxro4+I43Z2LAonMZesyrlDBM7QbA3Sh5+toUF3/EHIB R0qO4eu+qQKeAwiDouePodY5UWJrKrLiAk9nDxZ9VLRLV1FlHRU1RyH+IgoNBGifcGRX HvIrx85f0wpC8y4fSbzLFoBesDddKdchF7TH2Q3l3crwzcAalzS2soIEId79IoKVXTrc cmh3MB5+/OOOUzcJYgC/B/Tvr3zxIEvbfIykfz1RXz5RxPvtm3EajI2uzbdTSBG1raEP sp+7Ji6MacSk543GoULVJoEnN+zJpCHR3GbggD7BiDT/dHKB5PM0Qmo17ZJwBGdu+BIT isSQ== MIME-Version: 1.0 X-Received: by 10.182.144.73 with SMTP id sk9mr1947718obb.20.1364295820651; Tue, 26 Mar 2013 04:03:40 -0700 (PDT) X-Originating-IP: [59.66.130.213] In-Reply-To: <51517370.4060305@davea.name> References: <51517370.4060305@davea.name> Date: Tue, 26 Mar 2013 19:03:40 +0800 Subject: Re: At a loss on python scoping. From: Shiyao Ma To: Dave Angel Content-Type: multipart/alternative; boundary=14dae9340d49c25d2104d8d1df62 X-Gm-Message-State: ALoCoQmN07gBBOJkKGhf7AtFKn4JGlbDfvRVAwljuG9tdU3uHyRdDfN6xwkG4LLGHbme7yvx5d4N Cc: python-list@python.org 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: 125 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364295823 news.xs4all.nl 6841 [2001:888:2000:d::a6]:43518 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41879 --14dae9340d49c25d2104d8d1df62 Content-Type: text/plain; charset=ISO-8859-1 Thx, really a nice and detailed explanation. On Tue, Mar 26, 2013 at 6:07 PM, Dave Angel wrote: > On 03/26/2013 02:17 AM, Shiyao Ma wrote: > >> Hi, >> suppose I have a file like this: >> class A: >> r = 5 >> def func(self, s): >> self.s = s >> a = A() >> print(a.r) # this should print 5, but where does py store the name of r >> >> a.func(3) >> print(a.s) # this should print 3, also where does py store this name. >> what's the underlying difference between the above example? >> >> > I don't think this is a scoping question at all. These references are > fully qualified, so scoping doesn't enter in. > > The class A has a dictionary containing the names of r and func. These > are class attributes. Each instance has a dictionary which will contain > the name s AFTER the A.func() is called. Ideally such an attribute will be > assigned in the __init__() method, in which case every instance will have s > in its dictionary. > > When you use a.qqq the attribute qqq is searched for in the instance > dictionary and, if not found, in the class dictionary. If still not found, > in the parent classes' dictionary(s). > > You can use dir(A) and dir(a) to look at these dictionaries, but it shows > you the combination of them, so it's not as clear. In other words, dir(a) > shows you both dictionaries, merged. (Seems to me dir also sometimes > censors some of the names, but that's a vague memory. It's never left out > anything I cared about, so maybe it's things like single-underscore names, > or maybe just a poor memory.) > > > -- > DaveA > -- > http://mail.python.org/**mailman/listinfo/python-list > -- My gpg pubring is available via: gpg --keyserver subkeys.pgp.net--recv-keys 307CF736 More on: http://about.me/introom --14dae9340d49c25d2104d8d1df62 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thx, really a nice and detailed explanation.

On Tue, Mar 26, 2013 at 6:07 PM, Dave Angel <davea@davea.name> wrote:
On 03/26/2013 02:17 AM, Sh= iyao Ma wrote:
Hi,
suppose I have a file like this:
class A:
=A0 =A0 =A0r =3D 5
=A0 =A0 =A0def func(self, s):
=A0 =A0 =A0 =A0 =A0self.s =3D s
a =3D A()
print(a.r) =A0 =A0# this should print 5, but where does py store the name o= f r

a.func(3)
print(a.s) =A0 =A0# this should print 3, also where does py store this name= .
what's the underlying difference between the above example?


I don't think this is a scoping question at all. =A0These references ar= e fully qualified, so scoping doesn't enter in.

The class A has a dictionary containing the names of r and func. =A0These a= re class attributes. =A0Each instance has a dictionary which will contain t= he name s AFTER the A.func() is called. =A0Ideally such an attribute will b= e assigned in the __init__() method, in which case every instance will have= s in its dictionary.

When you use a.qqq =A0the attribute qqq is searched for in the instance dic= tionary and, if not found, in the class dictionary. =A0If still not found, = in the parent classes' dictionary(s).

You can use dir(A) and dir(a) to look at these dictionaries, but it shows y= ou the combination of them, so it's not as clear. =A0In other words, di= r(a) shows you both dictionaries, merged. =A0(Seems to me dir also sometime= s censors some of the names, but that's a vague memory. It's never = left out anything I cared about, so maybe it's things like single-under= score names, or maybe just a poor memory.)


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list



-- My gpg pubring is available via: gpg --keyserver subkeys.pgp.net --recv-keys 307CF736
More on: http://about= .me/introom

--14dae9340d49c25d2104d8d1df62--