Python __init__ vs class attribute

I am very new to programming. I just started a couple of weeks. I read for hours about the class, but I'm still confused. I have a specific question.

I got confused when to use class attributes and when to use initializer ( __init__ ).

I understand that when using __init__ I do not assign a value immediately, but I need to assign a value only when creating an object using this class. Class attributes are automatically inherent in the object created in this class.

But in terms of practical use, they do the same thing? Are these just two different ways to do the same? Or does __init__ do things that class attributes cannot do?

I passed testing with these codes, the results remained the same. I am confused when to use. To me, a cool attribute looks more convenient to use.

 #use class attributes for class Numbers_1 class Numbers_1: one = 1 two = 2 three = 3 six = two * three def multiply(self): return self.six * self.two * self.three #use initializer for class Numbers_2 class Numbers_2: def __init__(self, num10, num20, num30, num600): self.num10 = num10 self.num20 = num20 self.num30 = num30 self.num600 = num600 def multiply(self): return self.num600 * self.num20 * self.num30 #Now I run some test to compare the two classes... x = Numbers_1() y = Numbers_2(10, 20, 30, 20*30) print(x.one) #print 1 print(y.num10) #print 10 print(x.six) #print 6 print(y.num600) #print 600 #assign attributes to each objects x.eighteen = x.six * x.three y.num18000 = y.num600 * y.num30 print(x.eighteen) #print 18 print(y.num18000) #print 18000 #try printing methods in each object print(x.multiply()) #print 36 print(y.multiply()) #print 360000 #try reassign values to attributes in each object x.one = 100 y.num10 = 1000 print(x.one) #prints 100 print(y.num10) #prints 1000 
+5
source share
4 answers

Everything is fine with you - except that the class attributes also work like static variables in python.

Note that when parsing with the python interpreter, everything in the class area starts immediately .

 # file1.py def foo(): print("hello world") class Person: first_name = foo() last_name = None def __init__(self): last_name = "augustus" print("good night") # file2.py import file1 >>> "hello world" x = Person() >>> "good night" 
+3
source

To understand the difference, you need to consider the difference between classes and instances of these classes.

Class attributes apply to every object of this class. Modifying them changes all instances of this class. Changing the attributes of an instance only changes the specific object being processed.

For instance:

 class Foo: class_var = 'bar' def __init__(self): self.instance_var = 'baz' foo1 = Foo() foo2 = Foo() print(foo1.class_var, foo2.class_var) print(foo1.instance_var, foo2.instance_var) Foo.class_var = 'quux' Foo.instance_var = "this doesn't work" foo1.instance_var = 'this does' print(foo1.class_var, foo2.class_var) print(foo1.instance_var, foo2.instance_var) 

prints

 bar bar baz baz quux quux this does baz 

So, changing Foo.class_var replaces class_var for all existing instances of Foo , and modifying Foo.instance_var does nothing. However, changing the instance_var object of type Foo works, but only for this particular instance — other instances are not changed.

+2
source

If you create multiple objects, you can see the difference.

 class Numbers_1: one = 1 two = 2 six = one * two def __init__(self, o, t): self.o = o self.t = t def mul(self): return self.o * self.t o1 = Numbers_1(1, 2) o2 = Numbers_1(10, 20) o3 = Numbers_1(20, 30) print(o1.six) # 2 print(o2.six) # 2 print(o3.six) # 2 print(o1.mul()) # 2 print(o2.mul()) # 200 print(o3.mul()) # 600 

a variable, such as one, to, six, is called class variables.

Class variables are shared by objects created with the same class.

+2
source

class attributes are not object specific. For instance:

 x = Numbers_1() y = Numbers_1() 

In the above example, x and y will have the same class attributes.

In contrast, the init function defines the attributes of an object. For instance:

 s = Numbers_2(10, 20, 30, 20*30) t = Numbers_2(11, 21, 31, 21*31) 

s and t now have different object attributes.

+1
source

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


All Articles