-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosure_in.py
More file actions
26 lines (21 loc) · 809 Bytes
/
Copy pathclosure_in.py
File metadata and controls
26 lines (21 loc) · 809 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
#!/usr/bin/env python3
"""Demonstration of a simple closure
An example of a closure transporting a captured variable into a
function call.
NOTE:
A closure is a function that can capture the context of its surrounding
environment.
"""
def evaluate(function, param1, param2):
"""
Returns <function>, called with <param1> and <param2>
"""
return function(param1, param2)
if __name__ == '__main__':
from custom_modules.get_integer_from_user import get_integer
print('Guess my age or DOB : ', end='')
sample = get_integer() # Var : Only captured by lambda expr, not evaluate()
# Call evaluate() with a function (lambda expression) and two integer values
print(evaluate(
lambda x, y: True if x == sample or y == sample else False, 30, 1987
))