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

Alex Martelli aleax at aleax.it
Mon Nov 18 04:55:12 EST 2002


Greg Ewing wrote:

> Alan Kennedy wrote:
> 
>> class emptyGenerator:
>> 
>>     def __init__(self):
>>         raise StopIteration()
>> 
>> def emptyGen():
>>     yield emptyGenerator()
> 
> This is quite nice, but it's misnamed slightly.
> Something called xxxGenerator should be returning
> an iterator when you call it, but this isn't.
> 
> It would be nicer if it were called Nothing.
> Then you could write
> 
>    def emptyGen():
>       yield Nothing()
> 

It seems to me that there's no real usefulness in making Nothing a class -- 
it just takes an extra statement or so to come to the key raise statement.  

So why not use a function instead:

def Nothing(): raise StopIteration

def emptyGen(): yield Nothing()


Alex




More information about the Python-list mailing list