I'm really confused by the following code's output, that I am running on Ipython Notebook:
class Point(object):
def __init(self, x=0, y=0):
self.x = x
self.y = y
def __del__(self):
class_name = self.__class__.__name__
print class_name, "destroyed"
pt1 = Point()
pt2 = pt1
The output is as follows and I can't seem to understand when the destructor is called: Point destroyed
Also, then when I write the code below and expect to get an Error, I don't.
del pt1
del pt2
Thanks
Edit: I understand that Python only calls _del_()
when the reference count goes to 0, but what I don't understand is why writing the following results in the Output: Point destroyed
.
pt1 = Point()
pt2 = pt1
Also, given the above, I know that either pt1
or pt2
doesn't exist and would thus expect an error in the below code, but that doesn't happen.
del pt1
del pt2
pt1 = Point()pt2 = pt1
should not output anything