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?

- 12,594
- 8
- 50
- 82

- 6,479
- 17
- 54
- 71
-
7Note that `id` isn't a pointer at all in PyPy or Jython (not sure about IronPython); this is a CPython-specific thing. More importantly… why do you want the address? Any realistic use case will probably involve `ctypes`, in which case you want to use `ctypes` to get the address in a format that you can pass to C code, not `id`. – abarnert May 06 '13 at 22:55
3 Answers
id
is the method you want to use: to convert it to hex:
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 int
may result in the same memory address being used.)
Try:
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.

- 130
- 6

- 12,594
- 8
- 50
- 82
-
9I think that this is CPython specific. There's also `addressof` from [ctype](http://docs.python.org/2/library/ctypes.html#ctypes.addressof) – nvlass May 06 '13 at 22:54
-
4
-
1
-
-
1@Daenyth: Typing the same literal repeatedly may give you the same object each time even if they're not small ints or short strings (although it's not guaranteed to). The "small int" distinction is important for cases where you're _not_ using literals, like `x = 4; y = 3-1; x is y`. – abarnert May 06 '13 at 22:57
-
@abarnert Absolutely nothing == brain-fart. I'll correct that comment. – BlackVegetable May 06 '13 at 22:57
-
The fun fact doesn't work for large integers something like >999 it doesn't point to the same memory address – Chandra Kanth Aug 24 '18 at 06:52
-
2The reason why that happens is because both x and y are pointing to the same memory address. What reason would Python have in this case to create the exact same object in memory twice, when not explicitly asked to do so? – 1313e May 01 '19 at 01:50
-
As the OP was surprised that two integer "variable" have the same value for id: The reason is that "variables" work very differently in Python than in most other programming languages. Variables are just names for object references in Python. Small integers are predefined objects at runtime. ```` a = 3 b = 4 c = [a, b] print(a, b] # 3, 4 b = 5 print(a, b) # Still 3, 4, because you updated the assignment to b # but not the immutable object that was used when creating the list ```` The best background reference for this is IMO http://effbot.org/zone/call-by-object.htm – Martin Hepp Oct 29 '19 at 07:53
-
Step debug from a `C++` jotted down address and when in python, i printed out the `id` is not the memory address. In my case, it is the `addressof(buf.contents)` that matches the `C++` memory address. What's the true story here? Is this `id` vs `addressof` python-implementation dependent? I need some snake wisdom here.. thanks in advance. – daparic Jun 26 '20 at 18:12
-
In jupyter notebook, it always gives me the same address for integers up to 256. I assume it has to to with the integer only needing a single bite to be stored! – João Bravo Aug 10 '21 at 18:29
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.
x = 5
print hex(id(x))
this will print the memory address of x.

- 1,923
- 2
- 16
- 34
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).
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:
>>> 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.

- 354,177
- 51
- 601
- 671
-
This is the most accurate answer! Both to this post and to the "duplicate" (http://stackoverflow.com/questions/121396/accessing-object-memory-address) – DilithiumMatrix Oct 09 '14 at 16:43