A cycle (or “link cycle”) is two or more objects that reference each other, for example:
alist = [] anoth = [alist] alist.append(anoth)
or
class Child(object): pass class Parent(object): pass c = Child() p = Parent() c.parent = p p.child = c
Of course, these are very simple examples with two-part cycles; real life examples are often longer and more complex. There is no magic bullet telling you that you just made a cycle - you just need to keep track of it. The gc module (whose specific task is to collect invalid garbage collection cycles) can help you diagnose existing cycles (when you set the appropriate debug flags). The weakref module can help you avoid creating loops when you need it (for example, a child and a parent, to know about each other without creating a reference loop (make only one of two reciprocal links in a weak ref or proxy, or use convenient containers with a weak dictionary supplied by the module).
source share