UnboundLocalError

Terry Reedy tjreedy at udel.edu
Thu Nov 9 14:22:13 EST 2006


"Camellia" <breakfastea at gmail.com> wrote in message 
news:1163083074.786927.184170 at i42g2000cwa.googlegroups.com...
> hi all
>
> why it generates an "UnboundLocalError" when I do the following:
>
> <code>
> ...

Presumably, the elided code includes a 'def number():' statement

> def main():
>    number = number()

Within a function, a given name can be either global or local, but not 
both.
Here you are expecting the compiler to interpret the first occurance of 
'number' as local and the second as global.  Humans can often resolve such 
ambiguities correctly, but not necessarily always.  So this is too much to 
ask of a program and hence it is not allowed.

As Benjamin said, use two different names for the two different objects 
that you need to use in the same context.

Terry Jan Reedy







More information about the Python-list mailing list