-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_ClosureAndAnonymous.py
More file actions
81 lines (64 loc) · 1.49 KB
/
function_ClosureAndAnonymous.py
File metadata and controls
81 lines (64 loc) · 1.49 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
70
71
#function as return
def lazy_sum(*args):
def sum():
ax = 0
for n in args:
ax = ax + n
return ax
return sum
f = lazy_sum(1,3,5,7,9)
f
#<function lazy_sum........>
f()
#25
#sum keeps parameters and variables of lazy_sum.
#every time lazy_sum will return different function.
def createCounter():
n = [0]
def counter():
n[0] = n[0] + 1
return n[0]
return counter
cA = createCounter()
print(cA(), cA(), cA(), cA()) # 1 2 3 4
cB = createCounter()
print(cB(), cB(), cB(), cB()) # 1 2 3 4
s = 0
def createCounter():
def counter():
global s
s = s + 1
return s
return counter
cA = createCounter()
print(cA(), cA(), cA(), cA()) # 1 2 3 4
cB = createCounter()
print(cB(), cB(), cB(), cB()) # 5 6 7 8
# lambda x: x * x
# same as
def f(x):
return x * x
# lambda means anonymous function.
# lambda has only one expression
def _not_divisible(n):
return lambda x: x % n > 0
n = 2
it = [1,2,3,4,5]
it = filter(_not_divisible(n), it)
# _not_divisible(n) return lambda x: x % n > 0, still be a function.
# function has one attribute named __name__.
def log(func):
def wrapper(*args, **kw):
print('call %s():' % func.__name__)
return func(*args, **kw)
return wrapper
@log
def now(t):
print(t)
print('2018-7-6')
now(t) # t is not defined, but still work
''' result:
call now():
<function build.<locals>.<lambda> at 0x000002525D658AE8>
2018-7-6
'''