使用id()时[]和list()之间有区别吗? [英] Is there a difference between [] and list() when using id()?

查看:46
本文介绍了使用id()时[]和list()之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释以下内容吗?

Can somebody explain the following?

为什么id一样,list不一样?

Why is the id the same, but the lists are different?

>>> [] is []
False
>>> id([]) == id([])
True

列表创建上有区别吗?

>>> id(list()) == id(list())
False
>>> id([]) == id([])
True

为什么会这样?我得到两个不同的列表.为什么不只有一个,或者三个或更多?

Why is this happening? I get two different lists. Why not only one, or three or more?

>>> [].__repr__
<method-wrapper '__repr__' of list object at 0x7fd2be868128>
>>> [].__repr__
<method-wrapper '__repr__' of list object at 0x7fd2be868170>
>>> [].__repr__
<method-wrapper '__repr__' of list object at 0x7fd2be868128>
>>> [].__repr__
<method-wrapper '__repr__' of list object at 0x7fd2be868170>

推荐答案

您错误地使用了 id(). id([])接受被立即丢弃的对象的内存ID.毕竟,一旦完成 id()后,就再也没有引用它了.因此,下次使用 id([])时,Python会发现有机会重用内存和lo和belook,这些地址的确是相同的.

You used id() wrong. id([]) takes the memory id of an object that is discarded immediately. After all, nothing is referencing it anymore once id() is done with it. So the next time you use id([]) Python sees an opportunity to re-use the memory and lo and behold, those addresses are indeed the same.

但是,这是一个实现细节,您不能依靠它,并且它也永远无法重用内存地址.

However, this is an implementation detail, one you can't rely on, and it won't always be able to reuse the memory address.

请注意,在对象的生存期内, id()值仅是唯一的 ,请参见

Note that id() values are only unique for the lifetime of the object, see the documentation:

这是一个整数,可以保证此对象在其生存期内唯一且恒定.两个没有重叠生命周期的对象可能具有相同的 id()值.

This is an 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(list())无法重复使用内存位置的原因可能是由于将当前帧推入堆栈以调用函数,然后弹出而导致的额外的堆突变当 list()调用返回时,它再次出现.

That id(list()) can't re-use the memory location is probably due to the extra heap mutations caused by pushing the current frame on the stack to call a function, then popping it again when the list() call returns.

[] list()都产生一个 new 空列表对象;但您需要首先创建对这些单独列表(此处为 a b )的引用:

Both [] and list() produce a new empty list object; but you need to first create references to those separate lists (here a and b):

>>> a, b = [], []
>>> a is b
False
>>> id(a) == id(b)
False
>>> a, b = list(), list()
>>> a is b
False
>>> id(a) == id(b)
False

使用 [] .__ repr __ 时也会发生同样的情况.Python交互式解释器具有一个特殊的全局名称 _ ,您可以使用它来引用最后生成的结果:

The same happens when you used [].__repr__. The Python interactive interpreter has a special global name, _, that you can use to reference the last result produced:

>>> [].__repr__
<method-wrapper '__repr__' of list object at 0x10e011608>
>>> _
<method-wrapper '__repr__' of list object at 0x10e011608>

这会创建一个额外的引用,因此 __ repr __ 方法以及您为其创建的空列表仍被认为是活动的.内存位置不会被释放,并且无法用于您创建的下一个列表.

That creates an extra reference, so the __repr__ method, and by extension, the empty list you created for it, are still considered active. The memory location is not freed and not available for the next list you create.

但是再次执行 [] .__ repr __ 时,Python现在将 _ 绑定到该新方法对象.突然以前的 __ repr __ 方法不再被任何东西引用,并且可以被释放,列表对象也可以被释放.

But executing [].__repr__ again, Python now binds _ to that new method object. Suddenly the previous __repr__ method is no longer referenced by anything and can be freed, and so is the list object.

第三次执行 [] .__ repr __ 时,第一个内存位置可再次使用,因此Python会这样做:

The third time you execute [].__repr__ the first memory location is available again for reuse, so Python does just that:

>>> [].__repr__  # create a new method
<method-wrapper '__repr__' of list object at 0x10e00cb08>
>>> _            # now _ points to the new method
<method-wrapper '__repr__' of list object at 0x10e00cb08>
>>> [].__repr__  # so the old address can be reused
<method-wrapper '__repr__' of list object at 0x10e011608>

您创建的列表绝不能超过两个;前一个(仍由 _ 引用)和当前一个.如果要查看更多的内存位置,请使用变量添加另一个引用.

You never create more than two lists; the previous one (still referenced by _) and the current one. If you wanted to see more memory locations, use variables to add another reference.

这篇关于使用id()时[]和list()之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆