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

Ian Kelly ian.g.kelly at gmail.com
Sun Oct 14 21:27:24 EDT 2012


On Sun, Oct 14, 2012 at 7:08 PM, Cameron Simpson <cs at zip.com.au> wrote:
> On 14Oct2012 18:32, Ian Kelly <ian.g.kelly at 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.



More information about the Python-list mailing list