-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeco_via_class.py
More file actions
41 lines (30 loc) · 825 Bytes
/
Copy pathdeco_via_class.py
File metadata and controls
41 lines (30 loc) · 825 Bytes
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
class DecoNoArgs(object):
def __init__(self, f):
self._f = f
self._cached = None
def __call__(self, *args, **kwargs):
res = str(self._f(*args, **kwargs))
self._cached = res
return 'great ' + res
def __getattr__(self, key):
return self._cached
class DecoWithArgs(object):
def __init__(self, helper):
self._helper = helper
def __call__(self, f):
def wrapped(*args, **kwargs):
name = f.__name__
self.name = f(*args, **kwargs)
return self._helper + ' ' + f(*args, **kwargs)
return wrapped
# foo = Deco('hello')(foo)
@DecoWithArgs('hello')
def foo():
return 'world'
@DecoNoArgs
def bar(s):
return s
if __name__ == '__main__':
print foo()
print bar('world')
print bar.foo