“@” Decorator (in Python) [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-13 22:42:18

问题


Possible Duplicate:
Understanding Python decorators

What function does the "class decorator"/"method decorator" (@) serve? In other words, what is the difference between this and a normal comment?

Also, what does setter do when using @previousMethod.setter before a method? Thank you.


回答1:


@decorator
def function(args):
    #body

is just syntactic sugar for:

def function(args):
    #body

function = decorator(function)

That's really it.

As you see, the decorator gets called, so it's by no means a comment.



来源:https://stackoverflow.com/questions/10191561/decorator-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!