-
Notifications
You must be signed in to change notification settings - Fork 0
/
59 Day 59 - Decorators in Python.py
48 lines (39 loc) · 1.55 KB
/
59 Day 59 - Decorators in Python.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
''' A decorator is a function that takes another function as an argument and returns a new function that modifies the behavior of the original function. The new function is often referred to as a "decorated" function. The basic syntax for using a decorator is the following:
@decorator_function
def my_function():
pass
The @decorator_function notation is just a shorthand for the following code:
def my_function():
pass
my_function = decorator_function(my_function)
Practical use case: One common use of decorators is to add logging to a function.
For example, you could use a decorator to log the arguments and return value of a function each time it is called: '''
import logging
def log_function_call(func):
def decorated(*args, **kwargs):
logging.info(
f"Calling {func.__name__} with args={args}, kwargs={kwargs}")
result = func(*args, **kwargs)
logging.info(f"{func.__name__} returned {result}")
return result
return decorated
@log_function_call
def my_function(a, b):
return a + b
''' In this example, the log_function_call decorator takes a function as an argument and returns a new function that logs the function call before and after the original function is called. '''
def greet(fx):
def mfx(*args, **kwargs):
print("Good Morning")
fx(*args, **kwargs)
print("Thanks for using this function")
return mfx
@greet
def hello():
print("Hello world")
@greet
def add(a, b):
print(a+b)
# greet(hello)()
hello()
# greet(add)(1, 2)
add(1, 2)