Home > database >  Python - Change value at memory address
Python - Change value at memory address

Time:03-11

For example, I have a variable called a, and need to change it by its memory address:

a: str = "hello"
address: int = id(a)

change_by_address(address, a, 'newval') # is something like this possible?

Is there some way to do this, perhaps in the ctypes library?

CodePudding user response:

Well you can read by address in python:

import ctypes


a = 10
memfield = (ctypes.c_int).from_address(id(a))
print(memfield) # c_int(15)

But as far as I know, you cannot change values by address.

CodePudding user response:

Absolutely, positively not. That ID is an address of SOMETHING in memory, but in C terms these are all moderately complicated data structures.

Python is not C. You cannot think of things in the same way.

  • Related