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


Groups > comp.lang.python > #41880

Re: At a loss on python scoping.

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <i@introo.me>
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; 'received:209.85.219': 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; '>>>': 0.22; '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; 'received:209.85': 0.35; '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; 'so,': 0.37; 'received:209': 0.37; 'sometimes': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'url:mail': 0.40; 'read': 0.60; '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; 'to:none': 0.92; '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:cc:content-type:x-gm-message-state; bh=yBNx57VD3ajzn6SCxqHhLSOi0cYySR23ViXefwZvG14=; b=Gk3amQfiWZFtutvMaG2OPEY4sWBfDPlv1DCMqujEdSEErhXnbvqCQwMO3+jx26YcSf H4PLapdV+/wQi5w85/aD2FvPioY8LExeqN2C/6Qrizx6ad+53Oyt/0PntvPSIEwETOmM 4nrtqTkn/y7KFBIjI9LDZUGIvASA4V7lM1guEa/sCb9KJoPm0Dy81eFP0S2uttcSklMt y6aTtZJOzjU9+zwB+RjLPtu2P9cHB3EtW02wRrQnQpYQhUya8XkssWbTavNuQmggK/qM EXKZ0ExoSKTk/VGq0P8lGCq2nZh0MkQlmdk/iCX5Rxi9bYA3H8VaUBW35n7nFI6j4N7n w8Vg==
MIME-Version 1.0
X-Received by 10.182.118.105 with SMTP id kl9mr1935737obb.52.1364296462828; Tue, 26 Mar 2013 04:14:22 -0700 (PDT)
X-Originating-IP [59.66.130.213]
In-Reply-To <CAJQX3DxZz6J6Vsw5S+6s24EKSM3dwvBpZbnXHiVtaV4+=y0SMw@mail.gmail.com>
References <CAJQX3DyT53ocRcxW+1+xRWON2wzoAwEEkEO-zFTXBRm_v5KCOQ@mail.gmail.com> <51517370.4060305@davea.name> <CAJQX3DxZz6J6Vsw5S+6s24EKSM3dwvBpZbnXHiVtaV4+=y0SMw@mail.gmail.com>
Date Tue, 26 Mar 2013 19:14:22 +0800
Subject Re: At a loss on python scoping.
From Shiyao Ma <i@introo.me>
Cc python-list@python.org
Content-Type multipart/alternative; boundary=f46d0447a29f0916ae04d8d2067c
X-Gm-Message-State ALoCoQljgQa+T4XWJAsRsg5umMYKM4aIKVRJ9bwkq3pn/+L1aCChpkFNM3tXy/PTcd9LX1wIvqM7
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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.3734.1364296466.2939.python-list@python.org> (permalink)
Lines 156
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364296466 news.xs4all.nl 6927 [2001:888:2000:d::a6]:49938
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41880

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

After read Dave's answer, I think I confused LEGB with attribute lookup.
So, a.r has nothing to do with LEGB.

On Tue, Mar 26, 2013 at 7:03 PM, Shiyao Ma <i@introo.me> wrote:

> 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, 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<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
>
>


-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom

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


Thread

Re: At a loss on python scoping. Shiyao Ma <i@introo.me> - 2013-03-26 19:14 +0800

csiph-web