简体   繁体   English

Python:'inf 是 inf',但 '-inf 不是 -inf'?

[英]Python: 'inf is inf', but '-inf is not -inf'?

Python 3.7 Python 3.7

While writing the search code for the maximum, I encountered a strange behavior of negative infinity.在编写最大值的搜索代码时,我遇到了负无穷大的奇怪行为。 Can somebody explain why this behavior?有人可以解释为什么这种行为吗?

>>> inf = float('inf')
>>> inf is inf
True
>>> (-inf) is (-inf)
False

And I've already realized it's better to use == for comparison, but I'm interested in the answer to the question above.而且我已经意识到使用==进行比较会更好,但我对上述问题的答案很感兴趣。

inf is a variable, bound to a specific object. inf是一个变量,绑定到特定的 object。 Any object is itself, so inf is inf .任何 object is它自己,所以inf is inf

-inf is an expression. -inf是一个表达式。 It does math, and produces an object with value floating-point negative infinity.它进行数学运算,并产生一个 object 值浮点负无穷大。 Python makes no promises about whether this will be the same object as any other object with that value. Python 不承诺这是否与具有该值的任何其他 object 相同。 In your case, the two evaluations of -inf happened to produce different objects.在您的情况下, -inf的两个评估碰巧产生了不同的对象。

Again, there are no promises about what -inf is -inf will produce.同样,没有关于 -inf 是什么的承诺-inf is -inf会产生什么。 The current CPython implementation happens to consistently produce False.当前的 CPython 实现恰好始终产生 False。 PyPy's primitive handling produces True . PyPy 的原始处理产生 True A different Python version or implementation might inconsistently produce True or False based on current memory pressure, or whether you ran this as a script or interactively, or any other factor.不同的 Python 版本或实现可能会根据当前的 memory 压力,或者您是否将其作为脚本或交互方式或任何其他因素运行,不一致地产生 True 或 False。 CPython itself already has cases where object identity is different in a script or interactively; CPython 本身已经存在 object 身份在脚本或交互方式中不同的情况; for example, this:例如,这个:

x = 1000
y = 1000
print(x is y)

prints different things in the current CPython implementation depending on whether you run it interactively.根据您是否以交互方式运行它,在当前 CPython 实现中打印不同的内容。

-inf is an operation that produces a new float object, and you use it twice in the same expression. -inf是一个产生新float object 的操作,您在同一个表达式中使用了两次。 Two distinct objects are produced, because a Python implementation is not required to notice that both subexpressions could return a reference to the same object.产生两个不同的对象,因为 Python 实现不需要注意两个子表达式可以返回对相同 object 的引用。

I want to add one point: in Python, when we use id() function, is operator, == operator, etc, the real target we're working with is value , not variable .我想补充一点:在 Python 中,当我们使用id() function, is operator, == operator 等时,我们使用的真正目标是value ,而不是variable

a is b equals to id(a) == id(b) . a is b等于id(a) == id(b)

In your code, inf = float('inf') will produce a value named inf .在您的代码中, inf = float('inf')将产生一个名为inf的值。

inf is also an expression, which will be evaluated to a value. inf也是一个表达式,它将被评估为一个值。

inf is inf will be True , because the evaluated values of two expressions are same one, in fact. inf is inf将是True ,因为实际上两个表达式的评估值是相同的。

In Python, focusing on values is better and easier.在 Python 中,专注于价值观更好更容易。

For example, in CPython implementation:例如,在 CPython 实现中:

a = 1
b = 1
a is b # will be true

a = 10000
b = 10000
a is b # will be false

a = '10000'
b = '10000'
a is b # will be true

If we focus on variable, such thing is weird.如果我们专注于变量,这样的事情就很奇怪。 If we focus on values, and if I tell you that CPython implementation has cache pool, which is used for small int and some literal str value, such thing is simple again:如果我们专注于值,如果我告诉你 CPython 实现有缓存池,它用于小 int 和一些文字 str 值,那么事情就很简单了:

Because of cache pool, small int 1 is cached, str '10000' is cached too, but big int 10000 is not cached so it will be created two times.因为缓存池的原因,small int 1被缓存了,str '10000'也被缓存了,而 big int 10000没有被缓存,所以会被创建两次。

Try to focus on values.尝试专注于价值观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM