2010-01-20 16 views

回答

7

the documentation引用:

functools.wraps(wrapped[, assigned][, updated]) 

這是要定義一個包裝函數調用時作爲partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)函數裝飾便利功能。例如:

>>> from functools import wraps 
>>> def my_decorator(f): 
...  @wraps(f) 
...  def wrapper(*args, **kwds): 
...   print 'Calling decorated function' 
...   return f(*args, **kwds) 
...  return wrapper 
... 
>>> @my_decorator 
... def example(): 
...  """Docstring""" 
...  print 'Called example function' 
... 
>>> example() 
Calling decorated function 
Called example function 
>>> example.__name__ 
'example' 
>>> example.__doc__ 
'Docstring' 

不使用這個裝飾的廠家,例如函數的名稱將是「包裝」,和原來的實例文檔字符串()將已經丟失。

相關問題