[python]代码库
class Item:
def __init__(self,name,**kwargs):
self.name = name
self._spec = kwargs
def __repr__(self):
return '{} = {}'.format(self.name,self._spec)
class Cart:
def __init__(self):
self.items = []
def __len__(self):
return len(self.items)
def additem(self,item):
self.items.append(item)
def __add__(self, other): # +
print(other)
self.items.append(other)
return self
def __getitem__(self, index): #index
return self.items[index]
def __setitem__(self, key, value):
print(key,value)
self.items[key] = value
# self[key] = value
def __iter__(self):
return iter(self.items)
# def __missing__(self, key):
# print("key="+key)
def __repr__(self):
return str(self.items)
cart = Cart()