Re: strange unbound local error?
- From: Pablo Ziliani <pablo@xxxxxxxxxxxxx>
- Date: Sat, 29 Sep 2007 15:18:23 -0300
righes@xxxxxxxx wrote:
spam = 42
def eggs():
print spam
spam = spam + 1
if __name__=="__main__":
eggs()
This thows an UnboundLocalError at line 4 (print statement). But if I
comment out line 5 (variable assignment), no error occurs.
Can you explain me this, please?
Hi Enrico,
You need to say that you will be modifying the variable in the global scope, like this:
spam = 42
def eggs():
global spam # <--here
print spam
spam = spam + 1
if __name__=="__main__":
eggs()
This can help you:
http://www.pasteur.fr/recherche/unites/sis/formation/python/ch04.html
Pablo
.
- References:
- strange unbound local error?
- From: righes
- strange unbound local error?
- Prev by Date: Re: Program inefficiency?
- Next by Date: Re: strange unbound local error?
- Previous by thread: strange unbound local error?
- Next by thread: Re: strange unbound local error?
- Index(es):
Relevant Pages
|