are free variables part of the local dict ?
Cesar Douady
cesar.douady at asim.lip6.fr
Fri Jan 18 04:38:56 EST 2002
In article <3C4372A4.77AD176F at gol.ge>, "Giorgi Lekishvili" <gleki at gol.ge>
wrote:
> Hi!
> What you'd say about this?
>>>> def foo(i):
> global x
> x=1
> def bar(i):
> x=x+i
> print locals()
> bar(i)
>
> Or, have I undestood you in a wrong manner?
>
> Grtz,
> Giorgi
>
>
>>>> foo(2)
> Traceback (innermost last):
> File "<pyshell#22>", line 1, in ?
> foo(2)
> File "<pyshell#21>", line 7, in foo
> bar(i)
> File "<pyshell#21>", line 5, in bar
> x=x+i
> UnboundLocalError: Local variable 'x' referenced before assignment
Where is your problem ? because x is assigned in bar, x is a local and
you reference it before assigning to it.
This, by the way, implies that there is no way (that I know of, I'd be
glad to be contradicted) to update a free variable.
>>>> def foo(i):
> global x
> x=1
> def bar(i):
> x
> x=x+i
> print locals()
> bar(i)
>
>
>>>> foo(2)
> Traceback (innermost last):
> File "<pyshell#30>", line 1, in ?
> foo(2)
> File "<pyshell#29>", line 8, in foo
> bar(i)
> File "<pyshell#29>", line 5, in bar
> x
> UnboundLocalError: Local variable 'x' referenced before assignment
>>>>
idem.
>>>>
>
More information about the Python-list
mailing list