Python - 改变内存地址的值
Python - Change value at memory address
比如我有一个变量叫a
,需要修改它的内存地址:
a: str = "hello"
address: int = id(a)
change_by_address(address, a, 'newval') # is something like this possible?
有什么方法可以做到这一点,也许在 ctypes
库中?
绝对不会。该 ID 是 SOMETHING 在内存中的地址,但在 C 术语中,这些都是中等复杂的数据结构。
Python不是C。你不能用同样的方式思考问题。
好吧,您可以通过 python 中的地址阅读:
import ctypes
a = 10
memfield = (ctypes.c_int).from_address(id(a))
print(memfield) # c_int(15)
但据我所知,您不能通过地址更改值。
比如我有一个变量叫a
,需要修改它的内存地址:
a: str = "hello"
address: int = id(a)
change_by_address(address, a, 'newval') # is something like this possible?
有什么方法可以做到这一点,也许在 ctypes
库中?
绝对不会。该 ID 是 SOMETHING 在内存中的地址,但在 C 术语中,这些都是中等复杂的数据结构。
Python不是C。你不能用同样的方式思考问题。
好吧,您可以通过 python 中的地址阅读:
import ctypes
a = 10
memfield = (ctypes.c_int).from_address(id(a))
print(memfield) # c_int(15)
但据我所知,您不能通过地址更改值。