Commit 57e62c74 authored by Skip Montanaro's avatar Skip Montanaro

correct decorator example, tweak description slightly

parent 5378aaae
...@@ -289,7 +289,9 @@ The \code{@classmethod} is shorthand for the ...@@ -289,7 +289,9 @@ The \code{@classmethod} is shorthand for the
the following: the following:
\begin{verbatim} \begin{verbatim}
@A @B @C @A
@B
@C
def f (): def f ():
... ...
\end{verbatim} \end{verbatim}
...@@ -301,16 +303,18 @@ def f(): ... ...@@ -301,16 +303,18 @@ def f(): ...
f = A(B(C(f))) f = A(B(C(f)))
\end{verbatim} \end{verbatim}
Decorators must come on the line before a function definition, and Decorators must come on the line before a function definition, one decorator
can't be on the same line, meaning that \code{@A def f(): ...} is per line, and can't be on the same line as the def statement, meaning that
illegal. You can only decorate function definitions, either at the \code{@A def f(): ...} is illegal. You can only decorate function
module level or inside a class; you can't decorate class definitions. definitions, either at the module level or inside a class; you can't
decorate class definitions.
A decorator is just a function that takes the function to be decorated
as an argument and returns either the same function or some new A decorator is just a function that takes the function to be decorated as an
callable thing. It's easy to write your own decorators. The argument and returns either the same function or some new object. The
following simple example just sets an attribute on the function return value of the decorator need not be callable (though it typically is),
object: unless further decorators will be applied to the result. It's easy to write
your own decorators. The following simple example just sets an attribute on
the function object:
\begin{verbatim} \begin{verbatim}
>>> def deco(func): >>> def deco(func):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment