class P:
def __init__(self, x):
self.x = x
def __repr__(self):
return f"p{self.x}"
def a(self):
print(self.x)
a = P(10)
print(a, type(a)) # p10 <class '__main__.P'>
b = str(a)
print(b, type(b)) # p10 <class 'str'>
c = ????
For variable 'c' I would like to convert 'b' back into pointer p10. So I inspect to return for code
print(a.x, c.x)
10 10
I understand that the simple solution is c = a, but I need to convert class 'str' into pointer.
Solution with dictionary, dynamically adding objects
lst = [1, 3, 7, 4]
d = {}
for i in lst:
x = P(i)
d[str(x)] = x
d['p3'].a()
d['p7'].a()
I thought that there is a solution without additional dictionary.