As far as I understand Python destructors should be called when the reference count of an object reaches 0. But this assumption seems not to be correct. Look at the following code:
class A:
def __init__(self, b):
self.__b = b
print("Construct A")
def __del__(self):
# It seems that the destructor of B is called here.
print("Delete A")
# But it should be called here
class B:
def __init__(self):
print("Construct B")
def __del__(self):
print("Delete B")
b = B()
a = A(b)
Outputs
Construct B
Construct A
Delete B
Delete A
But A has a reference to B, so I would expect the following output:
Construct B
Construct A
Delete A
Delete B
What am I not getting?