The idea behind "using __repr__
to create new objects" is that the output of __repr__
can be valid python code, which, when interpreted with eval
, creates (a copy of) the original object. For example, repr("foo")
return "foo"
(including the "
), or repr([1,2,3])
returns [1,2,3]
.
In your example you probably need something like this:
def __repr__(self):
return "OrderedSet(%r)" % self.list
as well as a corresponding constructor:
def __init__(self, elements):
self.list = elements
This way, repr(OrderedSet([1,2,3]))
returns the string OrderedSet([1,2,3])
, which, when eval
uated, will invoke the contructor and create a new instance of the class.