blob: 034143a50cd8c00736859267c35f3a73f663b311 [file] [log] [blame]
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001\section{\module{functools} ---
Raymond Hettinger9c323f82005-02-28 19:39:44 +00002 Higher order functions and operations on callable objects.}
3
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004\declaremodule{standard}{functools} % standard library, in Python
Raymond Hettinger9c323f82005-02-28 19:39:44 +00005
6\moduleauthor{Peter Harris}{scav@blueyonder.co.uk}
7\moduleauthor{Raymond Hettinger}{python@rcn.com}
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00008\moduleauthor{Nick Coghlan}{ncoghlan@gmail.com}
Raymond Hettinger9c323f82005-02-28 19:39:44 +00009\sectionauthor{Peter Harris}{scav@blueyonder.co.uk}
10
11\modulesynopsis{Higher-order functions and operations on callable objects.}
12
Raymond Hettinger6a458e92005-03-02 15:10:38 +000013\versionadded{2.5}
Raymond Hettinger9c323f82005-02-28 19:39:44 +000014
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000015The \module{functools} module is for higher-order functions: functions
Raymond Hettinger9c323f82005-02-28 19:39:44 +000016that act on or return other functions. In general, any callable object can
17be treated as a function for the purposes of this module.
18
19
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000020The \module{functools} module defines the following function:
Raymond Hettinger9c323f82005-02-28 19:39:44 +000021
22\begin{funcdesc}{partial}{func\optional{,*args}\optional{, **keywords}}
23Return a new \class{partial} object which when called will behave like
24\var{func} called with the positional arguments \var{args} and keyword
25arguments \var{keywords}. If more arguments are supplied to the call, they
26are appended to \var{args}. If additional keyword arguments are supplied,
27they extend and override \var{keywords}. Roughly equivalent to:
28 \begin{verbatim}
29 def partial(func, *args, **keywords):
30 def newfunc(*fargs, **fkeywords):
31 newkeywords = keywords.copy()
32 newkeywords.update(fkeywords)
33 return func(*(args + fargs), **newkeywords)
34 newfunc.func = func
35 newfunc.args = args
36 newfunc.keywords = keywords
37 return newfunc
38 \end{verbatim}
39
40The \function{partial} is used for partial function application which
41``freezes'' some portion of a function's arguments and/or keywords
Walter Dörwald769f8212005-04-14 20:08:59 +000042resulting in a new object with a simplified signature. For example,
Raymond Hettinger9c323f82005-02-28 19:39:44 +000043\function{partial} can be used to create a callable that behaves like
44the \function{int} function where the \var{base} argument defaults to
45two:
46 \begin{verbatim}
47 >>> basetwo = partial(int, base=2)
Armin Rigo5ed262b2005-12-06 18:32:37 +000048 >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
Raymond Hettinger9c323f82005-02-28 19:39:44 +000049 >>> basetwo('10010')
50 18
51 \end{verbatim}
52\end{funcdesc}
53
Guido van Rossum0919a1a2006-08-26 20:49:04 +000054\begin{funcdesc}{reduce}{function, sequence\optional{, initializer}}
55 Apply \var{function} of two arguments cumulatively to the items of
56 \var{sequence}, from left to right, so as to reduce the sequence to
57 a single value. For example, \code{reduce(lambda x, y: x+y, [1, 2,
58 3, 4, 5])} calculates \code{((((1+2)+3)+4)+5)}. The left argument,
59 \var{x}, is the accumulated value and the right argument, \var{y},
60 is the update value from the \var{sequence}. If the optional
61 \var{initializer} is present, it is placed before the items of the
62 sequence in the calculation, and serves as a default when the
63 sequence is empty. If \var{initializer} is not given and
64 \var{sequence} contains only one item, the first item is returned.
65\end{funcdesc}
66
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000067\begin{funcdesc}{update_wrapper}
68{wrapper, wrapped\optional{, assigned}\optional{, updated}}
69Update a wrapper function to look like the wrapped function. The optional
70arguments are tuples to specify which attributes of the original
71function are assigned directly to the matching attributes on the wrapper
72function and which attributes of the wrapper function are updated with
73the corresponding attributes from the original function. The default
74values for these arguments are the module level constants
75\var{WRAPPER_ASSIGNMENTS} (which assigns to the wrapper function's name,
76module and documentation string) and \var{WRAPPER_UPDATES} (which
77updates the wrapper function's instance dictionary).
78
79The main intended use for this function is in decorator functions
80which wrap the decorated function and return the wrapper. If the
81wrapper function is not updated, the metadata of the returned function
82will reflect the wrapper definition rather than the original function
83definition, which is typically less than helpful.
84\end{funcdesc}
85
86\begin{funcdesc}{wraps}
87{wrapped\optional{, assigned}\optional{, updated}}
88This is a convenience function for invoking
89\code{partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)}
90as a function decorator when defining a wrapper function. For example:
91 \begin{verbatim}
92 >>> def my_decorator(f):
93 ... @wraps(f)
94 ... def wrapper(*args, **kwds):
95 ... print 'Calling decorated function'
96 ... return f(*args, **kwds)
97 ... return wrapper
98 ...
99 >>> @my_decorator
100 ... def example():
101 ... print 'Called example function'
102 ...
103 >>> example()
104 Calling decorated function
105 Called example function
106 >>> example.__name__
107 'example'
108 \end{verbatim}
109Without the use of this decorator factory, the name of the example
110function would have been \code{'wrapper'}.
111\end{funcdesc}
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000112
113
114\subsection{\class{partial} Objects \label{partial-objects}}
115
116
117\class{partial} objects are callable objects created by \function{partial()}.
118They have three read-only attributes:
119
120\begin{memberdesc}[callable]{func}{}
121A callable object or function. Calls to the \class{partial} object will
122be forwarded to \member{func} with new arguments and keywords.
123\end{memberdesc}
124
125\begin{memberdesc}[tuple]{args}{}
126The leftmost positional arguments that will be prepended to the
127positional arguments provided to a \class{partial} object call.
128\end{memberdesc}
129
130\begin{memberdesc}[dict]{keywords}{}
131The keyword arguments that will be supplied when the \class{partial} object
132is called.
133\end{memberdesc}
Raymond Hettinger3e1dd3b2005-03-08 07:15:36 +0000134
135\class{partial} objects are like \class{function} objects in that they are
136callable, weak referencable, and can have attributes. There are some
137important differences. For instance, the \member{__name__} and
138\member{__doc__} attributes are not created automatically. Also,
139\class{partial} objects defined in classes behave like static methods and
140do not transform into bound methods during instance attribute look-up.