为什么小整数缓存似乎不能与 Python 3 中的 round() 函数中的 int 对象一起使用?
Why doesn't small integer caching seem to work with int objects from the round() function in Python 3?
你能解释一下为什么 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))
输出:
2 2
<class 'int'> <class 'int'>
False
2406701496848
2406701496656
>>>
2在小整数缓存范围内。那么为什么会有不同的对象具有相同的值呢?
看起来像在 3.8 中,PyLong_FromDouble
(这是 float.__round__
最终委托给的)explicitly allocates a new PyLong
object and fills it in manually,没有对其进行规范化(通过 IS_SMALL_INT
检查和 get_small_int
缓存查找函数),因此它不会检查小 int
缓存以解析为规范值。
由于 issue 37986: Improve perfomance of PyLong_FromDouble(),这将在 3.9 中发生变化,当 double
小到足以无损地表示为 C [=] 时,它现在委托给 PyLong_FromLong
18=]。通过 side-effect,这将使用小 int
缓存,因为 PyLong_FromLong
可靠地规范化小值。
你能解释一下为什么 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))
输出:
2 2
<class 'int'> <class 'int'>
False
2406701496848
2406701496656
>>>
2在小整数缓存范围内。那么为什么会有不同的对象具有相同的值呢?
看起来像在 3.8 中,PyLong_FromDouble
(这是 float.__round__
最终委托给的)explicitly allocates a new PyLong
object and fills it in manually,没有对其进行规范化(通过 IS_SMALL_INT
检查和 get_small_int
缓存查找函数),因此它不会检查小 int
缓存以解析为规范值。
由于 issue 37986: Improve perfomance of PyLong_FromDouble(),这将在 3.9 中发生变化,当 double
小到足以无损地表示为 C [=] 时,它现在委托给 PyLong_FromLong
18=]。通过 side-effect,这将使用小 int
缓存,因为 PyLong_FromLong
可靠地规范化小值。