Name valid outside function, but error name inside function

Paul Prescod paul at prescod.net
Fri Jul 21 07:30:22 EDT 2000


"Fernando Rodríguez" wrote:
> 
>...
> This raises a name error:
> def func2():
>     A = A + 'hi'    # this line causes the error
>     print A

Does it really raise a NameError? Or an UnboundLocalError?

You probably want

def func2():
     global A
     A = A + 'hi'    # this line causes the error
     print A

You probably know that "from X import *" is considered bad style, BTW.
Also, I'm not sure if the code I wrote will do what you want it to.
Assigning to A in one module rebinds it in that module. I don't think it
will have any effect in the other module.

-- 
 Paul Prescod - Not encumbered by corporate consensus
"Hardly anything more unwelcome can befall a scientific writer than 
having the foundations of his edifice shaken after the work is 
finished.  I have been placed in this position by a letter from 
Mr. Bertrand Russell..." 
 - Frege, Appendix of Basic Laws of Arithmetic (of Russell's Paradox)




More information about the Python-list mailing list