[英]How to rewrite this code, such that PyCharm doesn't warn Local variable might be referenced before assignment?
Please consider请考虑
def foo():
bs = (4, 5)
for b in bs:
c = b + 1
return c
PyCharm marks the c
in the return c
with PyCharm 标记
return c
c
的 c
Local variable 'c' might be referenced before assignment
局部变量“c”可能在赋值前被引用
This is a good warning, usually, and I don't want to disable it.通常这是一个很好的警告,我不想禁用它。 (
#noqa
is not an answer) (
#noqa
不是答案)
In this case, however, it can be deduced before running that c always has a value.然而,在这种情况下,可以在运行之前推断出 c 始终具有值。
How to rewrite the code to help PyCharm understand this?如何重写代码来帮助PyCharm理解这个?
You can do what is done in most languages which is to instantiate c to a value of 0.你可以做大多数语言所做的事情,即将 c 实例化为 0 值。
def foo():
bs = (4, 5)
c = 0
for b in bs:
c = b + 1
return c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.