6

There is this code:

class Sample:
    variable = 2
object1 = Sample()
object2 = Sample()
print object1.variable  # 2
print object2.variable  # 2
object1.variable = 1
print object1.variable  # 1
print object2.variable  # 2 <- why not 1 too?

Why object2.variable is not also 1 after assignment to class variable?

scdmb
  • 15,091
  • 21
  • 85
  • 128
  • 3
    possible duplicate of [python class attribute](http://stackoverflow.com/questions/2923579/python-class-attribute) –  Dec 31 '11 at 12:23
  • For my understanding im asking this only: what was the thinking behind the question? Isn't it obvious that object1 & object2 are distinct instances of the same class? – Zain Khan Dec 31 '11 at 12:23
  • 1
    I think he believes that `variable` is static because it's defined outside of a function, or something like this. Not sure really :) Still a reasonable question somehow. – hochl Dec 31 '11 at 12:28
  • 1
    @hochl: +1 for the "somehow" :D – Zain Khan Dec 31 '11 at 12:30
  • it is reassigned. Use a mutable datastructure (like a dictionary) to get the expected behaviour. – Arne Babenhauserheide Apr 14 '13 at 15:47

7 Answers7

5

Don't confuse this with a static variable. In your case, both objects have a name variable point to a 2 object after instantiation. Now if you change the variable to 1 in one object, all you do is bind the name to a different object, i.e. the 1 object. The name in object2 still refers to a 2 object. The original class object is untouched and still has the name variable point to a 2, so object3 = Sample() will have a 2 bound to variable as well.

One way to get around this is to write the class like the following:

>>> class Sample:
...     variable=[2]
... 
>>> object1 = Sample()
>>> object2 = Sample()
>>> print object1.variable[0]; print object2.variable[0]
2
2
>>> object1.variable[0]=1
>>> print object1.variable[0]; print object2.variable[0]
1
1

This is because all classes bind the name variable to the same, muatable object, and you manipulate the contents of this same object (variable[0]).

hochl
  • 12,524
  • 10
  • 53
  • 87
4

Because you assign new instance variable into instance, not class variable. If you can change class attribute, then you must set variable through __class__ attribute. (Or directly to class object.)

class Sample:
    variable = 2
object1 = Sample()
object2 = Sample()
print object1.variable  # 2
print object2.variable  # 2
object1.__class__.variable = 1 # or Sample.variable = 1
print object1.variable  # 1
print object2.variable  # 1
horejsek
  • 1,378
  • 14
  • 8
2

Take a look at this question.

In a nutshell, your class and your class instance have different namespaces (Actually, this is not very correct because instance namespace kinda places on top of class' namespace so that in case if you have a class atribute and an instance attribute with the same name, instance' one will be accessed through inst.attr , otherways - class' one). So, when you do

object1.variable = 1

you do not actually change class variable (which is often called static field in other languages), you just bind a new field called variable to your class instance object1.

What you probably want to do is to change class variable, not instance one. It could be done like this:

Sample.variable = 1

or like this:

object1.__class__.variable = 1
Community
  • 1
  • 1
ikostia
  • 7,395
  • 6
  • 30
  • 39
1

Two instances of a same class don't share the same memory and the same variables. For instance, you can have a Car whose color is red and another whose color is blue.

Cydonia7
  • 3,744
  • 2
  • 23
  • 32
1

You've answered the question in the title. They are two separate and distinct instances of the same class. object1 and object2 are different objects and therefore anything that happens to one of these objects will not alter the other.

Ben
  • 51,770
  • 36
  • 127
  • 149
1

Because object1 is a different object than object2. To have them be the same, you need to do

class Sample:
  variable = 2
object1 = Sample()
object2 = object1
print object1.variable
print object2.variable
object1.variable = 1
print object1.variable
print object2.variable

Note the line that says

object2 = object1
CNeo
  • 736
  • 1
  • 6
  • 10
1

This is perhaps counterintuitive, but when you write

object1.variable = 1

You're not assigning to the class variable, but defining an instance variable of the same name. (You can inspect object1's __dict__ to verify this). Notice that if you write

Sample.variable

or

object1.__class__.variable

You'll get 2 in both cases, because variable was never actually changed. To assign to it, you can refer to it as above -- object1.__class__.variable = 1, for instance, makes it so that object2.variable == 1 also.

Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97