[Tutor] Static variable in Python

alan.gauld@bt.com alan.gauld@bt.com
Thu, 15 Mar 2001 22:37:12 -0000


> Can you define a static field, which will be in stack all the 
> time, so all forked child process can use it.

Depends on exactly what you want to do. I fddon't think there 
is an exact equivalent to C's static keyword but you can fake 
it in various ways depending on exactly what you need to do.

For example default parameters are defined just once so 
setting a hidden parameter to some value can do it sometimes:

foo = input("give me a value")
myStaticThing = lambda x=foo : return x

Now anytime you call myStaticThing() you will get back 
the value that foo had when you defined it... A bit convoluted 
but its the best I've found. Dunno how that works across 
threads tho, I've not used Python threads yet!

Alan G.
PS Note that I haven't tried that code in the interpreter so 
it might need a tweak. I have used te principle tho...