0
Question from Python challenge
I want to know why the output is 2 for b.n and 2 for the second a.n when n=0 in MyClass() ? https://code.sololearn.com/c6J4Urvy4cBR/?ref=app
2 Answers
+ 3
The class is set up to count +1 when a new instance is created and -1 when an instance is destroyed.
When you write a = ... the second time, you strip off the name a from the instance that had it before.
This means, it has no reference in your code anymore.
And this means that the object is deleted.
The same moment you create a new instance and stick the name a on it.
If you do -1, then +1, you end up with the same result.
I explain this in more detail here:
https://code.sololearn.com/c89ejW97QsTN/?ref=app
+ 2
Well, you see : the variable a is reused. That mean that the object it hold is destroyed, and a new object is created and reassigned.