Python 为什么 Pylint 不喜欢内置函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3569134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't Pylint like built-in functions?
提问by igorgue
I have a line like this:
我有这样一行:
filter(lambda x: x == 1, [1, 1, 2])
Pylint is showing a warning:
Pylint 显示警告:
W: 3: Used builtin function 'filter'
Why is that? is a list comprehension the recommended method?
这是为什么?列表理解是推荐的方法吗?
Of course I can rewrite this like this:
当然,我可以这样重写:
[x for x in [1, 1, 2] if x == 1]
And I get no warnings, but I was wondering if there's a PEP for this?
我没有收到任何警告,但我想知道是否有针对此的 PEP?
采纳答案by Ned Batchelder
Pylint often chatters on about stuff it shouldn't. You can disable the warning in a .pylintrc file.
Pylint 经常喋喋不休地谈论不该谈论的事情。您可以在 .pylintrc 文件中禁用警告。
This page http://pylint-messages.wikidot.com/messages:w0141indicates the problem is that filter and map have been superseded by list comprehensions.
此页面http://pylint-messages.wikidot.com/messages:w0141表明问题在于过滤器和地图已被列表理解取代。
A line like this in your pylintrc file will quiet the warning:
在您的 pylintrc 文件中这样的一行将使警告安静下来:
disable=W0141
回答by benjamin
I ran into the same problem and could not figure out
我遇到了同样的问题,无法弄清楚
why the built-in function `input' is bad. I you intend
为什么内置函数“输入”不好。我你打算
to disable it:
禁用它:
pylint --bad-functions="[map,filter,apply]" YOUR_FILE_TO_CHECK_HERE
pylint --bad-functions="[map,filter,apply]" YOUR_FILE_TO_CHECK_HERE
Once you like the settings:
一旦你喜欢这些设置:
pylint --bad-functions="[map,filter,apply]" --some-other-supercool-settings-of-yours
--generate-rcfile > test.rc
Verify that your settings are in the file, e.g.:
验证您的设置是否在文件中,例如:
cat test.rc | grep -i YOUR_SETTING_HERE
After that you can use this file locally
之后就可以在本地使用这个文件了
pylint --rcfile test.rc --your-other-command-line-args ...
or even use it as your default rcfile. For this I kindly refer you to
或者甚至将它用作您的默认 rcfile。为此,我恳请您参考
pylint --long-help
回答by serv-inc
Why is that? is a list comprehension the recommended method?
这是为什么?列表理解是推荐的方法吗?
List comprehension is recommended in the tutorial example, which states
教程示例中推荐使用列表理解,其中指出
it's more concise and readable.
它更简洁易读。
and by most answerers on SO's Python List Comprehension Vs. Mapwhere it is
以及 SO 的Python List Comprehension Vs上的大多数回答者。地图它在哪里
- more efficientto use list comprehension than
filterif you are defining alambdaeach time - maybe more readable(and with similar efficiency) to use
filterif the function is pre-defined - necessary to use
filterandmapif you- map
map, - curry
map, or - use functional programming
- map
- 使用列表理解比每次
filter都定义一个更有效lambda - 如果函数是预定义的,则使用起来可能更易读(并且效率相似)
filter - 需要使用
filter,map如果你- 地图
map, - 咖喱
map, 或 - 使用函数式编程
- 地图
TL;DR: use list comprehension in most cases
TL;DR:在大多数情况下使用列表理解
回答by Jorge Continuous
I've got the same warning on my project. I'm changing the source code to be py2/3 compatible, and pylint helps a lot.
我的项目也收到了同样的警告。我正在将源代码更改为与 py2/3 兼容,而 pylint 有很大帮助。
Running pylint --py3kshows only errors about compatibility.
运行pylint --py3k仅显示有关兼容性的错误。
In python 2, if use filter, it returns a list:
在python 2中,如果使用filter,它返回一个list:
>>> my_list = filter(lambda x: x == 1, [1, 1, 2])
>>> my_list
[1, 1]
>>> type(my_list)
<type 'list'>
But in python 3, filterand other similar methods (map, range, zip, ..) return a iterator, that is incompatible types and perhaps cause bugs in your code.
但是在 python 3 中,filter其他类似的方法 ( map, range, zip, ..) 返回一个迭代器,这是不兼容的类型,可能会导致代码中的错误。
>>> my_list = filter(lambda x: x == 1, [1, 1, 2])
>>> my_list
<filter object at 0x10853ac50>
>>> type(my_list)
<class 'filter'>
To make your code python 2/3 compatible, I use a cheat sheet from python future site
为了使您的代码与 python 2/3 兼容,我使用了python 未来站点的备忘单
To avoid this warning, you can use 4 approaches, that works on python 2 and 3:
为避免此警告,您可以使用 4 种适用于 python 2 和 3 的方法:
1 - Using a list comprehension like you said.
1 - 像你说的那样使用列表理解。
2 - Using a listfunction, grant that return always is a materialized list, result is same on both python versions
2 - 使用list函数,授予 return 始终是物化列表,结果在两个 python 版本上相同
>>> list(filter(lambda x: x == 1, [1, 1, 2]))
[1, 1]
3 - Using lfilter, that's a future package import. It always return a list, uses filter on py2, and list(filter(..)on py3. So, both pythons got the same behaviour and you got a cleaner syntax.
3 - 使用lfilter,这是未来的包导入。它总是返回一个列表,在 py2 和list(filter(..)py3上使用过滤器。因此,两个 python 都具有相同的行为,并且您获得了更清晰的语法。
>>> from future.utils import lfilter
>>> lfilter(lambda x: x == 1, [1, 1, 2])
[1, 1]
4 - The best! Use filteralways on a loop, this way pylint don't give warnings, and it have a nice performance boost on python 3.
4 - 最好的!使用filter一个循环总是,这样pylint的不给予警告,并有蟒蛇3一个不错的性能提升。
>>> for number in filter(lambda x: x == 1, [1, 1, 2]):
>>> print(number)
>>> 1
>>> 1
Always prefer functions that works on python 3, because python 2 will be retired soon.
总是更喜欢在 python 3 上运行的函数,因为 python 2 很快就会退休。

