1

Will the following not cause issue of freeing memory twice? Why is python3 destroying an object when it has already been destroyed by the programmer?

class Example:

    # Initializing
    def __init__(self):
        print('object created.')

    # Deleting (Calling destructor)
    def __del__(self):
        print('Destructor called, object deleted.')


obj = Example()
obj.__del__()

Output:

object created.
Destructor called, object deleted.
Destructor called, object deleted.
3
  • 2
    __del__ is not a destructor and does not destroy objects. Commented Mar 13, 2022 at 12:15
  • 1
    You should probably read What is the __del__ method and how do I call it? Commented Mar 13, 2022 at 12:16
  • It's probably a good idea to read the language reference when wondering about such "internals". In this case that would clear up that __del__ is not a destructor, not responsible for freeing memory, and that the object isn't necessarily destroyed after __del__ even when not invoked manually. Commented Mar 13, 2022 at 12:31

1 Answer 1

5

By calling __del__() you're just calling a method on the instance, without any special meaning.

The __del__() method, both yours and by default does absolutely nothing. Moreover, it is not a destructor but rather a finalizer:

Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor.

Python's memory allocation is completely internal. The only weak guarantee in Python is that if a method __del__() exists, it will be called right before the object is deleted from memory. It may even be called twice or thrice on certain occasions if the object get resurrected, or not at all in case of daemon threads.

For more info, please read the documentation.

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

5 Comments

Note that as per PEP 442, a regular object's finalizer is called exactly once (if at all) since Python 3.4.
@MisterMiyagi I remember that pep, question is: "While this PEP discusses CPython-specific implementation details, the change in finalization semantics is expected to affect the Python ecosystem as a whole" - Is it an implementation detail or a guarantee...? Wording is a bit unclear. Either way, it's either an implementation detail or a bug in the documentation: "It is implementation-dependent whether __del__() is called a second time when a resurrected object is about to be destroyed;"
@MisterMiyagi beep boop.
As far as I can tell, it's an implementation detail in as far as a compliant implementation could behave differently, but all existing compliant implementations follow it. Getting a clarification sure would be interesting.
@MisterMiyagi in that case, although I'm a bit hardcore, I treat it like it can change any day of the week, meaning I'd prefer leaving my answer as is :-) /me does not like bugz

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.