手记

装饰器学习代码,由浅及深

import functools
def log1(f):
def new_test():
print 'call %s()...' % (f.name)
return f()
return new_test

def log2(str):
def log1(f):
def new_test(*argc,*argv):
print '[%s]call %s' %(str,f.name)
return f(
argc,**argv)
return new_test
return log1

def log3(str):
def log1(f):
@functools.wraps(f)
def new_test(*argc,*argv):
print '[%s]third time call %s' %(str,f.name)
return f(
argc,**argv)
return new_test
return log1

@log1
def test():
pass

print test()

@log2('DEBUG')
def test():
pass

print test()
print test.name

@log3('DEBUG')
def test():
pass

print test()
print test.name

0人推荐
随时随地看视频
慕课网APP