“@” Decorator (in Python) [duplicate]

2019-09-25 07:08发布

问题:

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.