简体   繁体   English

pylint 用赋值表达式争论三元运算符

[英]pylint argues about ternary operator with assignment expression

I have some code like this:我有一些这样的代码:

return (
    (1 / a)
    if (a := foo())
    else 0
)

My pylint argues about this because "Using variable 'a' before assignment", even the evaluation order should be a := foo() first and then 1 / a or 0 .我的 pylint 对此争论不休,因为“在赋值之前使用变量 'a'”,甚至评估顺序也应该是a := foo() ,然后是1 / a0 I tried pip install --upgrade pylint , but it seems that pylint still do not agree this.我试过pip install --upgrade pylint ,但似乎 pylint 仍然不同意这一点。

OK, I find that this is an issue of Pylint: OK,我发现这是Pylint的问题:

https://github.com/PyCQA/pylint/issues/3347 https://github.com/PyCQA/pylint/issues/3347

"pylint can parse the walrus operator but we haven't actually implemented support for it." “pylint 可以解析 walrus 运算符,但我们实际上还没有实现对它的支持。” (21 Jan) (1月21日)

Anyway, I will modify the code to some equivalent versions which do not cause "Using variable before assignment", for example:无论如何,我会将代码修改为一些不会导致“在赋值前使用变量”的等效版本,例如:

if (a := foo()):
    return 1 / a
else:
    return 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM