470,636 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

static information...

I've been looking for a way of have static variables in a python class and all I
have found is this:

class A:
static =[]

def f(self):
A.static = [1,2,3]
That seems to be pretty static since that variable can be used either on the
class (such as A.static) or on an instance (such as A().static).
Now my problem is that every time some create a new instance of this class the
variable static is reset to its original value [] and the real value that it
took is lost. What I want is someplace to store class's information and not
instance's information.
Any idea?
Jul 18 '05 #1
2 1693
Try A.static.extend([1,2,3]) and see what that
give you.

Larry Bates

<ni**********@cimex.com.cu> wrote in message
news:ma************************************@python .org...
I've been looking for a way of have static variables in a python class and all I have found is this:

class A:
static =[]

def f(self):
A.static = [1,2,3]
That seems to be pretty static since that variable can be used either on the class (such as A.static) or on an instance (such as A().static).
Now my problem is that every time some create a new instance of this class the variable static is reset to its original value [] and the real value that it took is lost. What I want is someplace to store class's information and not instance's information.
Any idea?

Jul 18 '05 #2
<ni**********@cimex.com.cu> wrote in message
news:ma************************************@python .org...
I've been looking for a way of have static variables in a python class and all I have found is this:

class A:
static =[]

def f(self):
A.static = [1,2,3]
That seems to be pretty static since that variable can be used either on the class (such as A.static) or on an instance (such as A().static).
Now my problem is that every time some create a new instance of this class the variable static is reset to its original value [] and the real value that it took is lost. What I want is someplace to store class's information and not instance's information.
Any idea?
It doesn't exist. The class level variable is a default for
an instance level variable. If you want a class level
variable, there are two ways of going about it.

One is to declare a getter and setter and make them
class methods; the other is the one you have discovered,
and that Larry Bates references.

for example:

class Foo(object):
var = "bar"

def setVar(klas, value):
klas.var = value
setVar = classmethod(setVar)

def getVar(klas):
return klas.var
getVar = classmethod(getVar)

def bar(self):
self.setVar([1,2,3])

-------------------------------

Note that classmethod requires a new style
class, which is why the subclass from object.

HTH

John Roth

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Hal Vaughan | last post: by
3 posts views Thread by Lowen | last post: by
8 posts views Thread by Scott J. McCaughrin | last post: by
6 posts views Thread by Dumitru Sipos | last post: by
9 posts views Thread by AnandRaj | last post: by
8 posts views Thread by Fernando Lopes | last post: by
6 posts views Thread by PJ6 | last post: by
2 posts views Thread by r.roest | last post: by
7 posts views Thread by Jon Slaughter | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.