0

Can someone explain this result?

I found this code while doing challenges but I don't understand it. class A: def __init__(a, b, c): print(b) A(8,3) output: 8 the a variable is the main object

9th Jan 2018, 3:36 PM
Kevin Oudai
Kevin Oudai - avatar
2 Answers
+ 6
The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self.In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. Python doesn't force you on using "self". You can give it any name you want. But remember that the first argument in a method definition is a reference to the object.Python adds the self argument to the list for you; you do not need to include it when you call the methods. if you didn't provide self in init method then you will get an error so in this code it works like this class A: def __init__(a, b, c): print(b) A(8,3) in this it take argument 'a' as self so first argument is b and second argument is c so now if A(8,3) b=8 and c=3 and then it will print the value of b
9th Jan 2018, 3:48 PM
GAWEN STEASY
GAWEN STEASY - avatar
0
thank you
10th Jan 2018, 9:44 PM
Kevin Oudai
Kevin Oudai - avatar