empty lists vs empty generators

Leif K-Brooks eurleif at ecritters.biz
Wed May 4 16:33:31 EDT 2005


Jeremy Bowers wrote:
> On Wed, 04 May 2005 13:45:00 +0000, Leif K-Brooks wrote:
> 
> 
>>Jeremy Bowers wrote:
>>
>>>    def __init__(self, generator):
>>>        self.generator = generator
>>
>>You'll want to use iter(generator) there in order to handle reiterables.
> 
> 
> Can you expand that explanation a bit? I'm not certain what you mean. I'm
> just trusting what the user passes in; maybe the user should pass it
> iter(generator) when it's a "reiterable"? (Honest question.) 
> 
> What definition of "re-iterable" are you using? (A quick google for
> "Python reiterabile" just turns up some Python dev list entries from 2003.)

Reiterable is generally defined as an object which can be iterated over 
multiple times (i.e. is iterable but isn't an iterator). The simplest 
example is a list, but a few other built-in types (set and dict, for 
instance) also qualify.

With the EmptyGeneratorDetector class as you defined it, lists will fail:

 >>> EmptyGeneratorDetector([])
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 15, in __init__
AttributeError: 'list' object has no attribute 'next'

Of course, the class is labeled as an empty generator detector, not an 
empty iterable detector, so it's doing what it says it will, but a 
little bit of extra generalism can't hurt.



More information about the Python-list mailing list