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


Groups > comp.lang.python > #31272

Re: trouble with nested closures: one of my variables is missing...

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(python': 0.05; 'modify': 0.05; 'none:': 0.05; 'that?': 0.05; '(using': 0.07; 'parameter': 0.07; 'variable,': 0.07; 'works.': 0.07; 'python': 0.09; 'assigning': 0.09; 'def': 0.10; 'yet.': 0.13; 'did,': 0.16; 'distinct': 0.16; 'list),': 0.16; 'oct': 0.16; 'simpson': 0.16; 'variable.': 0.16; 'wrote:': 0.17; 'variable': 0.20; 'skip:" 30': 0.20; 'bit': 0.21; '3.x': 0.22; 'assignment': 0.22; 'explicit': 0.22; 'seems': 0.23; 'somewhere': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:" 20': 0.26; 'skip:m 30': 0.26; '(e.g.': 0.27; 'right.': 0.27; 'i.e.': 0.27; 'skip:> 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'container': 0.29; 'omitted': 0.29; 'yes.': 0.29; 'case,': 0.29; 'function': 0.30; 'code': 0.31; 'point': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'path': 0.35; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'subject:with': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'skip:u 10': 0.60; 'subject:...': 0.63; 'locals': 0.84; 'to:name:python': 0.84
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=5NK3ycT83Lt3VKXchHoS2QIBWq6Vuh9QUStJ+K4x1NU=; b=NyH+wGOxtSdVC0713Co2pMScqbVbK57jszFPPiYBd2fI5Mlgep4pHQmxbcHO1b+w5t z86s5VO8aPauwr9IJTJMD1tqXjsnVHZN9CsB8azQ8G0s5EP4ttn7c9lFXiZ1KzSYZNTV x4KdgTfIgmoLcgw979ihyaIhe7+Spz+VH7z7iuBUErfFuMuOXL2cFn1UIzRsMEfUlEh2 SHe42FugNsYc+ALXhe2YdD9S7W1LnCqHJmUrnKAaFZ37L2CClVkasowlkF7PHO7OScfh /0YcdmHUFrazoR8PSJa7x9n0UmqOtIQuk3lIev+mEcU2Od2qyFE2doV38JCGz9tPx/b3 8iEg==
MIME-Version 1.0
In-Reply-To <20121015010840.GA2907@cskk.homeip.net>
References <CALwzidnuEh9t=vBY_oPn=K+9UGbJ15ReO1Mh9G+WnUaV7SrO5w@mail.gmail.com> <20121015010840.GA2907@cskk.homeip.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 14 Oct 2012 19:27:24 -0600
Subject Re: trouble with nested closures: one of my variables is missing...
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 <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.2189.1350264477.27098.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350264477 news.xs4all.nl 6952 [2001:888:2000:d::a6]:40602
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31272

Show key headers only | View raw


On Sun, Oct 14, 2012 at 7:08 PM, Cameron Simpson <cs@zip.com.au> wrote:
> On 14Oct2012 18:32, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> | 'attr_name' is not in locals because while it's a local variable, it
> | has not been assigned to yet.  It has no value and an attempt to
> | reference it at that point would result in an UnboundLocalError.
>
> Can you elaborate a bit on that? The only place in my code that
> unset_object is set is as a default parameter in make_file_property
> (snippet):
>
>   def make_file_property(attr_name=None, unset_object=None, poll_rate=1):
>     print >>sys.stderr, "make_file_property(attr_name=%r, unset_object=%r, poll_rate=%r): locals()=%r" % (attr_name, unset_object, poll_rate,locals())
>     def made_file_property(func):
>       print >>sys.stderr, "made_file_property(func=%r): locals()=%r" % (func, locals())
>       if attr_name is None:
>         attr_name = '_' + func.__name__
>
> and attr_name is set there also.
>
> Is attr_name omitted from locals() in made_file_property _because_ I
> have an assignment statement?

Yes.  Syntactically, a variable is treated as local to a function if
it is assigned to somewhere in that function and there is no explicit
global or nonlocal declaration.

> If that's the case, should I be doing this (using distinct names for the
> closure variable and the function local variable):
>
>   def make_file_property(attr_name=None, unset_object=None, poll_rate=1):
>     print >>sys.stderr, "make_file_property(attr_name=%r, unset_object=%r, poll_rate=%r): locals()=%r" % (attr_name, unset_object, poll_rate,locals())
>     def made_file_property(func):
>       print >>sys.stderr, "made_file_property(func=%r): locals()=%r" % (func, locals())
>       if attr_name is None:
>         my_attr_name = '_' + func.__name__
>       else:
>         my_attr_name = attr_name
>       lock_name = my_attr_name + '_lock'
>       def getprop(self):
>         with getattr(self, lock_name):
>           pass
>         return getattr(self, my_attr_name, unset_object)
>
> i.e. deliberately _not_ assigning to attr_name as as to _avoid_ masking
> the outer attr_name from the inner locals()?
>
> BTW, doing that works. Is that The True Path for this situation?

That's a perfectly good way to do it as long as you don't want to
actually change the value of the outer attr_name.  If you did, then
you would either declare the variable as nonlocal (Python 3.x only) or
use a container (e.g. a 1-element list), which would allow you to
modify the contents of the container without actually assigning to the
variable.

> If so, I think I now understand what's going on: Python has inspected
> the inner function and not placed the outer 'attr_name' into locals()
> _because_ the inner function seems to have its own local attr_name
> in use, which should not be pre-tromped.

Exactly right.

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


Thread

Re: trouble with nested closures: one of my variables is missing... Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-14 19:27 -0600

csiph-web