[triangle-zpug] static list in python
Bradley A. Crittenden
brad.crittenden at gmail.com
Fri Mar 14 17:18:21 UTC 2008
On Mar 14, 2008, at 11:20 , 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
> I have found how to make a module static
> 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?
from your description it sounds like you're using python without
taking advantage of any object oriented features. you can do that,
but you'll end up scratching your head about how to do 'obvious' things.
how about just making a class and setting the list as an instance
variable? or it could be a class variable.
here's a quick example. 'attendees' is an instance variable while
'count' is a class variable. you'll see the attendee set is unique
per instance while the count has a running total.
hope this helps and i haven't misunderstood what you're trying to do.
>>> class Sprint:
... count = 0
... def __init__(self):
... self.attendees = set()
... def add(self, name):
... Sprint.count += 1
... def remove(self, name):
... Sprint.count -= 1
... except KeyError:
... def show(self):
... print Sprint.count
... for attendee in sorted(self.attendees):
... print attendee
>>> s = Sprint()
>>> s2 = Sprint()
>>> s2.add('mr. spacely')
More information about the triangle-zpug