PEP 8:与True的比较应为“if cond is True:”或“if cond:”

2024-04-29 11:41:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我这么做的时候PyCharm会发出警告np.哪里(温度==真)

我的完整代码:

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 is True,那么代码将无法按预期工作。在

应如何解决此警告?在

在(临时)工作的地方。谢谢!!@乔奥·维托里诺 谢谢你的解释,杰德沃兹。这有帮助。在


Tags: 代码fromposimportnumpytrue警告is
2条回答

根据Python中的PEP8指南,将事物与真进行比较并不是一种首选模式。在

temp = True
pcos = where(temp)

如果将“temp”指定为false,则仍在条件语句中仅指定“temp”将导致True。例如:

^{pr2}$

注意:如果您的代码没有遵循PEP8,这个例子不会给出任何错误。在

不要比较布尔值和布尔值。在

你应该检查一下是真是假。在

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

对你来说

^{pr2}$

Here一些解释

相关问题 更多 >