打印 Python 变量的内存地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16408472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:32:41  来源:igfitidea点击:

print memory address of Python variable

pythonmemory-address

提问by brno792

How do I print the memory address of a variable in Python 2.7? I know id() returns the 'id' of a variable or object, but this doesn't return the expected 0x3357e182 style I was expecting to see for a memory address. I want to do something like print &x, where x is a C++ int variable for example. How can I do this in Python?

如何在 Python 2.7 中打印变量的内存地址?我知道 id() 返回变量或对象的“id”,但这不会返回我期望看到的内存地址的预期 0x3357e182 样式。我想做类似的事情,例如print &x,其中 x 是 C++ int 变量。我怎样才能在 Python 中做到这一点?

采纳答案by BlackVegetable

idis the method you want to use: to convert it to hex:

id是您要使用的方法:将其转换为十六进制:

hex(id(variable_here))

hex(id(variable_here))

For instance:

例如:

x = 4
print hex(id(x))

Gave me:

给我:

0x9cf10c

Which is what you want, right?

哪个是你想要的,对吧?

(Fun fact, binding two variables to the same intmay result in the same memory address being used.)
Try:

(有趣的事实是,将两个变量绑定到相同的变量int可能会导致使用相同的内存地址。)
尝试:

x = 4
y = 4
w = 9999
v = 9999
a = 12345678
b = 12345678
print hex(id(x))
print hex(id(y))
print hex(id(w))
print hex(id(v))
print hex(id(a))
print hex(id(b))

This gave me identical pairs, even for the large integers.

这给了我相同的对,即使对于大整数也是如此。

回答by Jonathan

According to the manual, in CPython id()is the actual memory address of the variable. If you want it in hex format, call hex()on it.

根据手册,在 CPython 中id()是变量的实际内存地址。如果您想要十六进制格式,请调用hex()它。

x = 5
print hex(id(x))

this will print the memory address of x.

这将打印 x 的内存地址。

回答by abarnert

There is no way to get the memory address of a value in Python 2.7 in general. In Jython or PyPy, the implementation doesn't even know your value's address (and there's not even a guarantee that it will stay in the same place—e.g., the garbage collector is allowed to move it around if it wants).

通常,在 Python 2.7 中无法获取值的内存地址。在 Jython 或 PyPy 中,实现甚至不知道你的值的地址(甚至不能保证它会留在同一个地方——例如,如果需要,垃圾收集器可以移动它)。

However, if you only care about CPython, idis already returning the address. If the only issue is how to format that integer in a certain way… it's the same as formatting anyinteger:

但是,如果您只关心 CPython,id则已经返回了地址。如果唯一的问题是如何以某种方式格式化该整数……这与格式化任何整数相同:

>>> hex(33)
0x21
>>> '{:#010x}'.format(33) # 32-bit
0x00000021
>>> '{:#018x}'.format(33) # 64-bit
0x0000000000000021

… and so on.

… 等等。

However, there's almost never a good reason for this. If you actually need the address of an object, it's presumably to pass it to ctypesor similar, in which case you should use ctypes.addressofor similar.

然而,这几乎从来没有一个很好的理由。如果您确实需要一个对象的地址,大概是将它传递给ctypes或类似,在这种情况下,您应该使用ctypes.addressof或类似。