Python变量的打印内存地址[重复]

时间:2021-10-05 23:08:24

This question already has an answer here:

这个问题已经有了答案:

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中做这个?

3 个解决方案

#1


79  

id is the method you want to use: to convert it to hex:

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

hex(id(variable_here))

十六进制(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 int may 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.

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

#2


8  

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, id is already returning the address. If the only issue is how to format that integer in a certain way… it's the same as formatting any integer:

但是,如果您只关心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 ctypes or similar, in which case you should use ctypes.addressof or similar.

然而,这几乎从来都不是一个好的理由。如果您确实需要对象的地址,那么可以将其传递给ctype或类似的对象,在这种情况下,您应该使用ctype。addressof或类似。

#3


5  

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的内存地址。

#1


79  

id is the method you want to use: to convert it to hex:

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

hex(id(variable_here))

十六进制(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 int may 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.

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

#2


8  

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, id is already returning the address. If the only issue is how to format that integer in a certain way… it's the same as formatting any integer:

但是,如果您只关心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 ctypes or similar, in which case you should use ctypes.addressof or similar.

然而,这几乎从来都不是一个好的理由。如果您确实需要对象的地址,那么可以将其传递给ctype或类似的对象,在这种情况下,您应该使用ctype。addressof或类似。

#3


5  

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的内存地址。