posts - 183,  comments - 10,  trackbacks - 0
 1 class CounterList(list):
 2     def __init__(self, *args):
 3         super(CounterList, self).__init__(*args)
 4         self.counter = 0
 5 
 6     def __getitem__(self, index):
 7         self.counter += 1
 8         return super(CounterList, self).__getitem__(index)
 9 
10 
11 cl = CounterList(range(10))
12 print(cl)
13 cl.reverse()
14 print(cl)
15 del(cl[3:6])
16 print(cl)
17 print(cl.counter)
18 cl[4+ cl[2]
19 print(cl.counter)

输出:
>>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 3, 2, 1, 0]
0
2
posted on 2013-05-21 22:14 unixfy 阅读(132) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理