Python2.x学习小记

不定时更新,不一定适合3.X,但一定适合2.7。

一、Python中的装饰器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def main():
def decorator_with_params(arg_of_decorator):
print arg_of_decorator
print '1'
def newDecorator(func):
print '3'
print func.__name__
return func(1,2)
print '2'
return newDecorator
@decorator_with_params("deco_args")
def foo(a,b):
print 'foo('+str(a)+','+ str(b)+') is called'
if __name__ == '__main__':
main()

输出结果:

1
2
3
4
5
6
deco_args
1
2
3
foo
foo(1,2) is called
Comments