> a = len(s) >-6ren">
gpt4 book ai didi

python - 为什么 id( )'s result on IronPython equal to id()' s 在 Python 上没有结果?

转载 作者:行者123 更新时间:2023-11-30 23:49:12 24 4
gpt4 key购买 nike

我真的很困惑 IronPython 上 id() 函数的行为与 Python 不同。让我向您展示以下代码,

在 IronPython 中:

>> s = "hello"
>> a = len(s)
>> id(a)
44
>> a = len(s)
>> id(a)
45

正如你所看到的,id()的结果在每次调用时都会改变。但在 Python 中,不能更改。

在Python中:

>> s = "hello"
>> a = len(s)
>> id(a)
2633845
>> a = len(s)
>> id(a)
2633845

我知道 id() 函数返回其单个参数的对象标识号(ID)。为什么两个 python 解释器给出不同的结果?

最佳答案

CPython 有一个常量小整数缓存,可以在需要时使用。这个整数池是一种优化,可以提高性能,因为不需要根据需要为每个小整数分配一个新对象。显然 IronPython 的处理方式有所不同。

也就是说,id() 函数返回一个唯一标识对象的数字。您可以使用它来查看两个名称是否绑定(bind)到同一个对象。您无法使用此值来查看两个对象在任何意义上是否“相等”。

关于python - 为什么 id( )'s result on IronPython equal to id()' s 在 Python 上没有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7751132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com