gpt4 book ai didi

python - 有没有办法让 PyLint 标志 'Yoda conditions' ?

转载 作者:行者123 更新时间:2023-11-28 21:50:22 24 4
gpt4 key购买 nike

在我正在处理的一些 Python 代码中,我在这里和那里发现了所谓的“Yoda conditions”。 '.

例子是:

if 0 < len(someList): ...

if None != ComputeSomething(): ...

有没有办法让 PyLint 标记它们?

最佳答案

Pylint 目前将此实现为约定检查 C0122,starting with v1.5.0 (2015-11-29) :

Add a new convention message, ‘misplaced-comparison-constant’, emitted when a constant is placed in the left hand side of a comparison, as in ‘5 == func()’. This is also called Yoda condition, since the flow of code reminds of the Star Wars green character, conditions usually encountered in languages with variabile assignments in conditional statements.

来自features documentation :

misplaced-comparison-constant (C0122):

Comparison should be %s Used when the constant is placed on the left side of a comparison. It is usually clearer in intent to place it in the right hand side of the comparison.

示例输出:

C:130, 3: Comparison should be __name__ == '__main__' (misplaced-comparison-constant)

关于python - 有没有办法让 PyLint 标志 'Yoda conditions' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32008209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com