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 閱讀(349)
評論(0) 編輯 收藏 引用