-2

suppose I have a variable with a numeric value of 20. we used the id() function to get its memory address. How can the numeric value of 20 be changed to, for example , 40 using the memory address?! as a result, I have 40 outputs by calling x

x = 20

# the memory address for x variable
address = id(x)

# how to assign 40 to the memory address(address)
# my result should be like the following line
print(x)
# 40

ali
  • 53
  • 1
  • 5
  • 1
    The `id()` function isn't necessarily a memory address, just a unique identifier. In CPython, it *happens* to be a memory address, but that's an implementation detail. (See [here](https://docs.python.org/3/library/functions.html#id).) You can't treat the output of `id()` as if it were a pointer in C/C++. – jjramsey May 13 '22 at 17:34
  • is there a way to change the value of a variable by accessing its memory address in python? – ali May 13 '22 at 17:43
  • 1
    This definitely sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the real goal you're trying to accomplish here? – ChrisGPT was on strike May 13 '22 at 17:50
  • Python variables are not like variables in languages like C or Fortran. They aren't names for memory locations. See here for details: https://nedbatchelder.com/text/names.html – jjramsey May 13 '22 at 17:59

1 Answers1

2

One ordinarily do not to that in Python: it is not the language designed for that: int in Python is a high level, imutable object, and once you need an int with a different value, you will have another object, in another memory location.

But, there are data structures that store numbers as understood by the CPU in memory. One of them is the one used by bytearray, which essentially works as a C char pointer (string).

Changing a value in a bytearray will change its value inplace, in memory - but the value returned by the "id" of a bytearray is not the data address, it is the address of the object header. Again: when coding in Python, messing with "absolute" (in process) memory addresses is not a priority.

In [30]: x = bytearray(b"Hello World")

In [31]: x.decode()
Out[31]: 'Hello World'

In [32]: x[0] -= 7

In [33]: x.decode()
Out[33]: 'Aello World'

It is possible, by making use of ctypes, which allows for low level memory manipulation, and actually using the numbers returned by id as memory pointers: yo u still would have to reach the exact offset of the number value to change an int in place, and, as that is not an expected behavior, it would probably (and often does) lead to interpreter segfault - as the int immutability is a premise for the code.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • i used int as an example. suppose it is an object or.... in fact, my question is, is there a way to access the memory address to which the variable(name) attached? and how to change the value inside the memory address – ali May 13 '22 at 17:48
  • 1
    @ali [Yes](https://stackoverflow.com/q/70882092/12671057) (as jsbueno also already said), but don't. – Kelly Bundy May 13 '22 at 17:50