A bundle of questions from a Python newbie

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Feb 21 06:45:50 EST 2002


Paul Rubin <phr-n2002a at nightsong.com> wrote in
news:7xbsejkrl1.fsf at ruckus.brouhaha.com: 

>> Python's generators can only yield directly from the generator as
>> they do not attempt to maintain any kind of stack.
> 
> Is that right?  I thought the classic coroutine example is supposed to
> also work with generators:
> 
>   def traverse(tree):
>      if tree.left: traverse(tree.left)
>      yield tree.value
>      if tree(right) traverse(tree.right)
> 
> Am I missing something?
> 
> 

Look in lib/test/test_generators for Guido's binary tree example:

    >>> def inorder(t):
    ...     if t:
    ...         for x in inorder(t.left):
    ...             yield x
    ...         yield t.label
    ...         for x in inorder(t.right):
    ...             yield x



-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list