首页 > 代码库 > python2.x学习小记

python2.x学习小记

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

一、Python中的装饰器

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()

输出结果:

deco_args
1
2
3
foo
foo(1,2) is called


python2.x学习小记