Suppose I have a class, call it class1,
with 3 class variables var1,var2,var3,
and __init__
method, which assigns passed arguments to the class variables:
class class1(object):
var1 = 0
var2 = 0
var3 = 0
def __init__(self,a,b,c):
class1.var1 = a
class1.var2 = b
class1.var3 = c
Now I'm going to make two instances of the same class:
obj1 = class1(1,2,3)
obj2 = class1(4,5,6)
And now, let's take a look at variables values:
print (obj1.var1, obj1.var2,obj1.var3)
4 5 6
print (obj2.var1, obj2.var2,obj2.var3)
4 5 6
Shouldn't obj1
have values 1,2,3 ? Why __init__
method of the second instance changes vaues in the first instance(obj1
)? And how to make two independent separate instances of a class?