static keyword
Daniel Dittmar
daniel.dittmar at sap.com
Thu Apr 29 11:41:37 EDT 2004
Nick Jacobson wrote:
> I believe the following "static" command would be useful in Python.
[...]
> Just like in C, the variables i and firstcall are only assigned the
> first time foo() is called. To get this effect currently, one could
> use default arguments or wrapping the whole thing in a class. Both of
> these solutions seem like hacks, the above method IMO is more
> Pythonic. :)
You could also use funtion properties
>>> def statictest ():
... statictest.counter += 1
... print statictest.counter
...
>>> statictest.counter = 0
>>> statictest ()
1
>>> statictest ()
2
But this uses two lookups: one for the function in module scope and one for
the property. Three if you want to do this in a method
(class.method.property).
Just to describe all the possibilities, I probably wouldn't use this myself.
Daniel
More information about the Python-list
mailing list