Python中的注解:Decorator (Annotation)

python’s decorator is like Java’s annotation, but more flexible and easy to implement because of python’s syntax. Both is to use a time way to realize AOP, to insert job of other aspect and make you class/func concentrate to bussiness logic.

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

def anna(fn):
def new_func(*args):
print 'by anna args=%s' % args
return fn(*args)
return new_func

def annie(ar):
print 'by annie1 ar=%s' % ar
def _A(fn):
def new_func(*args):
print 'by annie2 args=%s' % args
return fn(*args)
return new_func
return _A

class ccc():
@anna
def __init__(self):
print 'ccc'
@anna
def ff(self, a):
print a

 

@anna
def test1(a):
print a

@annie('hi')
def test2(a):
print a

test1((1,2))
test2((3,4))