UnboundLocalError

Fredrik Lundh fredrik at pythonware.com
Sat Nov 11 09:19:14 CET 2006


Camellia wrote:

> Oh how can I thank you enough, you make my day:)
> According to what you said I finally figure it out, it is the same as:
> 
> <code>
> b = 1
> def a():
>     b = b #no good:)
> </code>

if you really want to modify a variable that lives outside the function, 
you can use the "global" directive:

http://effbot.org/pyfaq/how-do-you-set-a-global-variable-in-a-function.htm

 > So in every day programming I should avoid using the same name for
 > different types of objects because they will step on each other,
 > right?

if you want to distinguish between things, giving them distinct names is 
always a good idea.

</F>




More information about the Python-list mailing list