python利用裝飾器進行運算

今天想用python的裝飾器作一個運算,代碼以下
html

>>> def mu(x):
	def _mu(*args,**kwargs):
		return x*x
	return _mu

>>> @mu
def test(x,y):
	print '%s,%s' %(x,y)

	
>>> test(3,5)

Traceback (most recent call last):
  File "<pyshell#111>", line 1, in <module>
    test(3,5)
  File "<pyshell#106>", line 3, in _mu
    return x*x
TypeError: unsupported operand type(s) for *: 'function' and 'function'


原來是不能這樣弄的  函數與函數是不能運算的啊!python

怎麼辦呢?shell

In [1]: from functools import wraps

In [2]: def mu(x):
   ...:         @wraps(x)
   ...:         def _mu(*args,**kwargs):
   ...:                         x,y=args
   ...:                         return x*x
   ...:         return _mu
   ...: 

In [3]: @mu
   ...: def test(x,y):
   ...:         print '%s,%s' %(x,y)
   ...:     

In [4]: test(3,4)
Out[4]: 9


Python裝飾器decorator)在實現的時候,有一些細節須要被注意。例如,裝飾後的函數其實已是另一個函數了(函數名等函數屬性會發生改變)函數

Python的functools包中提供了一個叫wraps的decorator來消除這樣的反作用。寫一個decorator的時候,最好在實現以前加上functools的wrap,它能保留原有函數的名稱和docstringspa

相關文章
相關標籤/搜索