class ToBeDeleted:
def __init__(self, value):
self.value = val
# Whatever...
def __del__(self):
print self.value
l = [ToBeDeleted(i) for i in range(3)]
del l
This prints 2, 1, 0
.
Now, is the order of the deleted elements defined somewhere in specification or is it implementation-specific? (or maybe I don't understand the underlying mechanics)
Could the output, for example, be
0, 1, 2
? I realize the2, 1, 0
order is probably done to avoid the memory reallocations for the elements while deleting them, but still the question remains.And the last one - what's the difference between
del l
anddel l[:]
statements?
__del__()
is specified anywhere. Don't even think about trying to write code that relies on a particular ordering. – Jeer__del__
methods get called would be different than the order in which the references are removed from the list. – Cavern