gpt4 book ai didi

python - 如何解决显示装饰器结果的错误?

转载 作者:太空狗 更新时间:2023-10-30 02:29:59 24 4
gpt4 key购买 nike

<分区>

我正在学习 Python 3,想写一段简单的代码来学习装饰器。我试着运行这段代码:

def makebold(fn):
def wrapped():
return '<b>' + str(fn) + '</b>'
return wrapped()

def makeitalic(fn):
def wrapped():
return '<i>' + str(fn) + '</i>'
return wrapped()

@makebold
@makeitalic
def hello():
return "Hello World"

print(hello())

但我收到以下错误:

Traceback (most recent call last):
File "E:\Msn Folder\92\Python\Projects\PythonSamplesByMsn\05 Functions\D03_Decorator01.py", line 29, in <module>
print(hello())
TypeError: 'str' object is not callable

我做错了什么?我该如何纠正这个问题?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com