[Python-3000] Python 3000 Status Update (Long!)

Alex Martelli aleaxit at gmail.com
Wed Jun 20 19:50:48 CEST 2007


On 6/20/07, Christian Heimes <lists at cheimes.de> wrote:
> Nick Coghlan wrote:
> > Because (str(x) for x in seq) is not an improvement over map(str, x) -
> > applying a single existing function to a sequence is a very common
> > operation.
> >
> > map() accepts any function (given an appropriate number of sequences),
> > and thus has wide applicability.
> >
> > filter() accepts any single argument predicate function (using bool() by
> > default), and thus also has wide applicability.
>
> But map() and filter() can be easily replaced with a generator or list
> comprehensive expression. [str(x) for x in seq] and [x for x in seq if
> func(x)] are consider easier to read these days.
>
> IIRC map and filter are a bit slower than list comprehensive and may use
> much more memory than a generator expression since map and filter are
> returning a list of results.

No, in 3.0 they'll return iterables -- you really SHOULD read Guido's
blog entry referred to at the top of this thread,
<http://www.artima.com/weblogs/viewpost.jsp?thread=208549>, before
discussing Python 3.0 issues.

So, there's no reason their performance should suffer, either -- using
today's itertools.imap as a stand-in for 3.0's map, for example:

$ python -mtimeit -s'import itertools as it' -s'L=range(-7,17)' 'for x
in it.imap(abs,L): pass'
100000 loops, best of 3: 3 usec per loop
$ python -mtimeit -s'import itertools as it' -s'L=range(-7,17)' 'for x
in (abs(y) for y in L): pass'
100000 loops, best of 3: 4.47 usec per loop

(imap is faster in this case because the built-in name 'abs' is looked
up only once -- in the genexp, it's looked up each time, sigh --
possibly the biggest "we should REALLY tweak the language to let this
be optimized sensibly" gotcha in Python, IMHO).


Alex


More information about the Python-3000 mailing list