Class attribute for two instances of the same class

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 is object2.variable not equal to 1 after assigning a class variable?

+6
source share
7 answers

Do not confuse this with a static variable. In your case, both objects have the name variable point to object 2 after creating the instance. Now, if you change the variable to 1 in one object, all you do is bind name to another object, i.e. To object 1 . The name in object2 still refers to object 2 . The original class object is untouched and still has a variable name pointing to 2 , so object3 = Sample() will have 2 bound to variable .

One way around this is to write a class as follows:

 >>> 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 associate the variable name with the same muatable object, and you control the contents of the same object ( variable[0] ).

+4
source

Because you are assigning a new instance variable to the instance, not a class variable. If you can change the class attribute, then you must set the variable via the __class__ attribute. (Or directly to the 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 
+4
source

Look at the question .

In short, your class and your class instance have different namespaces (in fact, this is not very correct, because the namespace of the instance instances is placed on top of the class namespace, so if you have a class attribute and an instance attribute with the same name, instance 'one will be available through inst.attr , otherways - class' one). So, when you do

 object1.variable = 1 

you don’t actually change the class variable (which is often called a static field in other languages), you simply bind a new field called variable with an instance of class object1 .

What you probably want to do is change the class variable, not instance 1. This can be done as follows:

 Sample.variable = 1 

or like this:

 object1.__class__.variable = 1 
+2
source

Two instances of the same class do not use the same memory and the same variables. For example, you can have a Car whose color is red and the other color is blue .

+1
source

You answered the question in the title. These are two separate and different instances of the same class. object1 and object2 are different objects, and therefore everything that happens to one of these objects will not change the other.

+1
source

Because object1 is a different object than object2. To make them 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 

Pay attention to the line that indicates

 object2 = object1 
+1
source

This may not be logical, but when you write

 object1.variable = 1 

You do not assign a class variable, but define an instance variable with the same name. (You can check object1 __dict__ to check this). Please note that if you write

 Sample.variable 

or

 object1.__class__.variable 

In both cases, you will get 2 because variable never changed. To assign to it, you can refer to it as described above - object1.__class__.variable = 1 , for example, makes object2.variable == 1 as well.

+1
source

Source: https://habr.com/ru/post/904802/


All Articles