class Bag:
def __init__(self, i=None):
self.bag = []
if i == None:
pass # i is None, do nothing after create empty bag
elif type(i)==list:
self.bag.extend(i) # append whole list i into bag with extend method
else:
self.bag.append(i) # append single item i into bag
def __repr__(self):
for s in self.bag :
return s
def __str__(self):
for s in self.bag :
return s
In the __str__method. It supposed to return a string.
The list is Bag(['a', 'c', 'b', 'b', 'd', 'd', 'd']). And
__str__ is supposed to return Bag(a[1], c[1], b[2], d[3])
Can anyone tell me how to make it work? Many thanks.