Static vars in Python?

Courageous jkraska1 at san.rr.com
Sun May 7 15:13:43 EDT 2000


Juanjo Álvarez wrote:
> 
> Hi all!
> 
> It's posible to have in Python static vars (that is, vars in a function
> that retain his value over calls to the same function)?

In a way, all Class assigned variables start out this way,
but of course once they're assigned in a method on an instance,
the variables become bound to an instance. While I am new
at this, I believe that the way people achieve this is by
overriding the __getattr__ and __setattr__ methods.

Note that if you don't want to do this, you can put variables
in your module header as globals. While globals are "bad"
in other programming languages, they are okay in python,
because every global is really confined to a module. This
is sort of like file-static variables in C, only a bit
better.

In this way, you probably won't miss static class vars
very much.

C/



More information about the Python-list mailing list