-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_wrapper_lambda.py
More file actions
37 lines (29 loc) · 1.29 KB
/
basic_wrapper_lambda.py
File metadata and controls
37 lines (29 loc) · 1.29 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
# Generally it may make more sense to use a closure (basic_wrapper_closure.py). This example is an attempt to demonstrate
# decorators without closures as learning both at the same time is confusing.
def decorator_fn(fn): # the decorator function takes the decorated function as a param
print('decorator_fn')
# Using a lambda here so we can pass "fn" into wrapper_fn at the time of calling wrapper_fn.
# If we were to do "return wrapper_fn(fn)" then wrapper_fn would run once when @decorator_fn is called
return lambda: wrapper_fn(fn)
def wrapper_fn(fn):
print('wrapper start')
fn()
print('wrapper end')
print('- Calling @decorator_fn')
@decorator_fn
def original_fn():
print('original_fn')
print('- Calling original_fn()')
original_fn()
# decorator_fn is ONLY called once when the code reaches @decorator_fn. Therefore we must return a "Callable" (function, lambda)
#### OUTPUT:
#> - Calling @decorator_fn
#> decorator_fn
#> - Calling original_fn()
#> wrapper start
#> original_fn
#> wrapper end
####
# If decorator_fn were to "return wrapper_fn(fn)" without the lambda we would get the expected output, but only once
# when @decorator_fn is called and after that we would get an error when origin_fn() was called because it has been
# replaced by the None return of wrapper_fn(fn)