variable scope

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Mon Jul 31 10:47:16 EDT 2000


Cris A. Fugate wrote in comp.lang.python:
> Hi, Im new to Python and I have a question about variable scope.
> 
> It seems that any variable declared in a function is local.
> 
> Is there any way to define a global variable within a function?
> 
> For example, Tcl can use eval and uplevel to evaluate code
> 
> on any scope level.  In Scheme a local variable uses let,
> 
> otherwise it is global.

Please turn off sending posts in HTML in your newsreader. It makes your
posts look wrong, and far too long.

Okay. In Python, if you assign to a variable, it is a local by default.
If you only use it "read only", it is assumed to be global. You can 
declare a variable to be global with the "global" keyword, ie

def setx():
   global x
   x = 3
   
setx()
print x


(there was a plan for declaring a local variable that's never assigned to,
but the discussion got stuck at choosing the name)
-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
"This gubblick contains many nonsklarkish English flutzpahs, but the
 overall pluggandisp can be glorked from context"  (David Moser)



More information about the Python-list mailing list