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


Groups > comp.lang.python > #69677

Re: Scoping rules for class definitions

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.049
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; 'referenced': 0.09; 'def': 0.12; 'closures,': 0.16; 'grasp': 0.16; 'scope,': 0.16; 'subject:class': 0.16; 'all.': 0.16; 'wrote:': 0.18; "python's": 0.19; '>>>': 0.22; 'comparing': 0.24; 'second': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; '(since': 0.31; '>>>>': 0.31; 'loading': 0.31; 'loads': 0.31; 'anyone': 0.31; 'class': 0.32; 'fri,': 0.33; 'guess': 0.33; 'noticed': 0.34; 'knows': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'name': 0.63; 'today': 0.64; "'local'": 0.84; 'whereas': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=7yCEkzvH/o64GFSu0kjei6WYl8R8uMEL6eT/YKTIkJo=; b=Bobc6ujlWA4KjgXKOX+bOwyBvXCLxboD5O3BH/9YsMLCzVrFsktQFalH6qXv3V767/ nM12LrLVxztBH2ACXY+SLl0vjgOem0TQTxonfXFM83w1d6cynHg7+WDNYrcnKSzGDFDz M9/Hz2FbgF4+fwfd+P27ccewqhVcPy/0A2tk9tznkAbo+aZkJpIUMmE+Lu9vjkCWmzDr zFVATAwSf91YE7Y3E/31RbGbDB1nKqRbOzDHBVQPTj4HPFRZ4lT6gvegeTD6FC4nSFbO frAAS0nespMyFCy2MEWki3kpC1fDO5O8Hd/K5fP8J29ZWbJ15wai6bvlQHeFOYFhgWcc 5sZg==
X-Received by 10.66.146.105 with SMTP id tb9mr16550500pab.157.1396637748303; Fri, 04 Apr 2014 11:55:48 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <lhmu69$1nu$1@dont-email.me>
References <lhmu69$1nu$1@dont-email.me>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 4 Apr 2014 12:55:07 -0600
Subject Re: Scoping rules for class definitions
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8898.1396637751.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396637751 news.xs4all.nl 2907 [2001:888:2000:d::a6]:43451
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69677

Show key headers only | View raw


On Fri, Apr 4, 2014 at 12:37 PM, Rotwang <sg552@hotmail.co.uk> wrote:
> Hi all. I thought I had a pretty good grasp of Python's scoping rules, but
> today I noticed something that I don't understand. Can anyone explain to me
> why this happens?
>
>>>> x = 'global'
>>>> def f1():
>     x = 'local'
>     class C:
>         y = x
>     return C.y
>
>>>> def f2():
>     x = 'local'
>     class C:
>         x = x
>     return C.x
>
>>>> f1()
> 'local'
>>>> f2()
> 'global'

Start by comparing the disassembly of the two class bodies:

>>> dis.dis(f1.__code__.co_consts[2])
  3           0 LOAD_NAME                0 (__name__)
              3 STORE_NAME               1 (__module__)
              6 LOAD_CONST               0 ('f1.<locals>.C')
              9 STORE_NAME               2 (__qualname__)

  4          12 LOAD_CLASSDEREF          0 (x)
             15 STORE_NAME               3 (y)
             18 LOAD_CONST               1 (None)
             21 RETURN_VALUE
>>> dis.dis(f2.__code__.co_consts[2])
  3           0 LOAD_NAME                0 (__name__)
              3 STORE_NAME               1 (__module__)
              6 LOAD_CONST               0 ('f2.<locals>.C')
              9 STORE_NAME               2 (__qualname__)

  4          12 LOAD_NAME                3 (x)
             15 STORE_NAME               3 (x)
             18 LOAD_CONST               1 (None)
             21 RETURN_VALUE

The only significant difference is that the first uses
LOAD_CLASSDEREF, which I guess is the class version of LOAD_DEREF for
loading values from closures, at line 4 whereas the second uses
LOAD_NAME.  So the first one knows about the x in the nonlocal scope,
whereas the second does not and just loads the global (since x doesn't
yet exist in the locals dict).

Now why doesn't the second version also use LOAD_CLASSDEREF?  My guess
is because it's the name of a local; if it were referenced a second
time in the class then the second LOAD_CLASSDEREF would again get the
x from the nonlocal scope, which would be incorrect.

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


Thread

Scoping rules for class definitions Rotwang <sg552@hotmail.co.uk> - 2014-04-04 19:37 +0100
  Re: Scoping rules for class definitions Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-04 12:55 -0600
    Re: Scoping rules for class definitions Rotwang <sg552@hotmail.co.uk> - 2014-04-08 23:09 +0100

csiph-web