PEP 8:与 True 的比较应该是 'if cond is True:' 或 'if cond:' [英] PEP 8: comparison to True should be 'if cond is True:' or 'if cond:'

查看:174
本文介绍了PEP 8:与 True 的比较应该是 'if cond is True:' 或 'if cond:'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PyCharm 在执行 np.where(temp == True) 时发出警告

PyCharm is throwing a warning when I do np.where(temp == True)

我的完整代码:

from numpy import where, array

a = array([[0.4682], [0.5318]])
b = array([[0.29828851, 0., 0.28676873, 0., 0., 0., 0., 0.28801431, 0., 0., 0.71283046, 0.],
          [0.70171149, 0., 0.71323127, 0., 0., 0., 0., 0.71198569, 0., 0., 0.28716954, 0.]])

temp = b > 1.1*a
pos = where(temp == True)

print(pos) 

如果我按照其他帖子中的建议将 temp == True 更改为 temp 为 True,则代码无法按预期工作.

The code does not work as expected if I change temp == True to temp is True as suggested in other post.

应如何解决此警告?

where(temp) 有效.非常感谢 !!@若奥·维托里诺感谢您的解释,@jedwards.它有助于.

where(temp) works. Thanks a lot !! @Joao Vitorino Thanks for the explanation, @jedwards. It helps.

推荐答案

不要比较布尔值和布尔值.

Don't compare boolean against boolean.

你应该检查是真还是假.

You should check if is True or false.

b == true
if b: # If b is True
   do something 

就你而言

temp = b > 1.1*a
pos = where(temp)  

这里 一些解释

这篇关于PEP 8:与 True 的比较应该是 'if cond is True:' 或 'if cond:'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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