-
Notifications
You must be signed in to change notification settings - Fork 0
/
closures.py
33 lines (23 loc) · 890 Bytes
/
closures.py
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
# https://www.youtube.com/watch?v=swU3c34d2NQ
# https://github.com/CoreyMSchafer/code_snippets/tree/master/Closures
def outer_func(msg):
message = msg
def inner_func():
"""Note that this inner function oes not take in any arguments"""
print(message)
return inner_func
def main():
hi_func = outer_func('hi')
hello_func = outer_func('hello')
print(hi_func.__name__)
print(hello_func.__name__)
# A closure is an inner function that remembers and has access to variables
# in the local scope in which the variable was created, even after the outer
# function has finished executing.
# Each of these functions remembers the values of its own msg variable.
# A closure closes over the free variables from its environment
# (in this case, message).
hi_func()
hello_func()
if __name__ == "__main__":
main()