Multi-isinstance idiom (Re: Deprecate tabs for indenting (was Re: Indenting with tabs vs spaces))

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sun Dec 9 16:52:43 EST 2001


Fri, 07 Dec 2001 14:42:27 +2328, Fernando Pérez <fperez528 at yahoo.com> pisze:

> Can you comment on list comprehensions vs. map? I tend to shy away 
> from the convenience of comprehensions because I'm afraid of their 
> cost and --at the expense of clarity-- pretty much anything can be 
> written with map().

I think (and a quick test confirms) that
    [<expression> for x in L]
is faster than
    map(lambda x: <expression>, L)
but
    [<function>(x) for x in L]
is slower than
    map(<function>, L)

IOW map is faster when you have the function ready and apply it for
the LC case, and LC is faster when you have the body ready and wrap
it in a function for the map case.

This may even be a reasonable guide for choosing either variant
for readability.

The dictinction is quite well-defined: usually you either begin with
an expression and can wrap it in a function, or begin with a function
and call it. There is no confusion between these cases, which may not
be obvious but it's true.

The only doubt is when you have *different* optimal sources for
the body and the function. For example Class.method as the function
wrt. x.method() as the body (of course they don't behave identically
but they can be exchangable in the given case), or the operator module
wrt. the expression with an operator. They are not comparable using
this criterion.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK



More information about the Python-list mailing list