Python › Memory Management & Garbage Collection
How do circular references between objects with __del__ methods behave differently in Python 3.4+ compared to earlier versions?
AdvancedAnswer
Before Python 3.4 (PEP 442), objects in a reference cycle where any participant defined __del__ could never be automatically collected -- they were placed in gc.garbage and effectively leaked forever, requiring manual intervention. Since PEP 442, the garbage collector can safely finalize objects in cycles even with __del__ methods present, calling each object's __del__ exactly once and correctly handling resurrection edge cases, though relying on __del__ for critical logic is still discouraged.
import gc
class Node:
def __del__(self):
print('Node destroyed')
a, b = Node(), Node()
a.ref, b.ref = b, a # cycle
del a, b
gc.collect() # in 3.4+, both __del__ methods run correctly during collectionReal-world example A legacy codebase migrating from Python 2 removes workarounds that manually broke reference cycles before deletion, since Python 3.4+'s collector safely handles cycles involving __del__ without that extra code.