1

Suppose I have the following code in Python:

class Test:
    def __del__(self):
        print("del is called")


a = Test()
a = Test()

yields to the following output:

`del is called`

Why is that and what is the concept behind this?

Is "del" called after every reassignment?

2
  • Possible duplicate of When is del useful in python? Commented Apr 26, 2019 at 8:46
  • I would like to know, why "del" is implicitly called in this context Commented Apr 26, 2019 at 8:55

1 Answer 1

2

__del__ is called because there's no more references to the original a, causing it to be garbage-collected. See the Python docs for details.

You only get the output once, because

It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.