开发者

Python documentation for @? [duplicate]

开发者 https://www.devze.com 2023-02-21 16:52 出处:网络
This question already has answers here: What doe开发者_C百科s the "at" (@) symbol do in Python?
This question already has answers here: What doe开发者_C百科s the "at" (@) symbol do in Python? (14 answers) Closed 5 years ago.

Just a quick question,

Could someone link me to the documentation for the use of @ in python?

Due not being able to google @ and not knowing the use or name of it I'm not sure how to find it on my own :)

Many thanks!!


Symbols starting with "@" (e.g. @staticmethod) are called "decorators" in Python jargon.

You can find the PEP describing them at this url.

In short, they are syntactic sugar to invoke a function over the object being decorated, e.g.:

@staticmethod
def myfunc(...): ...

is equivalent to:

def myfunc(...): ...
myfunc = staticmethod(myfunc)

Then, searching on the web for "python decorator" will provide you with a lot of other useful information and use cases.

Hope it helps, ciao


Python decorators:

http://docs.python.org/reference/compound_stmts.html#function


Google for python decorator and you will find enough answers to your question.


As other people said, they are decorators. They take the decorated object as an argument and return a new object - which should usually be the same type as the initial one (function or class)

If you do not like the at syntax, you can always write it like that:

@foo
def bar():
    pass

# is the same as:
def bar():
    pass
bar = foo(bar)
0

精彩评论

暂无评论...
验证码 换一张
取 消