[Python-ideas] Cofunctions/yield from -> fibers

Scott Dial scott+python-ideas at scottdial.com
Fri Aug 13 17:37:57 CEST 2010

On 8/13/2010 4:11 AM, Greg Ewing wrote:
> That's not actually true -- a for-loop would work with any
> iterable node object just as well.

I agree that it works, but it does not avoid the recursion-depth problem
and you pay for the decent through all of the for-loops. I assume we are
both talking about an implementation __iter__() like:

    def __iter__(self):
        if self.left:
            for v in self.left:
                yield v
        if self.us:
            yield self.us
        if self.right:
            for v in self.right:
                yield v

But this fails with a recursion depth RuntimeError around a depth of
~1000 or so. Perhaps that is not an interesting practical problem. But,
my comment about it being "complicated and verbose" was that to avoid
that depth problem, the obvious solution is to make __iter__() implement
an in-order stack itself and manufacturer some way to deal with other
types being in the tree. But again, due to how deep of a structure you
need, perhaps it's not that interesting.

Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu

More information about the Python-ideas mailing list