简体   繁体   English

有人可以在 Python 中解释这种行为吗?

[英]Can someone explain this behavior in Python?

Consider the following interaction:考虑以下交互:

>>> def func():
...  pass
...
>>> id(func)
2138387939184
>>> def func():
...   x =  5
...
>>> id(func)
2138390016064
>>> def func():
...  pass
...
>>> id(func)
2138387939184
>>>
>>> def func2():
...  pass
...
>>> id(func2)
2138390016064

So you can see that after func is redefined to its original form: pass , it received the same memory address.所以你可以看到,在func被重新定义为原来的形式之后: pass ,它收到了相同的 memory 地址。 That got me thinking that when I define another function, no matter its name, if the body (and parameters list) are the same, it will be bound to the same address as well, but when I define func2 as only pass , it gets another address.这让我想到,当我定义另一个 function 时,无论它的名称如何,如果主体(和参数列表)相同,它也会绑定到相同的地址,但是当我将func2定义为仅pass时,它得到另一个地址。

Can someone explain this?有人可以解释一下吗?

EDIT编辑

My assumption was that the reason that when I defined我的假设是,当我定义

...
def func():
  pass

in the second time it received the same id, was that this function definition already exists in that memory address, so the interpreter doesn't have to recreate it.在第二次收到相同的 id 时,这个 function 定义已经存在于 memory 地址中,因此解释器不必重新创建它。 But given the following:但鉴于以下情况:

>>> def func():
...  pass
...
>>> id(func)
1829442649968
>>> def func():
...   x = 5
...
>>> id(func)
1829448396864
>>> def func():
...   y = 10
...
>>> id(func)
1829442649968

clearly shows that this thesis was wrong.清楚地表明这个论点是错误的。 It assigned the func object the same id only because it is now free.它为func object 分配了相同的 ID,只是因为它现在是免费的。

cf the documentation for the builtin id function : cf内置id function 的文档

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

You SHOULD not consider valid the id of objects that have been deleted.您不应该认为已删除对象的id是有效的。

Because you redefine your function func , you actually create a new object (functions are objects in Python) each time, that's why the id changed in the first place (because it is a new, different object, with a different id).因为你重新定义了你的 function func ,你实际上每次都创建一个新的 object (函数是 Python 中的对象),这就是为什么首先改变了 id (因为它是一个新的、不同的 ZA8CFDE68 不同的 931BD59B2ACZ666C) That the same id was reused later is coincidental, it may or not happen, and it tells you nothing useful (except for garbage collection implementation details).稍后重用相同的 id 是巧合,它可能会发生也可能不会发生,并且它不会告诉您任何有用的信息(垃圾收集实现细节除外)。

Comparing ids is safer when performed at the same time, for example if id(obj1) == id(obj2): print("we are sure it is the same object") .同时比较 id 更安全,例如if id(obj1) == id(obj2): print("we are sure it is the same object")

See also this very similar question: Why is the id of a Python class not unique when called quickly?另请参阅这个非常相似的问题: 为什么快速调用时 Python class 的 id 不是唯一的?

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

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