为什么Pylint认为在条件值中使用len(SEQUENCE)不正确? [英] Why is the use of len(SEQUENCE) in condition values considered incorrect by Pylint?

查看:209
本文介绍了为什么Pylint认为在条件值中使用len(SEQUENCE)不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码段:

from os import walk

files = []
for (dirpath, _, filenames) in walk(mydir):
    # more code that modifies files
if len(files) == 0: # <-- C1801
    return None

Pylint对此消息感到震惊,该消息与if语句所在的行有关:

I was alarmed by Pylint with this message regarding the line with the if statement:


[pylint] C1801:请勿使用 len(SEQUENCE)作为条件值

乍一看,规则C1801在我看来并不十分合理,而参考指南中的定义不能解释为什么会出现此问题。实际上,它完全将其称为错误使用

The rule C1801, at first glance, did not sound very reasonable to me, and the definition on the reference guide does not explain why this is a problem. In fact, it downright calls it an incorrect use.


按条件使用(C1801
请勿将 len(SEQUENCE)用作条件值,当Pylint检测到条件内对len(sequence)的使用不正确时使用。

len-as-condition (C1801): Do not use len(SEQUENCE) as condition value Used when Pylint detects incorrect use of len(sequence) inside conditions.

我的搜索尝试也未能为我提供更深入的解释。我确实知道,序列的length属性可能会被懒惰地求值,并且 __ len __ 可以编程为具有副作用,但是令人怀疑的是,仅此对Pylint是否存在问题称这种用法不正确。因此,在我简单地将项目配置为忽略规则之前,我想知道我的推理中是否缺少某些内容。

My search attempts have also failed to provide me a deeper explanation. I do understand that a sequence's length property may be lazily evaluated, and that __len__ can be programmed to have side effects, but it is questionable whether that alone is problematic enough for Pylint to call such a use incorrect. Hence, before I simply configure my project to ignore the rule, I would like to know whether I am missing something in my reasoning.

何时使用 len(SEQ)作为条件值有问题吗? Pylint尝试使用C1801避免什么主要情况?

When is the use of len(SEQ) as a condition value problematic? What major situations is Pylint attempting to avoid with C1801?

推荐答案


何时使用 len(SEQ)作为条件值有问题吗? Pylint尝试使用C1801避免哪些主要的
情况?

When is the use of len(SEQ) as a condition value problematic? What major situations is Pylint attempting to avoid with C1801?

真的没有问题使用 len(SEQUENCE) –尽管可能效率不高(请参阅 chepner的评论认为在条件值中使用镜头序列不正确)。无论如何,Pylint都会检查代码是否符合 PEP 8样式指南,其中指出

It’s not really problematic to use len(SEQUENCE) – though it may not be as efficient (see chepner’s comment). Regardless, Pylint checks code for compliance with the PEP 8 style guide which states that


对于序列(字符串,列表,元组),请使用空序列为假的事实。

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

Yes: if not seq:
     if seq:

No:  if len(seq):
     if not len(seq):


作为偶尔在不同语言之间徘徊的Python程序员,我认为 len(SEQUENCE)的构造更具可读性和明确性(最好再隐式)。但是,使用一个空序列在布尔上下文中得出 False 的事实被认为更 Pythonic。

As an occasional Python programmer, who flits between languages, I’d consider the len(SEQUENCE) construct to be more readable and explicit ("Explicit is better then implicit"). However, using the fact that an empty sequence evaluates to False in a Boolean context is considered more "Pythonic".

这篇关于为什么Pylint认为在条件值中使用len(SEQUENCE)不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆