在Python中抛出无效语法的修饰符

2024-10-03 21:30:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这可能是一个操作系统的问题,因为我在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}$

Tags: of函数namehelloworldoutputreturnyoutube
1条回答
网友
1楼 · 发布于 2024-10-03 21:30:52

then you use the special keywoard "@func_name" in order to pass the next line of arguments into the function

这不是装修工的工作方式。您可以将参数传递给这样的装饰器

@c("of python")
def decorate_this_func():
    pass

但要想做到这一点,您需要稍微调整一下您的decorator函数。在

请阅读https://stackoverflow.com/a/1594484/1843331

它很好地解释了decorator,它们是如何工作的,如何与decorator一起使用参数等等。在

相关问题 更多 >