为什么 Pylint 不喜欢内置函数?

发布于 2024-09-15 15:53:40 字数 301 浏览 5 评论 0原文

我有这样一行:

filter(lambda x: x == 1, [1, 1, 2])

Pylint 显示警告:

W:  3: Used builtin function 'filter'

为什么会这样?列表理解是推荐的方法吗?

当然,我可以这样重写:

[x for x in [1, 1, 2] if x == 1]

我没有收到任何警告,但我想知道是否有 PEP?

I have a line like this:

filter(lambda x: x == 1, [1, 1, 2])

Pylint is showing a warning:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月棠 2024-09-22 15:53:40

Pylint 经常闲聊一些不该闲聊的事情。您可以在 .pylintrc 文件中禁用警告。

此页面 http://pylint-messages.wikidot.com/messages:w0141 指示问题是过滤器和映射已被列表推导式取代。

pylintrc 文件中这样的一行将消除警告:

disable=W0141

Pylint often chatters on about stuff it shouldn't. You can disable the warning in a .pylintrc file.

This page http://pylint-messages.wikidot.com/messages:w0141 indicates the problem is that filter and map have been superseded by list comprehensions.

A line like this in your pylintrc file will quiet the warning:

disable=W0141
酒中人 2024-09-22 15:53:40

这是为什么呢?列表理解是推荐的方法吗?

教程示例中建议使用列表理解,其中指出

它更加简洁和可读。

以及 SO 的 Python 列表理解 Vs 的大多数回答者。映射,其中

  1. 使用列表理解比过滤器更有效 如果你每次都定义一个 lambda
  2. 那么如果函数是 pre 的话,使用 filter 可能会更具可读性(并且具有相似的效率) - 定义
  3. 需要使用 filtermap 如果您
    • 地图地图
    • 咖喱地图,或
    • 使用函数式编程

TL;DR:在大多数情况下使用列表理解

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. Map where it is

  1. more efficient to use list comprehension than filter if you are defining a lambda each time
  2. maybe more readable (and with similar efficiency) to use filter if the function is pre-defined
  3. necessary to use filter and map if you
    • map map,
    • curry map, or
    • use functional programming

TL;DR: use list comprehension in most cases

计㈡愣 2024-09-22 15:53:40

我遇到了同样的问题,但无法弄清楚

为什么内置函数“input”不好。我你打算

禁用它:

pylint --bad-functions="[映射、过滤器、应用]" YOUR_FILE_TO_CHECK_HERE

一旦您喜欢这些设置:

pylint --bad-functions="[map,filter,apply]" --some-other-supercool-settings-of-yours
--generate-rcfile > test.rc

验证您的设置是否在文件中,例如:

cat test.rc | grep -i YOUR_SETTING_HERE

之后您可以在本地使用此文件

pylint --rcfile test.rc --your-other-command-line-args ...

,甚至将其用作默认 rcfile 。为此,我请您参考

pylint --long-help

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

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

pylint --long-help
少女净妖师 2024-09-22 15:53:40

我的项目也收到同样的警告。我正在更改源代码以兼容 py2/3,并且 pylint 有很大帮助。

运行 pylint --py3k 仅显示有关兼容性的错误。

在python 2中,如果使用filter,它返回一个list

>>> my_list = filter(lambda x: x == 1, [1, 1, 2])
>>> my_list
[1, 1]
>>> type(my_list)
<type 'list'>

但是在python 3中,filter和其他类似的方法(map< /code>, range, zip, ..) 返回一个迭代器,这是不兼容的类型,可能会导致代码中出现错误。

>>> my_list = filter(lambda x: x == 1, [1, 1, 2])
>>> my_list
<filter object at 0x10853ac50>
>>> type(my_list)
<class 'filter'>

中的备忘单

为了使您的代码与 python 2/3 兼容,我使用了 python future site 避免此警告,您可以使用 4 种方法,适用于 python 2 和 3:

1 - 使用您所说的列表理解。

2 - 使用 list 函数,确保返回始终是物化列表,两个 python 版本的结果相同

>>> list(filter(lambda x: x == 1, [1, 1, 2]))
[1, 1]

3 - 使用 lfilter,这是未来的包导入。它总是返回一个列表,在 py2 上使用过滤器,在 py3 上使用 list(filter(..)。因此,两个 python 都有相同的行为,并且您得到了更清晰的语法。4

>>> from future.utils import lfilter
>>> lfilter(lambda x: x == 1, [1, 1, 2])
[1, 1]

- 最好的!使用filter 始终处于循环状态,这样 pylint 就不会发出警告,并且它在 python 3 上有很好的性能提升。

>>> for number in filter(lambda x: x == 1, [1, 1, 2]):
>>>     print(number)
>>> 1
>>> 1

总是更喜欢在 python 3 上工作的函数,因为 python 2 很快就会退役。

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.

Running pylint --py3k shows only errors about compatibility.

In python 2, if use filter, it returns a list:

>>> my_list = filter(lambda x: x == 1, [1, 1, 2])
>>> my_list
[1, 1]
>>> type(my_list)
<type 'list'>

But in python 3, filter and other similar methods (map, range, zip, ..) return a iterator, that is incompatible types and perhaps cause bugs in your code.

>>> 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

To avoid this warning, you can use 4 approaches, that works on python 2 and 3:

1 - Using a list comprehension like you said.

2 - Using a list function, grant that return always is a materialized list, result is same on both python versions

>>> 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.

>>> from future.utils import lfilter
>>> lfilter(lambda x: x == 1, [1, 1, 2])
[1, 1]

4 - The best! Use filter always on a loop, this way pylint don't give warnings, and it have a nice performance boost on python 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文