test = [A() for _ in range(3)]
for i in range(3, -1, -1):
    assert len(A.instances) == i
if test:
    test.pop()
print('see if 3 are removed at exit')
test = [A() for _ in range(3)]
In Python: How to remove an object from a list if it is only referenced in that list?