2 ms·
The @ syntax is just syntactic sugar. @decorator def function(): print("Hello World!") is equivalent to: def function(): print("Hello World
by sfilipov 12y ago
The @ syntax is just syntactic sugar.
@decorator
def function():
print("Hello World!")
is equivalent to:
def function():
print("Hello World!")
function = decorator(function)
- geofft 12y agoExactly. In particular, the decorator can be an expression, so @decorator_factory(foo, bar) def function(): print("Hello World!") becomes def function(): print("Hello World!") function = decorator_factory(foo, bar)(function) or in this case, app.route(path)(function). So route() needs to return something that can be called with one argument. That's executed immediately at the time the function is defined. Also the syntactic sugar lets you do stupid things like >>> @print ... def f(): ... pass ... <function f at 0x101cfdde8> >>> repr(f) 'None' because print is a function that has a side effect and returns None. (If you're on Python 2, don't forget to `from __future__ import print_function`)