Unbound Local error --???

Gabriel Genellina gabrielg_laburando at yahoo.com.ar
Wed Jul 4 03:27:02 EDT 2007

(Please keep posting on the list - I don't read this
account too often)

At Friday 29/06/2007 22:03, you wrote:

> But what i am asking in particular is, how python
> interpretes when we initialize a variable with a 
> name same as a method name ? for ex:
> def f(): 
> ... x = g()
> ... g = 'sample'

It's the same thing:
- First, the *compiler* determines (statically)
whether a variable name is local or not. If you assign
something to it, it IS local (unless you explicitely
use the global keyword). This is done when the code is
compiled, not when it is run.
- In your example, you assign something to g, so the
compiler knows that g is a local variable.
- When the function is run, there is an attempt to
reference a local variable g, but is still unassigned,
so you get an UnboundLocalError.

Python treats function names and variable names like
the same thing. That is, all names are just that...
names - pointing to objects.

Gabriel Genellina
Softlab SRL 

Todo sobre la Copa América.
Mantenete actualizado con las últimas noticias sobre esta competencia en Yahoo! Deportes. http://ar.sports.yahoo.com/futbol/copaamerica/index.html

More information about the Python-list mailing list