Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def add1_decorator(func):
- """
- a decorator
- it adds 1 to the result and prints "I'm decorated"
- """
- def inner(*args, **kwargs):
- print("I'm decorated")
- return func(*args, **kwargs) + 1
- return inner
- def add2_wrapper(fun, *args, **kwargs):
- """
- a wrapper
- a function that takes another function as argument
- it adds 2 to the result of the function and prints "I'm wrapped"
- """
- print ("I'm wrapped")
- return fun(*args, **kwargs) + 2
- def sum_base(a,b):
- """
- a base function
- """
- return a+b
- @add1_decorator
- def sum_decorated(a,b):
- """
- a decorated function
- """
- return a+b
- def sum_wrapped(func, a, b):
- """
- a wrapped function
- """
- return add2_wrapper(sum_base, a, b)
- print('Base function:')
- print(sum_base(2, 3))
- print('Decorated function:')
- print(sum_decorated(2, 3))
- print('Wrapped function:')
- print(sum_wrapped(sum_base, 2, 3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement