[triangle-zpug] static list in python

Josh Johnson josh_johnson at unc.edu
Fri Mar 14 17:03:01 CET 2008

I recall two meanings for static in programming. Static can mean a 
persistent value, like an internal counter in a list (I think that's 
what you want), but I've also heard the term mean "a class method that 
is accessed like a library instead of an instance method".

The latter is easy, you just put a function in a python script:

e.g. in mymodule.py:

def myfunction():

you can access it by:

 >>> import mymodule
 >>> mymodule.myfunction()

I'm not sure if python supports the sort of persistent variable I think 
you want (anybody else know?). If not, you may have to start with a list 
and have that list be returned by your function and then subsequently 
passed to it again:

 >>> def myfunc(l):
...    l.append('1')
...    return l
 >>> myl = []
 >>> myl = myfunc(myl)
 >>> myl = myfunc(myl)
 >>> myl
['1', '1']


Joseph Mack NA3T wrote:
> I'm trying to figure out the python equivalent of having a 
> static list in a function. I could have the list global, but 
> I assume that's no more kosher in python than anywhere 
> else.
> I have found how to make a module static
> @staticmethod
> def aStaticMethod(x, y, z):
> but I don't know if this is the solution or not yet (I don't 
> think I've ever had a static function). I don't need the 
> function to be static, just a variable in the function.
> One posting said that static variables aren't needed in 
> python, that I should use module level functions, but I 
> haven't found anything helpful on doing that.
> Any suggestions?
> Thanks Joe

More information about the TriZPUG mailing list