Background
I have a new feature to implement in my code and it can be solved by reasign a variable without change the pointer to memory.
a = "hi"
print(hex(id(a)))
a = "bye"
print(hex(id(a)))
DESIRED OUTPUT:
0x7fec363cd4f0
0x7fec363cd4f0
REAL OUTPUT:
0x7fec363cd4f0
0x7fec363cd530
Problem
It can't be done in Python from what I've read. So, I think to create a class that inherit from str
and implement a method that update the value. That is because after updating it, pointer to memory does not change. But Im stack.
Question
I have created a custom string class that inherit from str
:
class MyStr(str):
def __new__(cls, s):
return super().__new__(cls, s)
def __init__(self, s):
super().__init__()
If I create a object of this class and print it, the output is:
print(MyStr("hi"))
hi
What I want now, is to create a method in MyStr that update "value of the string" and have by output:
my_str = MyStr("hi")
my_str.update_value("bye")
print(my_str)
bye
How update_value method must be?
Further work
In addition, I would like to do this for int
and float
.
Thanks in advance