Reference Counting Cannot Break Cycles
import gc
class Node:
def __init__(self, label):
self.label = label
self.other = None
def __del__(self):
print(f"{self.label} freed")
gc.disable()
a = Node("A")
b = Node("B")
a.other = b
b.other = a
del a
del b
print("after del")
gc.collect()
print("after collect")
gc.disable() turns off CPython's cyclic garbage collector, so only reference counting is running until gc.collect() is called explicitly. What does this print, in order?
Sign in to answer questions and track your progress
Sign In