lint warnings

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Feb 15 04:58:07 CET 2011


On Tue, 15 Feb 2011 14:10:38 +1100, Ben Finney wrote:

> Andrea Crotti <andrea.crotti.0 at gmail.com> writes:
> 
>> I work on emacs with flymake activated and pylint, pyflakes and pep8
>> running in background to notify for some style problems.
>>
>> Now there are at a couple of pylint warnings which I don't understand
>> 1. Warning (W, filter_enums): Used builtin function 'map' [2 times]
>> what is the problem with using map and other builtin functions?
> 
> The ‘map’ builtin is deprecated; 

I don't believe it is. Do you have any evidence for this claim?


> using a list comprehension is neater and more efficient.

The first is a matter of opinion, the second is demonstrably untrue.


Testing in Python 3.1, there is no significant difference when the 
function is a pure Python function, although map is slightly faster:


>>> from timeit import Timer
>>> t1 = Timer('[f(x) for x in range(1000)]', 'def f(x): return x+1')
>>> t2 = Timer('list(map(f, range(1000)))', 'def f(x): return x+1')
>>> t1.timeit(number=100)
0.09910106658935547
>>> t2.timeit(number=100)
0.08968997001647949
>>> t1.timeit(number=1000)
0.9915580749511719
>>> t2.timeit(number=1000)
0.9404010772705078


If the function is a built-in written in C, map can be significantly 
faster:

>>> t1 = Timer('[len(s) for s in "a"*1000]', '')
>>> t2 = Timer('list(map(len, "a"*1000))', '')
>>> t1.timeit(number=100)
0.0598909854888916
>>> t2.timeit(number=100)
0.02748703956604004
>>> t1.timeit(number=10000)
3.6018471717834473
>>> t2.timeit(number=10000)
1.8807408809661865


The only time list comps are faster is if you have a expression which can 
be executed in-line in the comprehension, but needs to be written as a 
Python function in map. And even then, the difference is just a 
multiplicative constant, not a Big Oh difference.



-- 
Steven



More information about the Python-list mailing list