C-style static variables in Python?

Patrick Maupin pmaupin at gmail.com
Thu Apr 1 19:30:51 EDT 2010


On Apr 1, 6:10 pm, Steve Holden <st... at holdenweb.com> wrote:
> Chris Rebert wrote:
> > Personally, I hate such abuse with a passion; I think a global
> > variable is clearest.
>
> But the real problem is that the OP is insisting on using purely
> procedural Python when the problem is screaming for an object-oriented
> answer.
>
> If the function were instead a method then the instance namespace would
> be the logical place to store the required data.

In some situations I will use either the default parameter
initialization Chris mentioned, or the closure mechanism that the OP
presented, but only on code that I am optimizing for speed (local
variable lookups, even in nested functions, being much faster than
global or instance lookups).  If it doesn't need to go fast, globals
or instance variables are the way to go.

Pat



More information about the Python-list mailing list