yield_all needed in Python

Nick Coghlan ncoghlan at iinet.net.au
Thu Mar 3 10:58:58 CET 2005


Douglas Alan wrote:
> Wouldn't
> 
>    yield *(x for x in gen1(arg))
> 
> be sufficient, and would already be supported by the proposal at
> hand?

It would, but, as Steven pointed out, the * in func(*args) results in 
tuple(args) being passed to the underlying function.

So I see no reason to expect "yield *iterable" to imply a for loop that yields 
the iterators contents. IMO, it's even more of a stretch than the tuple 
unpacking concept (at least that idea involves tuples!)

Whereas:

   yield x for x in iterable if condition

Maps to:
   for x in iterable:
     if condition:
       yield x

Just as:
   [x for x in iterable if condition]

Maps to:
   lc = []
   for x in iterable:
     if condition:
       lc.append(x)

And:
   (x for x in iterable if condition)

Maps to:
   def g()
     for x in iterable:
        if condition:
           yield x

And removing a couple of parentheses is at least as clear as adding an asterisk 
to the front :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list