问题
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