简体   繁体   English

为什么id()在IronPython上的结果不等于id()在Python上的结果?

[英]Why doesn't id()'s result on IronPython equal to id()'s result on Python?

I really confuse that a behavior of id() function on IronPython who differ from Python. 我真的很混淆IronPython上与Python不同的id()函数行为。 Let me show you the following code, 让我告诉你以下代码,

In IronPython: 在IronPython中:

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

As you can see that, id()'s result is changed in every calls. 正如您所看到的,id()的结果在每次调用时都会更改。 In Python, however, not be changed. 但是,在Python中,不能更改。

In Python: 在Python中:

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

I know id() function returns an object identification number(ID) of its single parameter. 我知道id()函数返回其单个参数的对象标识号(ID)。 Why does two python interpreters give me a different result? 为什么两个python解释器给我一个不同的结果?

CPython has a cache of constant small integers that are used whenever needed. CPython有一个常量小整数的缓存,可以在需要时使用。 This pool of integers is an optimisation and improves the performance because a new object does not need to be allocated for every small integer as needed. 该整数池是一种优化,可以提高性能,因为不需要根据需要为每个小整数分配新对象。 Evidently IronPython handles this differently. 显然,IronPython以不同的方式处理这个问题。

That said, the id() function returns a number which uniquely identifies an object. 也就是说, id()函数返回唯一标识对象的数字。 You can use this to see whether two names are bound to the same object. 您可以使用它来查看两个名称是否绑定到同一个对象。 You cannot use this value to see whether two objects are "equal" in any sense. 您无法使用此值来查看两个对象在任何意义上是否“相等”。

http://docs.python.org/library/functions.html#id http://docs.python.org/library/functions.html#id

Return the “identity” of an object. 返回对象的“身份”。 This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. 这是一个整数(或长整数),在该对象的生存期内,此整数保证是唯一且恒定的。 Two objects with non-overlapping lifetimes may have the same id() value. 具有不重叠生存期的两个对象可能具有相同的id()值。

Python implementation detail: This is the address of the object in memory. Python实现细节:这是内存中对象的地址。

Why do you think ids would be constant? 您为什么认为id是恒定的?

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

相关问题 python对象是相同的,具有不同的id - python objects are equal with different id's 为什么 Python 的 len(readlines) 不等于 Bash 的 'wc -l' 命令? - Why doesn't Python's len(readlines) equal Bash's 'wc -l' command? 为什么 Scipy 的 curve_fit 不能为以下问题给出正确的结果? - Why doesn't Scipy's curve_fit give the correct result for the following problem(s)? scipy.sparse.linalg.eigsh()与Matlab的eigs()给出的结果不同,为什么呢? - scipy.sparse.linalg.eigsh() doesn't give out the same result as Matlab's eigs(), why? 循环内的Python超链接不会认为JSON数据的ID不同 - Python hyperlink inside a loop doesn't loop thought the different id`s of JSON data 为什么我的Python函数不返回结果? - Why my Python function doesn't return the result? Python 评论:为什么代码 1 产生的结果与代码 2 不同? - Python Review: Why doesn't code 1 produce same result as code 2? 为什么此Python表达式无法产生预期的结果? - Why this Python expression doesn't produce expected result? Django的OneToOneField不是引用另一个表的id字段吗? - Doesn't OneToOneField of Django refer another table's id field? 为什么sha3在不同的python模块中给出不同的结果? - Why sha3 in different python's modules gives different result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM