-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosure.py
More file actions
69 lines (45 loc) · 1.17 KB
/
closure.py
File metadata and controls
69 lines (45 loc) · 1.17 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def main_func(name):
def inner_function():
print('hello my friend', name)
return inner_function
# ---------------------------------------------------
def adder(value):
def inner(a):
return value + a
return inner
# ---------------------------------------------------
def counter():
count = 0
def inner():
nonlocal count
count += 1
return count
return inner
# ---------------------------------------------------
def average_numbers():
summa = 0
count = 0
def inner(number):
nonlocal summa, count
summa += number
count += 1
return summa / count
return inner
# ---------------------------------------------------
from time import perf_counter
def timer():
start = perf_counter()
def inner():
return perf_counter() - start
return inner
# ---------------------------------------------------
def add(a, b):
return a + b
def counter(func):
count = 0
def inner(*args, **kwargs):
nonlocal count
count += 1
print(f'Func {func.__name__} called {count} times')
return func(*args, **kwargs)
return inner