【发布时间】:2020-04-19 21:22:37
【问题描述】:
我在 Python 中遇到了一个奇怪的问题,其中评估涉及 np.linalg.norm 值的条件会导致问题。这是我的测试脚本:
import numpy as np
def isGoalReached():
start = np.array([4, 5, 6])
goal = np.array([17, 18, 19])
dist = np.linalg.norm(start - goal)
return (dist < 0.5)
print (isGoalReached())
print (isGoalReached() == 0)
print (isGoalReached() is False)
输出:
False
True
False
如果我删除 np.linalg.norm 部分,然后重写:
import numpy as np
def isGoalReached():
dist = 123.456
return (dist < 0.5)
print (isGoalReached())
print (isGoalReached() == 0)
print (isGoalReached() is False)
输出:
False
True
True
是什么导致了这种非常奇怪的行为?我在 Python 3.7.3 (Anaconda)
【问题讨论】:
-
确实很奇怪,但与您所说的相反,这不是问题,因为您根本不应该比较布尔值(通过
is或其他方式)。 -
其中一个是 Python bool,另一个是
numpy.bool_。它们的类型不同。 -
我只能看到Truth Value Testing文档中的正常行为。