【发布时间】:2015-03-24 13:58:19
【问题描述】:
以下是我的代码。给定任何内容,它必须将某些 HTML 标记附加到前面的内容中。 我正在学习使用 call 而不是函数闭包来编写装饰器。
class decorate:
def __init__(self, tag=""):
self.tag = tag
def __call__(self, function, *args):
return "<p>{}</p>".format(self.tag, function(*args), self.tag)
@decorate(tag="p")
def get_content(content):
return content
print(get_content("I'm awesome"))
# Error i got.
Traceback (most recent call last):
File "cache.py", line 27, in <module>
@decorate(tag="p")
File "cache.py", line 25, in __call__
return "<p>{}</p>".format(self.tag, function(*args), self.tag)
TypeError: get_content() takes exactly 1 argument (0 given)
【问题讨论】:
标签: python django decorator python-decorators