0

Why is the output 1, 2, 2 and not 1, 2, 3?

Why is the output 1, 2, 2 and not 1 2 3? If I never delete any element the output is 1, 2, 2, now when I change in line 16 the variable a by c .. if I have a 1, 2, 3 output? # https://code.sololearn.com/cmZL80XDJ5K3 1 class MyClass(): 2 n = 5 3 4 def __init__(self): 5 MyClass.n += 1 6 7 def __del__(self): 8 MyClass.n -= 1 9 10 a = MyClass() 11 print (a.n) 12 13 b = MyClass() 14 print (b.n) 15 16 a = MyClass() 17 print (a.n)

17th Mar 2017, 7:21 PM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
1 Réponse
+ 9
The last assignment deletes 'a' variable (thus decreasing MyClass.n by one) and instantiates it again at the same time (thus increasing MyClass.n by one). So the balance is neutral :)
17th Mar 2017, 8:34 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar