[Python-3000] map() Returns Iterator

Nicko van Someren nicko at nicko.org
Mon Aug 6 20:33:03 CEST 2007


On 6 Aug 2007, at 16:22, Steven Bethard wrote:

> On 8/6/07, Nicko van Someren <nicko at nicko.org> wrote:
...
>> Filter returning an iterator is going to break lots of code which
>> says things like:
>>         interesting_things = filter(predicate, things)
>>         ...
>>         if foo in interesting_things: ...
>
> Actually, as written, that code will work just fine::
> ...
> Perhaps you meant to have multiple if clauses?

You're right, I did!  I wrote an much longer example with multiple  
ifs and it looked too verbose, so I edited it down, and lost the  
meaning.

In fact, from a bug tracing point of view it's even worse, since,  
reusing your example, code with currently reads:
	interesting_things = filter(str.isalnum, 'a 1 . a1 a1.'.split())
	if '1' in interesting_things:
		print "Failure"
	if 'a1' in interesting_things:
		print "... is not an option"
will currently do the same as, but in v3 will be different from:
	interesting_things = filter(str.isalnum, 'a 1 . a1 a1.'.split())
	if 'a1' in interesting_things:
		print "Failure"
	if '1' in interesting_things:
		print "... is not an option"

If filter() really has to be made to return an iterator then (a) 2to3  
is going to have to make lists of its output and (b) the behaviour is  
going to need to be very clearly documented.  I do think that many  
people are going to be confused by this change.

	Nicko



More information about the Python-3000 mailing list