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

Ian Kelly ian.g.kelly at gmail.com
Sun Oct 14 20:32:01 EDT 2012


On Sun, Oct 14, 2012 at 3:54 PM, Cameron Simpson <cs at zip.com.au> wrote:
> |  You assign to it, but there's no nonlocal declaration, so Python thinks
> | it's a local var, hence your error.
>
> But 'unset_object' is in locals(). Why one and not the other?
> Obviously there's something about closures here I'm missing.

'unset_object' is in locals because it's a free variable and those are
included in locals(), and it has a value.
'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.



More information about the Python-list mailing list