Unbound Local error --???
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Jun 26 21:19:37 EDT 2007
En Tue, 26 Jun 2007 21:48:50 -0300, hari sirigibathina
<hari.siri74 at gmail.com> escribió:
> -Calling a method say aMethod() from another method where i mistakenly
> initialised a variable with name same as the called method like aMethod =
> 'string'. When i run this py script triggered "Unbound Local error".
> can any one explain me whats going here ? curios to learn from my
> mistake
> :)
A simple example:
>>> def f():
... x = a + 1
... a = 3
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
UnboundLocalError: local variable 'a' referenced before assignment
That means:
1) Python knows that variable "a" is local (because you assign 3 to it
later)
2) But you have not assigned anything to it yet.
>>> def g():
... x = a + 1
...
>>> g()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in g
NameError: global name 'a' is not defined
This time, "a" is not local, and not found in the global namespace either.
The message tells you that it was looking for a global variable, and could
not find it.
--
Gabriel Genellina
More information about the Python-list
mailing list