Yes, this is a simplification. If we look at the grammar , decorator appears only in the decorators rule, which appears only as part of a classdef or funcdef :
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE decorators: decorator+ decorated: decorators (classdef | funcdef)
What the link to the language says (and I think this is what is repeated in the linked answer) is that
@f1(arg) @f2 def func(): pass
equivalently
def func(): pass func = f1(arg)(f2(func))
and similarly for class definitions. But this does not mean that the @decorator syntax can be applied only anywhere; it is valid only before defining a function or class.
As an aside, even official documents are not strictly correct; while the decorator is called a function (or class), it is not tied to the enclosing namespace or scope, therefore, the syntaxes indicated are not entirely equivalent.
Something interesting about the def and class statements, which, in my opinion, are part of the reason that they are the only statements supported by the @decorator syntax: this is the only way in Python to associate a name for an object that knows what that name is.
Finally, here is another way to call a decorator that you might like:
@decorator class baked: __metaclass__ = lambda *_: Potato()
source share