I was learning about other built-in class methods in Python and came across one called __del__()
. Like __repr__()
and __str__()
, I believed it could be overriden and decided to test it.
Here is a basic class I created:
class SomeClass:
def __del__(self):
print("Deleted!")
Just to confirm this code works, here is me testing it in the console:
>>> test = SomeClass()
>>> del test
Deleted!
When running in the console, I tested it with x and y variables:
>>> x = SomeClass()
>>> y = x
>>> del x # This should print "Deleted!"
>>> del y
Deleted!
Strangely, when deleting x
, it never outputted anything, but when deleting y
, which was assigned to be equal to the x object
, it outputted "Deleted!".
I didn't stop there, though. I tested this concept again, but now with a
and b
variables.
>>> a = SomeClass()
>>> b = a
>>> del a
>>> b # Check if b exists
<__main__.SomeClass instance at 0x7f98a1a67fc8>
>>> del b # Like previously, with 'y', this should print "Deleted!"
>>> globals() # Well, it didn't. Let's check all our global variables to confirm
Deleted!
{'__builtins__': <module '__builtin__' (built-in)>, 'SomeClass': <class __main__.SomeClass at 0x7f98a1a5f668>, '__package__': None, '__name__': '__main__', '__doc__': None}
As you can see, calling the humble globals()
method seem to print "Deleted!" then printing the globals which contains no reference of b
, so it did delete it.
How comes overriding the __del__
method causes odd issues? I thought of using the method to control what happens when certain objects in a game are deleted, but now it makes me less convinced, as odd bugs may occur when I duplicate objects and delete them.