Say we have a class Foo
class Foo:
def __del__(self):
# do some kind of deleting
def run(self):
# run something
And foo has been added to a list. Its methods will be used later in the program.
l = [Foo()]
Foo is now inside a list.
If I wanted to use the method run
I simply call l
along with its index and method
l[0].run()
Now if I want to run the __del__
method. I could call Foo
's method __del__
l[0].__del__()
My question is:
why isn't del l[0]
not the same as l[0].__del__()
Edit:
After messing around a little further, I've found that list[0].__del__()
and del list[0]
do produce the same results, however calling __del__
does not remove the item in the list, del
does however
class Foo:
def __del__(self):
print("Deleting")
list = [Foo()]
print(list)
list[0].__del__()
print(list)
del list[0]
print(list)
>
[<Foo object at 0x7f4ad04d97f0>]
Deleting
[<Foo object at 0x7f4ad04d97f0>]
Deleting
[]
>