tags:

views:

126

answers:

2

im using the fantastic eric4 ide to code python, it's got a tool built in called 'cyclops', which is apparently looking for cycles. After running it, it gives me a bunch of big bold red letters declaring there to be a multitude of cycles in my code. The problem is the output is nearly indecipherable, there's no way im gonna understand what a cycle is by reading its output. ive browsed the web for hours and cant seem to find so much as a blog post. when the cycles pile up to a certain point the profiler and debugger stop working :(.

my question is what are cycles, how do i know when im making a cycle, how do i avoid making cycles in python. thanks.

+1  A: 

All Cyclops tells you is whether there are objects in your code that refer to themselves through a chain of other objects. This used to be an issue in python, because the garbage collector wouldn't handle these kinds of objects correctly. That problem has since been, for the most part, fixed.

Bottom line: if you're not observing a memory leak, you don't need to worry about the output of Cyclops in most instances.

Silas
Thanks for that Silas, cyclops is junk, as long as i,m not making references to things that make references to the things that referenced them. <-- now theres a mouthful.
spearfire
+4  A: 

A cycle (or "references loop") is two or more objects referring to each other, e.g.:

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 extremely simple examples with cycles of just two items; real-life examples are often longer and harder to spot. There's no magic bullet telling you that you just made a cycle -- you just need to watch for it. The gc module (whose specific job is to garbage-collect unreachable cycles) can help you diagnose existing cycles (when you set the appropriate debug flags). The weakref module can help you to avoid building cycles when you do need (e.g.) a child and parent to know about each other without creating a reference cycle (make just one of the two mutual references into a weak ref or proxy, or use the handy weak-dictionary containers that the module supplies).

Alex Martelli
Thanks Alex i get it now, one more, is weakref the only way to have a child and parent know each of each others existence?
spearfire
@spearfire, no, there are many ways -- you can decide to create a cycle after all; or, you can put all entities into a dict, each with a unique key, and have other entities hold such keys into that dict (to be used only after checking the key's still there!-) in lieu of referring to the entities directly -- and many other ways besides.
Alex Martelli