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.
buran
  • 13,682
  • 10
  • 36
  • 61
Max
  • 23
  • 3
  • 2
    `__del__` is not a destructor and does not destroy objects. – user2357112 Mar 13 '22 at 12:15
  • 1
    You should probably read [What is the \_\_del__ method and how do I call it?](https://stackoverflow.com/q/1481488/354577) – ChrisGPT was on strike Mar 13 '22 at 12:16
  • It's probably a good idea to read [the language reference](https://docs.python.org/3/reference/datamodel.html#object.__del__) 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. – MisterMiyagi Mar 13 '22 at 12:31

1 Answers1

4

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.

Bharel
  • 23,672
  • 5
  • 40
  • 80
  • 1
    Note that as per [PEP 442](https://peps.python.org/pep-0442/), a regular object's finalizer is called exactly once (if at all) since Python 3.4. – MisterMiyagi Mar 13 '22 at 12:37
  • @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;" – Bharel Mar 13 '22 at 12:44
  • 1
    @MisterMiyagi [beep boop](https://github.com/python/peps/issues/2424). – Bharel Mar 13 '22 at 12:51
  • 1
    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 Mar 13 '22 at 13:13
  • 2
    @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 – Bharel Mar 13 '22 at 13:16