这可能是一个操作系统的问题,因为我在youtube上看到有人在演示如何在基于Linux的系统中使用python中的decorator。在
所以我想和装修师玩。在通常的形式中,首先创建它的函数,然后使用特殊的keywoard“@func_name”,以便将下一行参数传递到函数中。这种利用decorator的特殊方法行不通。我在PyDev(Eclipse)中试过了,但它只是语法错误。我还尝试了交互式Python。在
当前运行的是windows OS 7 Python 2.78版
这里有更具体的例子
def c(n):
def d():
return "Hello world",n
return d()
c=c("of Python")
print c
output: ('Hello world', 'of Python')
@c
"of Python"
output: "of Python?"
^
SyntaxError: invalid syntax
根据乔恩的回答创建了一个工作版本
^{pr2}$
这不是装修工的工作方式。您可以将参数传递给这样的装饰器
但要想做到这一点,您需要稍微调整一下您的decorator函数。在
请阅读https://stackoverflow.com/a/1594484/1843331
它很好地解释了decorator,它们是如何工作的,如何与decorator一起使用参数等等。在
相关问题 更多 >
编程相关推荐