forked from edwardwbli/Python_Total_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.py
More file actions
50 lines (46 loc) · 1.04 KB
/
Copy pathcounter.py
File metadata and controls
50 lines (46 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# --*-- coding:utf-8 --*--
from __future__ import print_function
class Counter(object):
"""一个计数器
用法:
>>> counter1 = Counter()
>>> counter1()
1
>>> counter1()
2
>>> counter2 = Counter(lambda : 2,-3)
>>> counter2()
-1
>>> counter2()
1
"""
def __str__(self):
return "state:"+str(self.value)
def __repr__(self):
return self.__str__
def __call__(self):
def count():
self.value += self.func()
return self.value
return count()
def __init__(self,func=lambda : 1,start=0):
self.value = start
self.func = func
test = Counter()
test()
test()
print(test)
if __name__=="__main__":
counter1 = Counter()
counter2 = Counter()
for i in range(10):
counter1()
for i in range(8):
counter2()
if counter1.value == counter2.value:
print("not success")
else:
print("don't known")
import doctest
doctest.testmod(verbose=True)