473,503 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1777
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
3723
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
1299
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
4561
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
5819
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
9
2803
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
2045
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
1554
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
1363
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
1621
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...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.