diff options
author | Raymond Hettinger <python@rcn.com> | 2005-02-28 19:39:44 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2005-02-28 19:39:44 (GMT) |
commit | 9c323f8de4910dfc0baa5e55aa84eb1b02bcbb72 (patch) | |
tree | 7f0eecf7973ff979710eb1cb1795598e55736774 /Doc | |
parent | 049ade2997aee8cd6564e05d29dbe0b390ebf27b (diff) | |
download | cpython-9c323f8de4910dfc0baa5e55aa84eb1b02bcbb72.zip cpython-9c323f8de4910dfc0baa5e55aa84eb1b02bcbb72.tar.gz cpython-9c323f8de4910dfc0baa5e55aa84eb1b02bcbb72.tar.bz2 |
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Combined efforts of many including Peter Harris, Hye-Shik Chang,
Martin v. Löwis, Nick Coghlan, Paul Moore, and Raymond Hettinger.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/lib/lib.tex | 1 | ||||
-rw-r--r-- | Doc/lib/libfunctional.tex | 72 |
2 files changed, 73 insertions, 0 deletions
diff --git a/Doc/lib/lib.tex b/Doc/lib/lib.tex index 37ab91d..9913449 100644 --- a/Doc/lib/lib.tex +++ b/Doc/lib/lib.tex @@ -132,6 +132,7 @@ and how to embed it in other applications. \input{libarray} \input{libsets} \input{libitertools} +\input{libfunctional} \input{libcfgparser} \input{libfileinput} \input{libcalendar} diff --git a/Doc/lib/libfunctional.tex b/Doc/lib/libfunctional.tex new file mode 100644 index 0000000..c092d6d --- /dev/null +++ b/Doc/lib/libfunctional.tex @@ -0,0 +1,72 @@ +\section{\module{functional} --- + Higher order functions and operations on callable objects.} + +\declaremodule{standard}{functional} % standard library, in Python + +\moduleauthor{Peter Harris}{scav@blueyonder.co.uk} +\moduleauthor{Raymond Hettinger}{python@rcn.com} +\sectionauthor{Peter Harris}{scav@blueyonder.co.uk} + +\modulesynopsis{Higher-order functions and operations on callable objects.} + + +The \module{functional} module is for higher-order functions: functions +that act on or return other functions. In general, any callable object can +be treated as a function for the purposes of this module. + + +The \module{functional} module defines the following function: + +\begin{funcdesc}{partial}{func\optional{,*args}\optional{, **keywords}} +Return a new \class{partial} object which when called will behave like +\var{func} called with the positional arguments \var{args} and keyword +arguments \var{keywords}. If more arguments are supplied to the call, they +are appended to \var{args}. If additional keyword arguments are supplied, +they extend and override \var{keywords}. Roughly equivalent to: + \begin{verbatim} + def partial(func, *args, **keywords): + def newfunc(*fargs, **fkeywords): + newkeywords = keywords.copy() + newkeywords.update(fkeywords) + return func(*(args + fargs), **newkeywords) + newfunc.func = func + newfunc.args = args + newfunc.keywords = keywords + return newfunc + \end{verbatim} + +The \function{partial} is used for partial function application which +``freezes'' some portion of a function's arguments and/or keywords +resulting in an new object with a simplified signature. For example, +\function{partial} can be used to create a callable that behaves like +the \function{int} function where the \var{base} argument defaults to +two: + \begin{verbatim} + >>> basetwo = partial(int, base=2) + >>> basetwo('10010') + 18 + \end{verbatim} +\end{funcdesc} + + + +\subsection{\class{partial} Objects \label{partial-objects}} + + +\class{partial} objects are callable objects created by \function{partial()}. +They have three read-only attributes: + +\begin{memberdesc}[callable]{func}{} +A callable object or function. Calls to the \class{partial} object will +be forwarded to \member{func} with new arguments and keywords. +\end{memberdesc} + +\begin{memberdesc}[tuple]{args}{} +The leftmost positional arguments that will be prepended to the +positional arguments provided to a \class{partial} object call. +\end{memberdesc} + +\begin{memberdesc}[dict]{keywords}{} +The keyword arguments that will be supplied when the \class{partial} object +is called. +\end{memberdesc} |