You are confusing the del
statement and the __del__
method.
del a
simply unbinds the name a
from whatever object it referenced. The list referenced by aa
is unchanged so the objects all continue to exist.
The __del__
method is only called after the last reference to an object has been destroyed. That could be after a call to __del__
but usually isn't.
You rarely need to use del
. It would be much more common just to rebind aa
and then all the objects it contains will be released, and if not otherwise referenced their __del__
methods will be called automatically.
Also, you rarely need to use __del__
. For most purposes Python's management of objects will handle cleanup automatically. Adding a __del__
method to a class is generally a bad idea as it can interfere with the garbage collector, so rather paradoxically __del__
makes it more likely that your program will leak memory. Also Python won't guarantee whether __del__
is actually called on program exit, and if it does you may find global variables you cant to use no longer exist, nor will it guarantee to only call it once (though you have to jump through hoops to make it call it more than once).
In short, avoid using __del__
if you possibly can.