Why doesn't small integer caching seem to work with int objects from the round() function in Python 3?

Cha*_*mod 8 python integer python-3.x

Can you please explain why this happens in Python v3.8?

a=round(2.3)
b=round(2.4)

print(a,b)
print(type(a),type(b))

print(a is b)
print(id(a))
print(id(b))
Run Code Online (Sandbox Code Playgroud)

Output:

2 2
<class 'int'> <class 'int'>
False
2406701496848
2406701496656
>>>
Run Code Online (Sandbox Code Playgroud)

2 is within the range of the small integer caching. So why are there different objects with the same value?

Sha*_*ger 9

看起来像在 3.8 中,PyLong_FromDouble(这是float.__round__最终委托给的)显式分配一个新PyLong对象并手动填充它,而不对其进行规范化(通过IS_SMALL_INT检查和get_small_int缓存查找功能),因此它不会检查小int缓存以解析为规范值。

由于问题 37986:提高 PyLong_FromDouble() 的性能,这将在 3.9 中发生变化,现在它委托给PyLong_FromLongdouble足够小以无损地表示为 C 时long。作为副作用,这将使用小int缓存,因为可以PyLong_FromLong可靠地对小值进行标准化。