I have the following code:
class Stat(list):
def __init__(self, lst = []):
self.s = list(lst)
def __repr__(self):
return "Stat({})".format(self.s)
def add(self, item):
self.s.append(item)
def len(self):
return len(self.s)
...(more methods, but not necessary)
All of the methods work properly but len(). No matter the length of the Stat object, the returned length is always 0; I don't understand why.
lenmethod?list.len(Stat()), the special method name is__len__. The right way to call thelenmethod (as it’s just a method namedlen) on your current class is justStat().len(). But you wouldn’t need to inherit fromlistto do either of these. Pick one of wrappinglist(havingself.s) or inheriting from it.