posts - 183,  comments - 10,  trackbacks - 0
 1 class Filter:
 2     def init(self):
 3         self.blocked = []
 4     def filter(self, sequence):
 5         return [x for x in sequence if x not in self.blocked]
 6 
 7 class SPAMFilter(Filter): # SPAMFilter 是 Filter 的子类
 8     def init(self):
 9         self.blocked = ['SPAM']
10 
11 f = Filter()
12 f.init()
13 print(f.filter([1, 2, 3]))
14 
15 s = SPAMFilter()
16 s.init()
17 print(s.filter(['SPAM', 'SPAM', 'SPAM', 'SPAM', 'eggs', 'bacon', 'SPAM']))

输出:
>>>
[1, 2, 3]
['eggs', 'bacon']
posted on 2013-05-19 11:39 unixfy 阅读(428) 评论(0)  编辑 收藏 引用