我想了解Python的裝飾,並寫了這個代碼:例裝飾錯誤
def hello_world(fn):
print('hello world')
fn()
pass
@hello_world
def decorate():
print('hello decoatrion')
return
decorate()
我的目標是前「你好裝飾」打印「世界你好」,但輸出如下:
hello world
hello decoatrion
Traceback (most recent call last):
File "test_decortor.py", line 11, in <module>
decorate()
TypeError: 'NoneType' object is not callable
好的,那你有什麼問題? – melpomene
[我如何在Python中創建一個函數裝飾器鏈?](http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in -python) – GingerPlusPlus
你的裝飾器正在返回'None',而不是裝飾函數。查看[這些示例](https://docs.python.org/3/whatsnew/2.4.html#pep-318-decorators-for-functions-and-methods),以及[PEP 318 - 裝飾器函數,方法和類](https://www.python.org/dev/peps/pep-0318/)本身。 – martineau