Recursive list comprehension

Steven Bethard steven.bethard at gmail.com
Wed Dec 8 19:56:11 EST 2004


Terry Reedy wrote:
> "Steven Bethard" <steven.bethard at gmail.com> wrote in message 
> news:o5Jtd.224974$R05.24953 at attbi_s53...
> 
>>Probably you want to catch a TypeError instead of an AttributeError; 
>>objects may support the iterator protocol without defining an __iter__ 
>>method:
> 
> 
> No, having an __iter__ method that returns an iterator is an essential half 
> of the current iterator protocol just so that iter(iterator) (== 
> iterator.__iter__()) always works.  This requirement conveniently makes 
> 'iterator' a subcategory of 'iterable'.

Yeah, you're right, I probably should have referred to it as the 
'iterable protocol' instead of the 'iterator protocol'.

 > (I am ignoing the old and obsolete
 > getnext protocol, as does the itertools library module.)

What is the getnext protocol?  Is that the same thing that the iter() 
docs call the sequence protocol?  Because this definitely still works 
with itertools:

 >>> class C:
...     def __getitem__(self, index):
...         if index > 3:
...             raise IndexError(index)
...         return index
...
 >>> import itertools
 >>> list(itertools.imap(str, C()))
['0', '1', '2', '3']

Steve



More information about the Python-list mailing list