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


Groups > comp.lang.python > #52653

Re: Local variable in a closure

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'variables': 0.07; 'happens.': 0.09; 'span': 0.09; 'testing,': 0.09; 'elsewhere.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'globals.': 0.16; 'interpreter,': 0.16; 'retrieving': 0.16; 'subject:variable': 0.16; 'usage,': 0.16; 'variables,': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'aug': 0.22; 'updating': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'appear': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'easier': 0.31; 'question:': 0.31; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'level': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'most': 0.60; 'effective': 0.61; 'simply': 0.61; 'kind': 0.63; 'happen': 0.63; 'different': 0.65; "it'd": 0.84; 'short,': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=oeLRmZZosqXb5j1yvwfSt6aVP7CloA71eYprheLJOcY=; b=XjerfD3vcScA04Mof13NNwGZYwnviiLhBWqj/vdZhb4a8+y55HPoLJ0Pp4IqicS1zm sB1J9NcDt4JT4RCSsEpjMr5FHmAtaOxr+2brbbuKPMpoqg3ECH6nbwCmYypH4/Q3D0yH t+YVVRhYIIhqw/C0IG5X7zhZAMZIorQtLl1WZigH/m1jY55MY4MmRBayoUzvcVYhDo2D ipX8xhFx1s5c2SsmKSuD9DsGFcVwbASE657rcwEUEr/buyWpXHJ36T9//pow9/rV/fSl eziLIFXUp7cHHsox0q3UKsYStxSX6GpiD6iX58qjUFv7ClNAkraE8LoH07wywkxEyo8Z Oruw==
MIME-Version 1.0
X-Received by 10.221.36.4 with SMTP id sy4mr7820475vcb.11.1376821720449; Sun, 18 Aug 2013 03:28:40 -0700 (PDT)
In-Reply-To <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com>
References <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com>
Date Sun, 18 Aug 2013 11:28:40 +0100
Subject Re: Local variable in a closure
From Chris Angelico <rosuav@gmail.com>
To 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 <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.24.1376821729.23369.python-list@python.org> (permalink)
Lines 18
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376821729 news.xs4all.nl 15872 [2001:888:2000:d::a6]:56536
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52653

Show key headers only | View raw


On Sun, Aug 18, 2013 at 10:41 AM,  <w.w.milner@googlemail.com> wrote:
> Is f local or not?
> http://pastebin.com/AKDJrbDs

With something that short, it'd be easier to simply paste it straight
into your post, rather than having it off elsewhere. But to answer
your question: It is its own kind of beast. You can play around with
the dis.dis() function (start with "import dis", which is not just
"import this" with an accent) in the interactive interpreter, as an
effective way of finding out what actually happens. In my testing, the
opcodes for retrieving and updating 'f' are LOAD_DEREF and
STORE_DEREF, different from LOAD_FAST/STORE_FAST as used for locals,
and LOAD_GLOBAL/STORE_GLOBAL for globals. In normal usage, nonlocal
variables are most like local variables, but they happen to span one
level of function nesting. So they're still basically locals, hence
they appear in locals(). At least, that's my understanding of it.

ChrisA

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


Thread

Local variable in a closure w.w.milner@googlemail.com - 2013-08-18 02:41 -0700
  Re: Local variable in a closure Chris Angelico <rosuav@gmail.com> - 2013-08-18 11:28 +0100
  Re: Local variable in a closure Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-18 04:40 -0600
  Re: Local variable in a closure Dave Angel <davea@davea.name> - 2013-08-18 10:44 +0000
  Re: Local variable in a closure Terry Reedy <tjreedy@udel.edu> - 2013-08-18 16:42 -0400

csiph-web