[Python-ideas] Propagating StopIteration value

Terry Reedy tjreedy at udel.edu
Mon Oct 8 04:40:31 CEST 2012


On 10/7/2012 7:30 PM, Greg Ewing wrote:
> Oscar Benjamin wrote:
>> Before pep 380 filter(lambda x: True, obj) returned an object that was
>> the same kind of iterator as obj (it would yield the same values). Now
>> the "kind of iterator" that obj is depends not only on the values that
>> it yields but also on the value that it returns. Since filter does not
>> pass on the same return value, filter(lambda x: True, obj) is no
>> longer the same kind of iterator as obj.
>
> Something like this has happened before, when the ability to
> send() values into a generator was added. If you wrap a
> generator with filter, you likewise don't get the same kind
> of object -- you don't get the ability to send() things
> into your filtered generator.
>
> So, "provide the same kind of iterator" is not currently part
> of the contract of these functions.

Iterators are Python's generic sequential access device. They do that 
one thing and do it well.

The iterator protocol is intentionally and properly minimal. An iterator 
class *must* have appropriate .__iter__ and .__next__ methods. It *may* 
also have any other method and any data attribute. Indeed, any iterator 
much have some specific internal data. But these are ignored in generic 
iterator (or iterable) functions. If one does not want that, one should 
write more specific code.

For instance, file objects are iterators. Wrappers such as filter(lambda 
line: line[0] != '\n', open('somefile')) do not have any of the many 
other file methods and attributes. No one expects otherwise. If one 
needs access to the other attributes of the file object, one keeps a 
direct reference to the file object. Hence, the recommended idiom is to 
use a with statement.

Generators are another class of objects that are both iterators (and 
hence iterables) and something more. When they are used as input 
arguments to generic functions of iterables, the other behaviors are 
ignored, and should be ignored, just as with file objects and any other 
iterator+ objects.

Serhily, if you want a module of *generator* specific functions 
('gentools' ?), you should write one and submit it to pypi for testing.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list