What does the following code demonstrate about Python’s GC and reference cycles? import gc class Node: def __init__(self): self.ref = None a = Node() b = Node() a.ref = b b.ref = a del a, b print(gc.collect())

Python Professional Hard

Python Professional — Hard

What does the following code demonstrate about Python’s GC and reference cycles? import gc class Node: def __init__(self): self.ref = None a = Node() b = Node() a.ref = b b.ref = a del a, b print(gc.collect())

Key points

  • gc.collect() is used to detect and collect cyclic garbage
  • Mutual references between objects create reference cycles
  • Python's GC system handles cyclic garbage collection
  • The positive integer output indicates successful collection

Ready to go further?

Related questions