lint warnings

A

Andrea Crotti

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?

2. Warning (W): Relative import 'parameters', should be 'othermodule.parameters'
if I am in the same directory why should I do a relative import, does it make any difference?

I think it's more clear/safe to just import the 'parameters', am I wrong?

Thanks,
Andrea
 
S

Steven D'Aprano

Andrea Crotti said:
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.
 
A

Andrea Crotti

Il giorno 15/feb/2011, alle ore 04.10, Ben Finney ha scritto:
The ‘map’ builtin is deprecated; using a list comprehension is neater
and more efficient.

Ok well it depends,
map(int, biglist)
is better than:
[int(x) for x in biglist]
at least for me.

Efficiency is probably not a big issue apparently, and it's really not important
until I see that this is a bottleneck.
It makes the code unnecessarily ambiguous; the person reading the code
can't tell that it's a relative import.

Yes but what if I move those two files (parameter and the importing module)
away, the import will continue to work if it's not relative to the upper directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top