-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator_demo.py
51 lines (44 loc) · 873 Bytes
/
decorator_demo.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
""" copy from https://segmentfault.com/a/1190000004238416"""
"""
demo 1
"""
def deco(func):
def _deco():
print 'before invoked'
func()
print 'after invoked'
return _deco
@deco
def f():
print 'f is invoked'
"""
demo 2
"""
def deco(func):
def _deco(*args, **kwargs):
print 'before invoked'
ret = func(*args, **kwargs)
print 'after invoded'
return ret
return _deco
@deco
def f(a):
print 'f is invoked'
return a + 1
"""
demo 3
"""
def deco(*args):
def _deco(func):
def __deco(*args, **kwargs):
print 'decorator args is', args
print 'before invoked'
ret = func(*args, **kwargs)
print 'after invoded'
return ret
return __deco
return _deco
@deco('test')
def f(a):
print 'f is invoked'
return a + 1