Which of the following should be used to create a variable that is shared by all instances of a class?

I started coding in python a week ago, it is my mistake i started coding using oops,classes and objects that soon. I assumed my C++ proficiency will help.... I got bit by the following code

class A: var=0 list=[] def __init__(self): pass

Here to my surprise, var and list are kinda global variable, it is shared across all instances it seems.... What I thought was it was different across all the instances..... It took me half a day to figure out that.... It does not make even slightest sense, that a variable can be accessed by a class object only, but is shared across all instances....... Just Curious, is there a reason behind it?????

Ken White

122k14 gold badges215 silver badges430 bronze badges

asked Jun 14, 2012 at 19:56

2

var should definitely not be shared as long as you access it by instance.var or self.var. With the list however, what your statement does is when the class gets evaluated, one list instance is created and bound to the class dict, hence all instances will have the same list. Whenever you set instance.list = somethingelse resp. self.list = somethingelse, it should get an instance level value.

Example time:

>>> class A(): ... var = 0 ... list = [] ... >>> a = A() >>> b = A() >>> a.var 0 >>> a.list [] >>> b.var 0 >>> b.list [] >>> a.var = 1 >>> b.var 0 >>> a.list.append('hello') >>> b.list ['hello'] >>> b.list = ['newlist'] >>> a.list ['hello'] >>> b.list ['newlist']

answered Jun 14, 2012 at 20:01

4

These are basically like static variables in Java:

// Example equivalent Java class A { static int var = 0; static String[] list; }

This is the intended behavior: Class variables are for the class.

For normal instance variables, declare them in the constructor:

class A: def __init__(self): self.var = 0 self.list = []

You may want to look at Static class variables in Python.

answered Jun 14, 2012 at 19:59

Brendan LongBrendan Long

51.9k21 gold badges142 silver badges181 bronze badges

4

The reason is that in Python class is an executable statement that is executed like any other. The body of the class definition is executed once, when the class is defined. If you have a line line var = [] in there, then it is only executed once, so only one list can ever be created as a result of that line.

answered Jun 14, 2012 at 20:03

BrenBarnBrenBarn

234k35 gold badges397 silver badges375 bronze badges

Note that you do not see this behaviour for var, you only see it for list:

>>> class A: ... var = 0 ... list = [] ... >>> a1 = A() >>> a2 = A() >>> a1.var = 3 >>> a2.var 0

answered Jun 14, 2012 at 20:00

Simeon VisserSimeon Visser

116k18 gold badges178 silver badges178 bronze badges

3

What variables that are shared by every instances of a class are?

A static variable is shared by all instances of a class. Only one variable created for the class.

What kind of a variable do you use if you need to share a variable from one instance of a class to the next?

You need to make the variables in class aaa as class variables, and then you can use these variables of class aaa in class bbb by using object of class aaa. e.g. aaa obj2=new aaa(); System.

Which variable is shared by all objects?

Class variables are shared in the sense that they are accessed by all objects (instances) of that class. There is only copy of the class variable and when any one object makes a change to a class variable, the change is reflected in all the other instances as well.

Which methods are shared among all the object of the class?

On a side note, static data members are shared among objects. All objects share codes of all methods.

Toplist

Neuester Beitrag

Stichworte