-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_or_not.py
57 lines (40 loc) · 1.22 KB
/
callback_or_not.py
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
51
52
53
54
55
56
57
def DoWorkInGenerator(num_work_items):
for i in xrange(num_work_items):
print 'G do some work here'
yield i
print 'G do more work here'
for i in DoWorkInGenerator(3):
print i
################################################################################
def DoWorkWithCallback(num_work_items, callback):
for i in xrange(num_work_items):
print 'C do some work here'
callback(i)
print 'C do more work here'
def MyCallback(i):
print i
DoWorkWithCallback(3, MyCallback)
################################################################################
class DoWorkBase(object):
def __init__(self, num_work_items):
for self.i in xrange(num_work_items):
print 'B do some work here'
self.CustomCode()
print 'B do more work here'
def CustomCode(self):
raise NotImplementedError()
class DoCustomWork(DoWorkBase):
def CustomCode(self):
print self.i
DoCustomWork(3)
################################################################################
class DoWorkInObject(object):
def __init__(self, i):
print 'O do some work here'
self.i = i
def MoreWork(self):
print 'O do more work here'
for i in xrange(3):
w = DoWorkInObject(i)
print w.i
w.MoreWork()