I'm trying to create a class that returns a value, not self.
I will show you an example comparing with a list:
>>> l = list()
>>> print(l)
[]
>>> class MyClass:
>>> pass
>>> mc = MyClass()
>>> print mc
<__main__.MyClass instance at 0x02892508>
I need that MyClass returns a list, like list()
does, not the instance info. I know that I can make a subclass of list. But is there a way to do it without subclassing?
I want to imitate a list (or other objects):
>>> l1 = list()
>>> l2 = list()
>>> l1
[]
>>> l2
[]
>>> l1 == l2
True
>>> class MyClass():
def __repr__(self):
return '[]'
>>> m1 = MyClass()
>>> m2 = MyClass()
>>> m1
[]
>>> m2
[]
>>> m1 == m2
False
Why is m1 == m2
False? This is the question.
I'm sorry if I don't respond to all of you. I'm trying all the solutions you give me. I cant use def
, because I need to use functions like setitem, getitem, etc.