+ 4

Need help in below example of class and function !

Can someone help in describing below code, step by step, why the output will be 4? class FirstClass: n = 4 def __init__(self, n): n = n // 4 print(n) a = FirstClass(8) print(a.n)

28th Jul 2018, 2:19 PM
Dolan
Dolan - avatar
1 Odpowiedź
+ 2
At a glance, it seems that in the init function you'll get 2 out, but no assignment to the class variable takes place (should be self.n = n // 4. The left side of = is the class variable and the right side is the local parameter of the init function.) When you access a.n it's still 4, as declared above
28th Jul 2018, 2:23 PM
Dan Walker
Dan Walker - avatar