这是我的代码段:
def my_function(x, y):
...
xyz = 0
try:
do_something
xyz = 1
except (SomeException) as e:
do_somethingelse
if condition A happens:
xyz = 2
else:
xyz = 0
if xyz == 0:
print("Case 1 happened")
elif xyz == 1:
print("Case 2 happened")
else:
print("Case 3 happened")
return xyz
因此,尽管我使用xyz
甚至将其用作返回参数,但在PyCharm中却收到一条警告消息,内容为:Local variable 'xyz' value is not used
。是什么原因,如何解决此警告?
答案 0 :(得分:1)
可以省略第一个xyz = 0
,因为在下一个代码块中,您正在重新定义其值。
它将全部按预期进行,并且xyz
将设置为1
:
try:
do_something
xyz = 1
否则它将捕获异常并将其设置为2
或0
:
except (SomeException) as e:
do_somethingelse
if condition A happens:
xyz = 2
else:
xyz = 0