-2

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.

Community
  • 1
  • 1
Jared Zoneraich
  • 561
  • 1
  • 5
  • 13
  • 2
    A decorator is not a comment. Check [the docs](http://docs.python.org/reference/compound_stmts.html#function) for what they are. As for the latter note, it's using [``property()``](http://docs.python.org/library/functions.html#property) to avoid getters/setters in favour of attributes. – Gareth Latty Apr 17 '12 at 12:50
  • http://stackoverflow.com/questions/739654/understanding-python-decorators – tMC Apr 17 '12 at 13:40

1 Answers1

5
@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.

ch3ka
  • 11,792
  • 4
  • 31
  • 28