mirror of
https://github.com/python/cpython.git
synced 2025-12-04 08:34:25 +00:00
PEP-0318, @decorator-style. In Guido's words:
"@ seems the syntax that everybody can hate equally" Implementation by Mark Russell, from SF #979728.
This commit is contained in:
parent
fd7dc5169c
commit
c2a5a63654
28 changed files with 2965 additions and 2335 deletions
|
|
@ -73,6 +73,9 @@
|
|||
\lineiii{Continue}{}{}
|
||||
\hline
|
||||
|
||||
\lineiii{Decorators}{\member{nodes}}{List of function decorator expressions}
|
||||
\hline
|
||||
|
||||
\lineiii{Dict}{\member{items}}{}
|
||||
\hline
|
||||
|
||||
|
|
@ -101,7 +104,8 @@
|
|||
\lineiii{}{\member{names}}{}
|
||||
\hline
|
||||
|
||||
\lineiii{Function}{\member{name}}{name used in def, a string}
|
||||
\lineiii{Function}{\member{decorators}}{\class{Decorators} or \code{None}}
|
||||
\lineiii{}{\member{name}}{name used in def, a string}
|
||||
\lineiii{}{\member{argnames}}{list of argument names, as strings}
|
||||
\lineiii{}{\member{defaults}}{list of default values}
|
||||
\lineiii{}{\member{flags}}{xxx}
|
||||
|
|
|
|||
|
|
@ -109,10 +109,14 @@ def my_import(name):
|
|||
|
||||
\begin{verbatim}
|
||||
class C:
|
||||
@classmethod
|
||||
def f(cls, arg1, arg2, ...): ...
|
||||
f = classmethod(f)
|
||||
\end{verbatim}
|
||||
|
||||
The \code{@classmethod} form is a function decorator -- see the description
|
||||
of function definitions in chapter 7 of the
|
||||
\citetitle[../ref/ref.html]{Python Reference Manual} for details.
|
||||
|
||||
It can be called either on the class (such as \code{C.f()}) or on an
|
||||
instance (such as \code{C().f()}). The instance is ignored except for
|
||||
its class.
|
||||
|
|
@ -122,6 +126,7 @@ class C:
|
|||
Class methods are different than \Cpp{} or Java static methods.
|
||||
If you want those, see \function{staticmethod()} in this section.
|
||||
\versionadded{2.2}
|
||||
Function decorator syntax added in version 2.4.
|
||||
\end{funcdesc}
|
||||
|
||||
\begin{funcdesc}{cmp}{x, y}
|
||||
|
|
@ -936,10 +941,14 @@ except NameError:
|
|||
|
||||
\begin{verbatim}
|
||||
class C:
|
||||
@staticmethod
|
||||
def f(arg1, arg2, ...): ...
|
||||
f = staticmethod(f)
|
||||
\end{verbatim}
|
||||
|
||||
The \code{@staticmethod} form is a function decorator -- see the description
|
||||
of function definitions in chapter 7 of the
|
||||
\citetitle[../ref/ref.html]{Python Reference Manual} for details.
|
||||
|
||||
It can be called either on the class (such as \code{C.f()}) or on an
|
||||
instance (such as \code{C().f()}). The instance is ignored except
|
||||
for its class.
|
||||
|
|
|
|||
|
|
@ -315,8 +315,12 @@ section~\ref{types}):
|
|||
|
||||
\begin{productionlist}
|
||||
\production{funcdef}
|
||||
{"def" \token{funcname} "(" [\token{parameter_list}] ")"
|
||||
{[\token{decorators}] "def" \token{funcname} "(" [\token{parameter_list}] ")"
|
||||
":" \token{suite}}
|
||||
\production{decorators}
|
||||
{\token{decorator} ([NEWLINE] \token{decorator})* NEWLINE}
|
||||
\production{decorator}
|
||||
{"@" \token{dotted_name} ["(" [\token{argument_list} [","]] ")"]}
|
||||
\production{parameter_list}
|
||||
{(\token{defparameter} ",")*}
|
||||
\productioncont{("*" \token{identifier} [, "**" \token{identifier}]}
|
||||
|
|
@ -343,6 +347,27 @@ as the global namespace to be used when the function is called.
|
|||
The function definition does not execute the function body; this gets
|
||||
executed only when the function is called.
|
||||
|
||||
A function definition may be wrapped by one or more decorator expressions.
|
||||
Decorator expressions are evaluated when the function is defined, in the scope
|
||||
that contains the function definition. The result must be a callable,
|
||||
which is invoked with the function object as the only argument.
|
||||
The returned value is bound to the function name instead of the function
|
||||
object. If there are multiple decorators, they are applied in reverse
|
||||
order. For example, the following code:
|
||||
|
||||
\begin{verbatim}
|
||||
@f1
|
||||
@f2
|
||||
def func(): pass
|
||||
\end{verbatim}
|
||||
|
||||
is equivalent to:
|
||||
|
||||
\begin{verbatim}
|
||||
def func(): pass
|
||||
func = f2(f1(func))
|
||||
\end{verbatim}
|
||||
|
||||
When one or more top-level parameters have the form \var{parameter}
|
||||
\code{=} \var{expression}, the function is said to have ``default
|
||||
parameter values.'' For a parameter with a
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue