标签:数据结构 raise int object add turn test exce assert
class Bag(object):
def __init__(self,maxsize=10):
self.maxsize=maxsize
self._item=list()
def add(self,item):
if len(self)>self.maxsize:
raise Exception('bag is full')
self._item.append(item)
def remove(self,item):
self._item.remove(item)
def __len__(self):
return len(self._item)
def __iter__(self):
for item in self._item:
yield item
def test_bag():
bag=Bag()
bag.add(1)
bag.add(2)
bag.add(3)
assert len(bag)==3
bag.remove(3)
assert len(bag)==2
for i in bag:
print(i)
test_bag()
标签:数据结构 raise int object add turn test exce assert
原文地址:https://www.cnblogs.com/peitianwang/p/11435266.html