This is something I don't really get. I am trying to use __repr__
to create a new object from its output.
I have a class, OrderedSet, which contains a list and methods to organize it. The str method of this class is
def __str__(self):
s = "Set contains: "
for elem in self.list: s += (" '" + elem + "'")
return s
Now I am supposed to use __repr__
in a way to instanciate a new object from it.
Like Orderedset second = repr(first)
Can I just do it like this?
def __repr__(self):
return self.list.__str__()
eval(repr(some_object))
? See e.g. here. – Tzonglist
is a bad variable name since there is a builtin function of the same name. – Tzong