Newbie asks about static variables...

Greg Ewing greg.ewing at compaq.com
Wed May 5 18:41:59 EDT 1999


Emile van Sebille wrote:
> 
> In other words, is there any reason to avoid using this construct knowing
> that this is how it works?  Or, is there reason to think that it may be
> changed to behave differently in some future release?

Using a mutable default argument as a static variable works
perfectly well and is unlikely to change. But it's only
going to confuse anyone reading or modifying the program.
People expect arguments to be used as arguments, not
local variables!

Using an object is much clearer. This is what objects were
designed for in the first place - encapsulating state and
behaviour - and the reader of your program will understand
instantly how it is supposed to work.

Greg




More information about the Python-list mailing list