-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide_decorators.py
54 lines (39 loc) · 934 Bytes
/
guide_decorators.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
# what is decorator
# is it possible to decorate with two decorators (spec. via commentaries)
# what if *args, **kwargs are interchanged
# def decorator(func):
# return func
#
#
# @decorator
# def decorated():
# print("Hello")
#
#
# decorated = decorator(decorated)
import functools
def decorator(func):
def new_func():
pass
return new_func
@decorator
def decorated():
print("Hello")
# decorated = decorator(decorated)
decorated()
print(decorated.__name__)
log = []
def logger(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
result = func(*args, **kwargs)
f = open("test.txt", "a")
f.write(str(result) + "\n")
f.close()
return wrapped
@logger
def summator(new_list):
return sum(new_list)
summator([2, 3, 444])
with open("test.txt", "r") as f:
print("test.txt: ", f.read())