scope of function parameters
Chris Angelico
rosuav at gmail.com
Sun May 29 14:38:26 EDT 2011
On Mon, May 30, 2011 at 4:01 AM, Chris Rebert <clp2 at rebertia.com> wrote:
> def foo():
> print bar
> bar = 42
>
> foo()
>
> ===>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 2, in foo
> UnboundLocalError: local variable 'bar' referenced before assignment
Wow
I thought it basically functioned top-down. You get a different error
on the print line if there's a "bar = 42" *after* it. This could make
debugging quite confusing.
Guess it's just one of the consequences of eschewing variable
declarations. Sure it's easier, but there's complications down the
road.
Chris Angelico
More information about the Python-list
mailing list