[root@iZj6chejzrsqpclb7miryaZ ~]# python t.py
module: __main__, name: func, doc: this is func.
module: __main__, name: _inner, doc: this is _inner.
module: __main__, name: func, doc: this is func.
[root@iZj6chejzrsqpclb7miryaZ ~]# cat t.py
# coding: utf8
import functools
def deco(f):
def _inner(*a, **kw):
"""this is _inner"""
print "before invoke"
try:
return f(*a, **kw)
finally:
print "after invoke"
return _inner
def func():
"""this is func"""
print "func"
print "module: %s, name: %s, doc: %s." \
% (func.__module__, func.__name__, func.__doc__)
# 使用了装饰器之后,__module__,__name__,__doc__
# + 变成了被装饰器返回的函数的了
@deco
def func():
"""this is func"""
print "func"
print "module: %s, name: %s, doc: %s." \
% (func.__module__, func.__name__, func.__doc__)
# 使用functools.wraps(fn)可以将被其装饰的函数的
# + __module__,__name__,__doc__设置为fn的
def deco(f):
@functools.wraps(f)
def _inner(*a, **kw):
"""this is _inner"""
print "before invoke"
try:
return f(*a, **kw)
finally:
print "after invoke"
return _inner
@deco
def func():
"""this is func"""
print "func"
print "module: %s, name: %s, doc: %s." \
% (func.__module__, func.__name__, func.__doc__)