Python: “@” followed by a function -> What does it stand for?
我目前正在学习python,我遇到了一个我想知道的符号:
1 2 3 4 5 6 7 | import taskmanager as tm #........ @tm.task(str) def feat(folder): BLA BLA BLA ... |
该代码是https://github.com/kfrancoi/phd-retailreco/blob/master/libraries/plsa/example_plsa.py(该文件包含多个使用
我尽了最大努力在谷歌上搜索这个,但还是发现@符号在我的搜索中被删除了(太短,特殊字符)。StackOverflow也会发生同样的情况。
提前非常感谢
这是一个装饰,由PEP 318定义。词汇表摘录:
A function returning another function, usually applied as a function transformation using the
@wrapper syntax. Common examples for decorators areclassmethod() andstaticmethod() .The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:
1
2
3
4
5
6
7 def f(...):
...
f = staticmethod(f)
@staticmethod
def f(...):
...The same concept exists for classes, but is less commonly used there. See the documentation for function definitions and class definitions for more about decorators.
相关:Python装饰器的一些常见用途是什么?