My (late) beef with Simple Generator syntax (PEP 255)

Cameron Horn camhorn at mac.com
Wed Nov 13 21:19:52 EST 2002


On Wednesday, November 13, 2002, at 08:17 PM, Terry Reedy wrote:

>
> "Cameron Horn" <camhorn at mac.com> wrote in message
> news:mailman.1037219902.17042.python-list at python.org...
>> If I want to write an empty generator, I write this(?):
>> def foo():
>>    return
>>    yield "never"
>>
>> Assuming that having an empty generator is as valid as empty
> functions and classes,
>> what kind of linguistic cruft is that?  I expect better out of
> python, dang it.
>
> I challenge the assumption.  A generator, by definition, is a
> (nonempty) function (and not a separate generator type) that returns
> an iterator with __iter__ and next methods.

I agree, sort of.  The thing is that the implementation seems designed to 
let you ignore all those iteration details in most simple cases.  I just 
find it weird that writing the simplest possible generator, the one that 
iterates over no elements, either requires that you write that bit of 
nonsense above or know enough about the implementation to return an empty 
iterator.

Do you think that a generator that returns an empty iterator (i.e., one 
that has no elements, as written above) isn't valid or useful?   Or that as 
written above doesn't make such a generator?





More information about the Python-list mailing list