forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path188.py
More file actions
40 lines (26 loc) · 616 Bytes
/
188.py
File metadata and controls
40 lines (26 loc) · 616 Bytes
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
"""
Problem:
What will this code print out?
def make_functions():
flist = []
for i in [1, 2, 3]:
def print_i():
print(i)
flist.append(print_i)
return flist
functions = make_functions()
for f in functions:
f()
How can we make it print out what we apparently want?
"""
# The code will print 3 thrice (in 3 lines) as i is passed by reference
def make_functions():
flist = []
for i in [1, 2, 3]:
def print_i(i):
print(i)
flist.append((print_i, i))
return flist
functions = make_functions()
for f, i in functions:
f(i)