Style question on recursive generators

Alex Martelli aleaxit at yahoo.com
Mon Oct 18 14:54:21 EDT 2004


On 2004 Oct 18, at 16:57, Carlos Ribeiro wrote:
    ...
> def walk(self):
>    """generator-based recursive tree traversal"""
>    yield child
>    for child in childs:

Btw, what are this 'child' and 'childs', _globals_?  I assume you must 
mean something like self and self.children, but as that may become 
important in further discussion it would be nice to be more precise in 
the example.

>         transfer child.walk()
>
> It's quite weird -- a generator plus a direct flow-of-execution
> transfer. It's not a goto, its not a co-routine... It requires a new
> keyword ('transfer', in this case), which _is_ a real problem --

How does the semantics of this differ from those of
          for x in child.walk(): yield x
?  I don't know what a "direct flow of execution transfer" _is_, since 
when the 'transfer' is finished execution had better come back here.  
So, I fail to appreciate the difference, if any, between:
     for x in y: yield x
and
     transfer y
which is just the same thing I had mentioned (under guise of 'yieldall' 
in lieu of 'transfer', but that's mere sugar) in the post you're 
replying to.  And I think that a combination such as
     yield from y
besides not requiring a new keyword might be even more readable.  I'm 
using y as just a placeholder here, it would be "yield from 
child.walk()" in your case.

If the issue is strictly one of whether it's worth somehow making
     for x in y: yield x
terser (by a new kw, combination of kw, and/or punctuation) I guess we 
can discuss that (I just don't see the gain to offset the price of 
introducing new syntax and one more language thing to learn for 
Pythonistas and newcomers to Python).  If you mean something different 
for your 'transfer' then maybe you could clarify _what_...


Alex




More information about the Python-list mailing list