-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_decorators.py
More file actions
76 lines (60 loc) · 1.79 KB
/
Copy path13_decorators.py
File metadata and controls
76 lines (60 loc) · 1.79 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import functools
print('Function decorators')
def start_end_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print('Start')
result = func(*args, **kwargs)
print('End')
return result
return wrapper
def repeat(num_times):
def decorator_repeat(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for _ in range(num_times):
result = func(*args, **kwargs)
return result
return wrapper
return decorator_repeat
# a decorator function that prints debug information about the wrapped function
def debug(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
args_repr = [repr(a) for a in args]
kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()]
signature = ", ".join(args_repr + kwargs_repr)
print(f"Calling {func.__name__}({signature})")
result = func(*args, **kwargs)
print(f"{func.__name__!r} returned {result!r}")
return result
return wrapper
# start_end_decorator(print_name)
# The decorators are called by their declaration order
@debug
@start_end_decorator
@repeat(3)
def print_name(name):
# print('Alex')
print(f'Hello, my name is {name} :)')
print_name('Alex')
print('\n')
print('Function identity')
print(help(print_name))
print(print_name.__name__)
print('\n')
print('Class decorators')
class CountCalls:
def __init__(self, func):
self.func = func
self.num_calls = 0
#Allows class instance to be called as function
def __call__(self, *args, **kwargs):
self.num_calls += 1
print(f'This is executed {self.num_calls} times')
return self.func(*args, **kwargs)
@CountCalls
def say_hello():
print('Hello')
say_hello()
say_hello()