(why) inconsistent yield/return syntax?

Alex Martelli aleax at aleax.it
Tue Feb 11 17:51:24 EST 2003


Andrew Koenig wrote:

> Alex> If one's happy with any empty _iterator_, though:
> 
> Alex> def zeroleniter(): return iter([])
> 
> Alex> is clearly simpler and even more concise.  I see
> Alex> no reason to use any other form.
> 
> I do.  If I write
> 
>         def foo():
>                 return
>                 yield None
> 
> and I want to come back and change it later so that the sequence it
> yields is no longer empty, then it's easy to change.

Basically, you have to throw away the body of the function
and write the new one you want.  With function zeroleniter,
on the other hand, you have to throw away the body of the 
function and write the new one you want.

So, the two implementations are equally easy to change (with
most editors, which make throwing away half a line just about
as easy as throwing away two lines; some editors with weird
human factors _might_ give a fraction of a second advantage
to one form or the other -- but I don't think that matters).


> If, on the other hand, I want to write a function that forevermore
> will yield an empty sequence, then I think your suggestion is better.

I don't think any commitment to omniscience is necessary in
order for zeroleniter to be preferable, because it's just as
plastic as 'foo' is.


Alex





More information about the Python-list mailing list