-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay28 python_decorators.py
More file actions
60 lines (41 loc) · 1.05 KB
/
Day28 python_decorators.py
File metadata and controls
60 lines (41 loc) · 1.05 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Day 28 - Decorators (Complete Example)
# Creating a decorator
def my_decorator(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
# Using decorator
@my_decorator
def say_hello():
print("Hello, welcome to Python!")
# Calling the decorated function
say_hello()
print("--------------------------------")
# Decorator with arguments
def login_required(func):
def wrapper(user):
if user == "admin":
print("Access granted")
func(user)
else:
print("Access denied")
return wrapper
@login_required
def dashboard(user):
print("Welcome to dashboard,", user)
# Calling function with different users
dashboard("admin")
dashboard("guest")
print("--------------------------------")
# Decorator for logging
def log_function(func):
def wrapper():
print("Function name:", func.__name__)
func()
return wrapper
@log_function
def sample_task():
print("Task is running")
sample_task()