473,216 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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 1769
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hal Vaughan | last post by:
In the app I'm working on, there is a need, from all classes, to get configuration information. Much of that info is stored in single line files (which may be put in the Preferences structure in...
3
by: Lowen | last post by:
It's my understanding a static member variable will be one instance among all objects created from it, and static member functions only access static variables. When coding, I am still not sure how...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
9
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
8
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks....
6
by: PJ6 | last post by:
OK I now truly appreciate that I can have static information on the server, it is automatically preserved in any DLL static fields. It's great, I'm happy. Only it's a little too good - when I...
2
by: r.roest | last post by:
Hi, I'm looking for a way to fill static vars from a different c-file. Here is what I want to do: file1.c static DEBUG calling function X using DEBUG to display information calling...
7
by: Jon Slaughter | last post by:
I have some static fields in a class to keep track of "global" information but this information is local to each form that the class is used on. e.g., the class represents a base...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.