blob: 9218454fe3e43e1b50c0a064565ddaa76d19f267 [file] [log] [blame]
Raymond Hettinger9c323f82005-02-28 19:39:44 +00001\section{\module{functional} ---
2 Higher order functions and operations on callable objects.}
3
4\declaremodule{standard}{functional} % standard library, in Python
5
6\moduleauthor{Peter Harris}{scav@blueyonder.co.uk}
7\moduleauthor{Raymond Hettinger}{python@rcn.com}
8\sectionauthor{Peter Harris}{scav@blueyonder.co.uk}
9
10\modulesynopsis{Higher-order functions and operations on callable objects.}
11
Raymond Hettinger6a458e92005-03-02 15:10:38 +000012\versionadded{2.5}
Raymond Hettinger9c323f82005-02-28 19:39:44 +000013
14The \module{functional} module is for higher-order functions: functions
15that act on or return other functions. In general, any callable object can
16be treated as a function for the purposes of this module.
17
18
19The \module{functional} module defines the following function:
20
21\begin{funcdesc}{partial}{func\optional{,*args}\optional{, **keywords}}
22Return a new \class{partial} object which when called will behave like
23\var{func} called with the positional arguments \var{args} and keyword
24arguments \var{keywords}. If more arguments are supplied to the call, they
25are appended to \var{args}. If additional keyword arguments are supplied,
26they extend and override \var{keywords}. Roughly equivalent to:
27 \begin{verbatim}
28 def partial(func, *args, **keywords):
29 def newfunc(*fargs, **fkeywords):
30 newkeywords = keywords.copy()
31 newkeywords.update(fkeywords)
32 return func(*(args + fargs), **newkeywords)
33 newfunc.func = func
34 newfunc.args = args
35 newfunc.keywords = keywords
36 return newfunc
37 \end{verbatim}
38
39The \function{partial} is used for partial function application which
40``freezes'' some portion of a function's arguments and/or keywords
Walter Dörwald769f8212005-04-14 20:08:59 +000041resulting in a new object with a simplified signature. For example,
Raymond Hettinger9c323f82005-02-28 19:39:44 +000042\function{partial} can be used to create a callable that behaves like
43the \function{int} function where the \var{base} argument defaults to
44two:
45 \begin{verbatim}
46 >>> basetwo = partial(int, base=2)
Armin Rigo5ed262b2005-12-06 18:32:37 +000047 >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
Raymond Hettinger9c323f82005-02-28 19:39:44 +000048 >>> basetwo('10010')
49 18
50 \end{verbatim}
51\end{funcdesc}
52
53
54
55\subsection{\class{partial} Objects \label{partial-objects}}
56
57
58\class{partial} objects are callable objects created by \function{partial()}.
59They have three read-only attributes:
60
61\begin{memberdesc}[callable]{func}{}
62A callable object or function. Calls to the \class{partial} object will
63be forwarded to \member{func} with new arguments and keywords.
64\end{memberdesc}
65
66\begin{memberdesc}[tuple]{args}{}
67The leftmost positional arguments that will be prepended to the
68positional arguments provided to a \class{partial} object call.
69\end{memberdesc}
70
71\begin{memberdesc}[dict]{keywords}{}
72The keyword arguments that will be supplied when the \class{partial} object
73is called.
74\end{memberdesc}
Raymond Hettinger3e1dd3b2005-03-08 07:15:36 +000075
76\class{partial} objects are like \class{function} objects in that they are
77callable, weak referencable, and can have attributes. There are some
78important differences. For instance, the \member{__name__} and
79\member{__doc__} attributes are not created automatically. Also,
80\class{partial} objects defined in classes behave like static methods and
81do not transform into bound methods during instance attribute look-up.