对Python的id()感到困惑 [英] Confused about Python’s id()

查看:161
本文介绍了对Python的id()感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以理解以下定义:

每个对象都有一个标识,一个类型和一个值.对象的身份 一旦创建就永远不会改变;您可能会认为它是 对象在内存中的地址. is运算符比较 两个对象; id()函数返回一个整数,表示它的 身份.

Every object has an identity, a type and a value. An object's identity never changes once it has been created; you may think of it as the object’s address in memory. The is operator compares the identity of two objects; the id() function returns an integer representing its identity.

我认为上面的定义在创建某物"时起作用,例如:

I would assume the above definition works when "something" is created, such as:

>>> a = 0
>>> id(a)
1720438480

但是我不明白:

>>> id(1)
1720438512
>>> b = 1
>>> id(b)
1720438512

我还没有创建任何东西;那么整数"1"如何具有ID?这是否意味着一旦我在Python Shell中提及" 1,便立即将其分配给了一个内存地址?另外,这是否意味着因为ID在其生命周期内从未发生变化,并且由于我的计算机内存有限,所以如果我反复询问唯一事物的id(),我最终会得到类似内存不足"的消息吗? (它无法重新分配内存,因为其他对象的生命期尚未结束.)

I did not create anything yet; so how can the integer "1" have an ID? Does it mean that as soon as I "mention" 1 in the Python Shell, it is assigned to a memory address? Also, does it mean that because the ID never changes during its lifetime, and because my computer has a limited memory, if I repeatedly ask for the id() of unique things, I will eventually get something like "out of memory" message? (It cannot reallocate memory, because the lifetime of others have not ended yet.)

或者,从相反的方向显示我的耳朵:

Or, showing my ear from the other way round:

>>> id(something_1)
some unique memory address
>>> id(something_2)
some unique memory address
>>> ...

在什么时候重新分配内存?也就是说,在那一点上,

At which point is the memory reallocated? That is, at which point,

>>> my_variable = something_1
>>> id(my_variable)

会提供与id(something_1)不同的ID?

推荐答案

通常,一旦您使用整数或字符串或任何其他文字,Python就会为您在内存中创建一个新对象.保证对象的生存期具有相同的id,即其引用计数不为零.

In general, as soon as you use an integer or a string or any other literal, Python creates a new object in memory for you. It is guaranteed to have the same id for the lifetime of the object, that is, while its reference count is not zero.

当您编写类似的内容时:

When you write something like:

>>> id(1000)
140497411829680

Python创建整数1000并返回其id(CPython中对象的内存地址).完成此操作后,整数对象1000的引用计数为零,并将其删除.这样可以确保您不能仅通过编写id(something)(并且不将任何变量名绑定到对象)来保持填充内存.

Python creates the integer 1000 and returns its id (the memory address of the object in CPython). After this is done, the reference count of the integer object 1000 is zero and it is deleted. This ensures that you cannot keep filling memory just by writing id(something) (and not binding any variable name to the object).

通常,您无法预测何时会发生重用,但是在我的Python shell中,它会一致地发生:

Typically, you cannot predict when reuse will happen, but in my Python shell it happens quite consistently:

>>> id(1000)
140697307078576
>>> id(1001)
140697307078576
>>> id(1002)
140697307078576
>>> id(1003)
140697307078576

您可以看到在创建每个新整数时,一次又一次地使用相同的内存地址.但是,如果您防止引用计数下降到零,则可以看到使用了新的内存:

You can see that the same memory address get used again and again when each new integer is created. However, if you prevent the reference count from dropping to zero, you can see that new memory is used instead:

>>> a = 1000
>>> id(a)
140697307078576
>>> b = 1001
>>> id(b)
140697306008368

在CPython中,从-5到255的整数是特殊情况,因为它们一直存在(因此在Python运行时始终具有相同的id).这是为了避免重复创建和破坏常用整数而进行的优化.

In CPython, the integers -5 through to 255 are special cases in that they always exist (and so always have the same id during a Python runtime). This is an optimisation to avoid repeated creation and destruction of commonly-used integers.

这篇关于对Python的id()感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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