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


Groups > comp.lang.python > #52653

Re: Local variable in a closure

References <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com>
Date 2013-08-18 11:28 +0100
Subject Re: Local variable in a closure
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.24.1376821729.23369.python-list@python.org> (permalink)

Show all headers | 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