-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoratorClass.py
More file actions
executable file
·49 lines (39 loc) · 1.02 KB
/
Copy pathDecoratorClass.py
File metadata and controls
executable file
·49 lines (39 loc) · 1.02 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
#!/usr/bin/env python
from functools import wraps
from inspect import isfunction
class Deco(object):
def __init__(self, f_or_arg):
print '__init__'
if isfunction(f_or_arg):
print 'isfunc: %s' % f_or_arg.__name__
self.arg = None
self.f = self.wrap(f_or_arg)
else:
print 'notfunc: %s' % f_or_arg.__name__
self.arg = f_or_arg
def wrap(self, f):
print 'wrap', f.__name__
@wraps(f)
def wrapped(*args, **kwd):
print 'wrap-'
result = f(*args, **kwd)
print '-ped'
return result
return wrapped
def __call__(self, *args):
print '__call__', self.arg
if self.f:
self.f()
else:
self.wrap(args[0])()
if __name__ == '__main__':
@Deco
def test_fun():
print "test_fun"
class Test(object):
@Deco # nah...
def fun(self):
print 'Test.fun'
test_fun()
t = Test()
t.fun()