Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用@_Python - Fatal编程技术网

在Python中使用@

在Python中使用@,python,Python,我在IDLE中看到了python内置的super()方法的方法和文档,该方法使用help()函数。 我偶然发现了这段代码 这也适用于类方法:|类C(B):|@classmethod| def cmeth(cls,arg):| super().cmeth(arg) 在第二行中,您可以看到@符号在classmethod之前 @符号在python中起什么作用?它的用途是什么?字符表示修饰符。decorator是一种函数,它可以通过环绕另一个函数来临时修改或扩展另一个函数的行为 修饰符通过将函数作为参数

我在IDLE中看到了python内置的
super()
方法的方法和文档,该方法使用
help()
函数。 我偶然发现了这段代码

这也适用于类方法:|类C(B):|@classmethod| def cmeth(cls,arg):| super().cmeth(arg)

在第二行中,您可以看到
@
符号在
classmethod
之前


@
符号在python中起什么作用?它的用途是什么?

字符表示修饰符。decorator是一种函数,它可以通过环绕另一个函数来临时修改或扩展另一个函数的行为

修饰符通过将函数作为参数接收来包装函数。
@
语法(也称为“饼”语法)在代码段中定义了
classmethod
decorator之后,将其应用于
cmeth

您可以从您的示例(
classmethod
)中阅读有关特定装饰器的更多信息。

请始终对所有与python相关的问题使用泛型[python]标记。装饰器语法不是“classmethod(cmeth(cls,arg))的缩写”;这将对调用
cmeth
的结果应用
classmethod
。装饰器将
classmethod
应用于
cmeth
本身。也就是说,将
@classmethod
装饰器应用于
cmeth
是在
cmeth
定义之后执行
cmeth=classmethod(cmeth)
的合成糖。