Suppose we have a string, such as <dict object at 0x0000000000511948>
. How would we then get a reference to the object from that string? If the example given does not contain sufficient information to get a reference to the object, I am willing to change the string.
Basically, we are seeking two functions obj2str
and str2obj
such that the following test returns clear:
def single_test(obj):
try:
assert (str2obj(obj2str(obj)) is obj)
print('things went well')
return 0
except (BaseException, Exception) as exep:
print('test failed')
if isinstance(exep, AssertionError) and 'str2obj(obj2str(obj))' in str(exep):
return 1
return 999
It seems like a good candidate for obj2str
would be as follows:
def obj2str(obj):
# ignores overridden __repr__ if it is overridden
return object.__repr__(obj)
However, I don't know what we might do for str2obj
In short what you are asking for is impossible.
The string like <dict object at 0x0000000000511948>
actually tell you the address of the object in the memory. but python is very very high level programming language, so you cannot access the memory (RAM) directly, even if you have the exact address (like in C), all the more you cannot modify the address.
But if you want to use something like this you can simply create a new variable referencing your object instead of the string.
your_dict = {'a':5}
new_dict = your_dict
new_dict['a'] = 17
print(your_dict['a'])
$17
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.