pylint: What's wrong with the builtin map()

T

Tuomas

#!/usr/bin/python
"""test pydev_0.9.3/../pylint"""
__revision__ = "test_mod 0.1 by TV 06/10/22"

lst = ['aaa', ' bbb', '\tccc\n']
lst = map(lambda x: x.strip(), lst)

result = """
No config file found, using default configuration
************* Module test_mod
W: 6: Used builtin function 'map'
E: 6: Using variable 'x' before assigment
....
"""
 
G

Georg Brandl

Tuomas said:
#!/usr/bin/python
"""test pydev_0.9.3/../pylint"""
__revision__ = "test_mod 0.1 by TV 06/10/22"

lst = ['aaa', ' bbb', '\tccc\n']
lst = map(lambda x: x.strip(), lst)

result = """
No config file found, using default configuration
************* Module test_mod
W: 6: Used builtin function 'map'
E: 6: Using variable 'x' before assigment

Some people think that all occurences of map() must be replaced
by list comprehensions. The designer of pylint seems to be
one of those.

Georg
 
F

Fredrik Lundh

Tuomas said:
> lst = map(lambda x: x.strip(), lst)

list comprehensions are more efficient than map/lambda combinations;
the above is better written as:

lst = [x.strip() for x in lst]

in general, map() works best when the callable is an existing function
(especially if it's a built-in). not sure if pylist is smart enough to
tell the difference, though.

</F>
 
T

Tuomas

Georg said:
Some people think that all occurences of map() must be replaced
by list comprehensions. The designer of pylint seems to be
one of those.

So it seems, but why? Formally spoken we ase "using variable 'x' before
assigment" in the comprehension too.

#!/usr/bin/python
"""test pydev_0.9.3/../pylint"""
__revision__ = "test_mod 0.2 by TV 06/10/22"

lst = ['aaa', ' bbb', '\tccc\n']
# lst = (lambda x: x.strip(), lst) # revision 0.1
lst = [x.strip() for x in lst]

result = """revision 0.1:
No config file found, using default configuration
************* Module test_mod
W: 6: Used builtin function 'map'
E: 6: Using variable 'x' before assigment
....

revision 0.2:
....
Your code has been rated at 10.00/10 (previous run: -10.00/10)
"""
 
G

Georg Brandl

Tuomas said:
So it seems, but why?

See Fredrik's post. There's no error in the expression with map(),
it's just less effective than a LC, but only if used with a
lambda.
Formally spoken we ase "using variable 'x' before
assigment" in the comprehension too.

That's another issue. I'd suggest a bug in pylint. (You probably
have a variable called "x" later in the file.

My version of pylint (0.12.1) doesn't show this "error".

Georg
 
R

Robert Kern

Tuomas said:
#!/usr/bin/python
"""test pydev_0.9.3/../pylint"""
__revision__ = "test_mod 0.1 by TV 06/10/22"

lst = ['aaa', ' bbb', '\tccc\n']
lst = map(lambda x: x.strip(), lst)

result = """
No config file found, using default configuration
************* Module test_mod
W: 6: Used builtin function 'map'
E: 6: Using variable 'x' before assigment
...
"""

In addition to what the others said, note that pylint is entirely configurable.
If you don't want to be bothered with warnings about map() which you are going
to ignore, a simple modification to the pylint configuration file will prevent
it from doing that check.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top