-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathclosures.py
More file actions
155 lines (101 loc) · 2.52 KB
/
closures.py
File metadata and controls
155 lines (101 loc) · 2.52 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
x = "global x"
def level_one():
return x
def level_two(v):
print(v)
if v:
x = "local x"
return x
def level_three():
z = "outer z"
def inner(y):
return x, y, z
return inner("y arg")
def level_four():
z = "first outer z"
def inner(y):
return x, y, z
z = "second outer z"
return inner("y arg")
def level_five(n):
z = f"outer z {n}"
def inner(y):
return x, y, z
return inner
def call_n_times(n):
def inner(f):
for _ in range(n):
f()
return inner
call_3_times = call_n_times(3)
call_3_times(lambda: print("hello"))
def level_six():
z = "outer z"
def donky():
def inner(y):
return x, y, z
z = "donky z"
return inner
def chonky():
x = "chonky x"
f = donky()
return f("y arg")
return chonky()
def what_about_nonlocal_and_global():
x = "nonlocal x"
def f():
nonlocal x
return x
def g():
global x
return x
return f, g
def what_about_lambdas_and_comprehensions():
l = [x * x for x in range(10)]
l = list(x * x for x in range(10))
l = list((x * x for x in range(10)))
g = (x * x for x in range(10))
def gen():
for x in range(10):
yield x * x
g = gen()
def level_seven():
def please_dont_do_this():
if False:
a = None
def gen_func():
nonlocal a
for v in range(10):
a = v
yield v
return gen_func(), lambda: a
gen, fun = please_dont_do_this()
# print(fun()) # error
next(gen)
print(fun()) # 0
next(gen)
print(fun()) # 1
_empty = object()
class Cell:
def __init__(self, cell_contents=_empty):
self._cell_contents = cell_contents
def __repr__(self):
contents = self._cell_contents
if contents is _empty:
contents_str = "empty"
else:
contents_str = f"{contents.__class__.__name__} object at {id(contents):016X}"
return f"<{self.__class__.__name__} at {id(self):016X}: {contents_str}>"
@property
def cell_contents(self):
if self._cell_contents is _empty:
raise ValueError("Cell is empty")
return self._cell_contents
@cell_contents.setter
def cell_contents(self, value):
# except you can't do this from Python
self._cell_contents = value
def main():
level_seven()
if __name__ == '__main__':
main()