forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.py
More file actions
executable file
·182 lines (131 loc) · 3.31 KB
/
decorator.py
File metadata and controls
executable file
·182 lines (131 loc) · 3.31 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
"""
## decorator
Decorators are syntaxical sugar syntax that allows to write functions or classes in the forms:
@decorator
def f():
pass
@decorator
class C:
pass
Where `decorator` is any callable such as a function of a class that implements `__call__`.
They are documented together with function and class definition syntax.
"""
if 'Create a function based function decorator':
def decorator(func):
def wrapper(i, *args, **kwargs):
return func(i, *args, **kwargs) + 1
return wrapper
# Decorator sugar
@decorator
def f(i):
return i
assert f(1) == 2
# Exact same but without decorator sugar:
def f(i):
return i
decorated = decorator(f)
assert decorated(1) == 2
if 'Multiple decorators':
def decorator(func):
def wrapped(i):
return func(i) + 2
return wrapped
def decorator2(func):
def wrapped(i):
return func(i) * 2
return wrapped
@decorator
@decorator2
def f(i):
return i
assert f(0) == 2
if 'Create a callable class based function decorator':
class Decorator:
def __init__(self, j):
self.j = j
def __call__(self, func):
def wrapper(i, *args, **kwargs):
return func(i, *args, **kwargs) + self.j
return wrapper
# Decorator sugar
@Decorator(2)
def f(i):
return i
assert f(1) == 3
if 'Create a function based ## class decorator':
def decorator(cls):
def f(self):
return 1
cls.f = f
return cls
@decorator
class C(object):
pass
assert C().f() == 1
if 'It is only possible to decorate functions or classes, not variables.':
def d():
pass
#@d #SyntaxError
a = 1
if 'Decorators can take multiple arguments.':
def decorator(func, j):
def wrapper(i, *args, **kwargs):
return func(i, *args, **kwargs) + j
return wrapper
# Decorator sugar
#@decorator
def f(i):
return i
# TODO get working
#assert f(1) == 3
if 'Decorator must take at least one argument to not raise a TypeError.':
def d():
pass
try:
@d
def f():
pass
except TypeError:
pass
else:
assert False
if 'Decorator functions of __call__ methods must take at exactly one argument.':
"""
If you want to pass parameters to a decorator, make a class based decorator
and use its __init__ method for the arguments.
"""
# Direct function approach fails.
def decorator(func, j):
def wrapper(i, *args, **kwargs):
return func(i, *args, **kwargs) + j
return wrapper
try:
@decorator(2)
def f(i):
return i
except TypeError:
pass
else:
assert False
if 'The decorator call is only made at function / class definition.':
def decorator(func):
global i
i += 1
def wrapper(*args, **kwargs):
pass
return wrapper
i = 0
@decorator
def f():
pass
assert i == 1
f()
assert i == 1
if 'Decorators can return anything.':
def d(g):
return 1
@d
def f():
pass
assert f == 1