-
Notifications
You must be signed in to change notification settings - Fork 1
/
Functions are objects too
92 lines (62 loc) · 1.94 KB
/
Functions are objects too
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import datetime
import time
class TimedEvent:
def __init__(self, endtime,callback):
self.endtime = endtime
self.callback = callback
def ready(self):
return self.endtime <= datetime.datetime.now()
class Timer:
def __init__(self):
self.events = []
def call_after(self,delay,callback):
end_time = datetime.datetime.now() + datetime.timedelta(seconds=delay)
self.events.append(TimedEvent(end_time,callback))
def run(self):
while True:
ready_events = (e for e in self.events if e.ready())
for event in ready_events:
event.callback(self)
# here callback is a function this means run callback function
self.events.remove(event)
time.sleep(0.5)
def format_time(mes,*args):
now = datetime.datetime.now().strftime("%I:%M:%S")
print(mes.format(*args, now=now))
def one(timer):
format_time("{now}: Called One")
def two(timer):
format_time("{now}: Called Two")
def three(timer):
format_time("{now}: Called Three")
timer = Timer()
timer.call_after(1, one)
timer.call_after(2, one)
timer.call_after(2, two)
timer.call_after(8, two)
timer.call_after(3, three)
timer.call_after(6, three)
format_time("{now}: Starting")
timer.run()
def my_function():
print('The function was called')
my_function.description='Silly'
def second_function():
print('The second was called')
second_function.descroption = 'Siller'
def another_function(fx):
print(fx.descroption)
print('Now call the function')
fx()
another_function(second_function)
def my_function(a,b):
print(a+b)
my_function.description='Silly'
def second_function():
print('The second was called')
second_function.description = 'Siller'
def another_function(fx,*args):
print(fx.description)
print('Now call the function')
fx(*args)
another_function(my_function,1,5)