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

References <CALwzidnuEh9t=vBY_oPn=K+9UGbJ15ReO1Mh9G+WnUaV7SrO5w@mail.gmail.com> <20121015010840.GA2907@cskk.homeip.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-10-14 19:27 -0600
Subject Re: trouble with nested closures: one of my variables is missing...
Newsgroups comp.lang.python
Message-ID <mailman.2189.1350264477.27098.python-list@python.org> (permalink)

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