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?

asked Dec 31, 2011 at 12:15
5
  • 3
    possible duplicate of python class attribute Commented Dec 31, 2011 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? Commented Dec 31, 2011 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. Commented Dec 31, 2011 at 12:28
  • 1
    @hochl: +1 for the "somehow" :D Commented Dec 31, 2011 at 12:30
  • it is reassigned. Use a mutable datastructure (like a dictionary) to get the expected behaviour. Commented Apr 14, 2013 at 15:47

7 Answers 7

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]).

answered Dec 31, 2011 at 12:19
Sign up to request clarification or add additional context in comments.

Comments

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
answered Dec 31, 2011 at 12:35

Comments

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
answered Dec 31, 2011 at 12:29

Comments

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.

answered Dec 31, 2011 at 12:18

Comments

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.

answered Dec 31, 2011 at 12:19

1 Comment

that’s not quite true (though valid for simple attribute assignments). As soon as you have mutable data structures, the instances are connected.
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
answered Dec 31, 2011 at 12:19

Comments

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.

answered Dec 31, 2011 at 12:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.