Static Variables in Python?

John Salerno johnjsal at NOSPAMgmail.com
Mon Jul 31 16:05:38 EDT 2006


bearophileHUGS at lycos.com wrote:
> tac-tics:
>> If you declare bits in set_bit() as "global bits = ...", it will create
>> it as a global variable without you having to declare it outside of the
>> function. Just be careful about name conflicts.
> 
> Are you sure?
> 
> def fun():
>     global x = 10
> fun()
> print x
> 
> Bye,
> bearophile
> 

This works for me:

 >>> def fun():
	global x
	x = 10

	
 >>> fun()
 >>> print x
10
 >>>

But of course:

 >>> def fun():
	global x = 10
	
SyntaxError: invalid syntax
 >>>



More information about the Python-list mailing list