在实践中,我可以在多大程度上依靠该对象的id()及其唯一性?例如。:

  • id(a) == id(b)a is b还是反之?那相反呢?
  • id保存在某个地方以便以后使用(例如,保存到某个注册表中而不是对象本身)有多安全?

  • (作为对Canonicals for Python: are objects with the same id() the same object, `is` operator, unbound method objects的建议规范编写)

    最佳答案

    根据 id() documentation,只能保证id是唯一的

  • 用于特定对象的生命周期,
  • 特定解释器实例中的

  • 因此,比较id是不安全的,除非您还以某种方式确保在比较时两个被取了id的对象仍然处于 Activity 状态(并且与同一Python解释器实例相关联,但是您需要真正尝试使其变为错误)。

    正是is所做的-使得比较id是多余的。如果您出于某种原因不能使用is语法,那么总会有 operator.is_

    现在,在比较时对象是否仍然存在并不总是显而易见的(有时非常不明显):
  • 访问某些属性(例如bound methods of an object)每次都会创建一个新对象。 因此,结果的id在每个属性访问上可能相同,也可能不同。

    例子:
    >>> class C(object): pass
    >>> c=C()
    >>> c.a=1
    
    >>> c.a is c.a
    True        # same object each time
    
    >>> c.__init__ is c.__init__
    False       # a different object each time
    
    # The above two are not the only possible cases.
    # An attribute may be implemented to sometimes return the same object
    # and sometimes a different one:
    @property
    def page(self):
        if check_for_new_version():
            self._page=get_new_version()
        return self._page
    
  • 如果对象是由于计算表达式而创建的,并且未保存在任何地方,则将立即丢弃它, 1,之后创建的任何对象都可以使用其id
  • 在同一代码行中甚至如此。例如。 id(create_foo()) == id(create_bar())的结果是不确定的。

    例子:
    >>> id([])     #the list object is discarded when id() returns
    39733320L
    >>> id([])     #a new, unrelated object is created (and discarded, too)
    39733320L      #its id can happen to be the same
    >>> id([[]])
    39733640L      #or not
    >>> id([])
    39733640L      #you never really know
    


  • 由于比较id时存在上述安全要求,因此保存id而不是对象不是很有用,因为无论如何您都必须保存对对象本身的引用-以确保它保持 Activity 状态。也没有任何性能提升: is implementation is as simple as comparing pointers

    最后,作为内部优化(和实现细节,因此实现和发行版之间可能会有所不同),CPython重用了一些经常使用的不可变类型的简单对象。在撰写本文时,其中包括small integerssome strings。因此,即使您从不同的地方获得它们,它们的id也会重合。

    这(从技术上)没有违反上述id()文档的唯一性保证:重用的对象在所有重用中均保持 Activity 状态。

    这也不是什么大问题,因为两个变量是否指向同一个对象只是知道该对象是否可变的:if two variables point to the same mutable object, mutating one will (unexpectedly) change the other, too。不可变类型没有这个问题,因此对于它们来说,两个变量指向两个相同的对象还是指向同一对象都没有关系。

    1有时,这称为“未命名的表达式”。

    关于python - id()与 `is`运算符。比较 `id`是否安全?相同的 `id`表示相同的对象吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52268343/

    10-11 19:19
    查看更多