As we know, there are __str__
and __repr__
convert object into str
, but is there any magic function that I can used to do the exact opposite thing? For example, I have
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return '({}, {})'.format(self.x, self.y)
I can get (1, 2)
if I call print(Point(1, 2))
and assume I saved it into a file. And now, if I want to recover it from the file. Is there any magic function that I can overload to parse the raw string into object? Just like what the <<
operator does in C++, and what the Read
class does in Haskell.
Emm, I'm not sure if this just is what called serialization and deserialization.
UPDATE
I just want a magic function's name, which play the role of placeholder, when I want to parse a raw string into a object, I just come to this name. I'm not asking how to parse. So this is not duplicated with Convert strig to Python class object?.