python中注解使用
一、简介
python3添加了注解功能,借助注解,可以实现aop功能,如日志记录等。
二、使用示例
def logDec(fn):
def doLogDec(*args):
print('start function {} args:{}'.format(fn, args))
return fn(*args)
return doLogDec
@logDec
def hello(name):
rs = 'hello {}'.format(name)
return rs
if __name__ == '__main__':
rs = hello('apple')
print(rs)
输出:
start function <function hello at 0x7fa680d8aea0> args:('apple',)
hello apple