This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-decorator.py
89 lines (59 loc) · 1.69 KB
/
simple-decorator.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# @property
# [Decorator]
# Add functionality to existing code
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def my_decorator(func):
def inner(value):
print("Decorator Called...")
return func(value)
return inner
@my_decorator
def my_function(value):
print("Show:", value)
my_function(12)
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def show_values_decorator(func):
def inner(*args):
print("\nThis is from show decorator:", args, args[0])
return func(*args)
return inner
def pow_values_decorator(func):
def inner(*args):
args = list(args)
for i in range(len(args)):
args[i] **= 2
print("This is from pow decorator:", args, args[0])
return func(*args)
return inner
@show_values_decorator
@pow_values_decorator
def calc(*args):
print("This is From Function:", args, args[0])
calc(1)
calc(1, 2)
calc(1, 2, 3, 4, 5)
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def check_type_decorator(func):
def inner(*args):
for a in args:
print(a, type(a))
return func(*args)
return inner
@check_type_decorator
def my_mean_function(a: int, b: int, c: int):
return (a + b + c) / 3
print(my_mean_function(4, 5, 6))
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def clean_input_decorator(func):
def inner(*args):
L = list(args)
for index, arg in enumerate(L):
L[index] = arg.replace(" ", "")
return func(*L)
return inner
@clean_input_decorator
def f1(a, b, c):
print(a)
print(b)
print(c)
f1(" hello ", "hello world", "good bye")