When I try to delete one instance of a class, it causes unexpected output from the __del__ method of another instance:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
print('{0} has been born!'.format(self.name))
def __str__(self):
return '{0} is {1} years old'.format(self.name, self.age)
def __del__(self):
print('{0} is deleted!'.format(self.name))
p1 = Person("John", 20)
p2 = Person("Mary", 27)
print(p1)
print(p2)
del(p2)
Output is:
John has been born!
Mary has been born!
John is 20 years old
Mary is 27 years old
Mary is deleted!
John is deleted!
Why "John is deleted!"?