source: vendor/python/2.5/Doc/lib/libfunctools.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 5.1 KB
Line 
1\section{\module{functools} ---
2 Higher order functions and operations on callable objects.}
3
4\declaremodule{standard}{functools} % standard library, in Python
5
6\moduleauthor{Peter Harris}{scav@blueyonder.co.uk}
7\moduleauthor{Raymond Hettinger}{python@rcn.com}
8\moduleauthor{Nick Coghlan}{ncoghlan@gmail.com}
9\sectionauthor{Peter Harris}{scav@blueyonder.co.uk}
10
11\modulesynopsis{Higher-order functions and operations on callable objects.}
12
13\versionadded{2.5}
14
15The \module{functools} module is for higher-order functions: functions
16that 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
20The \module{functools} module defines the following function:
21
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
42resulting in a new object with a simplified signature. For example,
43\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)
48 >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
49 >>> basetwo('10010')
50 18
51 \end{verbatim}
52\end{funcdesc}
53
54\begin{funcdesc}{update_wrapper}
55{wrapper, wrapped\optional{, assigned}\optional{, updated}}
56Update a wrapper function to look like the wrapped function. The optional
57arguments are tuples to specify which attributes of the original
58function are assigned directly to the matching attributes on the wrapper
59function and which attributes of the wrapper function are updated with
60the corresponding attributes from the original function. The default
61values for these arguments are the module level constants
62\var{WRAPPER_ASSIGNMENTS} (which assigns to the wrapper function's name,
63module and documentation string) and \var{WRAPPER_UPDATES} (which
64updates the wrapper function's instance dictionary).
65
66The main intended use for this function is in decorator functions
67which wrap the decorated function and return the wrapper. If the
68wrapper function is not updated, the metadata of the returned function
69will reflect the wrapper definition rather than the original function
70definition, which is typically less than helpful.
71\end{funcdesc}
72
73\begin{funcdesc}{wraps}
74{wrapped\optional{, assigned}\optional{, updated}}
75This is a convenience function for invoking
76\code{partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)}
77as a function decorator when defining a wrapper function. For example:
78 \begin{verbatim}
79 >>> def my_decorator(f):
80 ... @wraps(f)
81 ... def wrapper(*args, **kwds):
82 ... print 'Calling decorated function'
83 ... return f(*args, **kwds)
84 ... return wrapper
85 ...
86 >>> @my_decorator
87 ... def example():
88 ... print 'Called example function'
89 ...
90 >>> example()
91 Calling decorated function
92 Called example function
93 >>> example.__name__
94 'example'
95 \end{verbatim}
96Without the use of this decorator factory, the name of the example
97function would have been \code{'wrapper'}.
98\end{funcdesc}
99
100
101\subsection{\class{partial} Objects \label{partial-objects}}
102
103
104\class{partial} objects are callable objects created by \function{partial()}.
105They have three read-only attributes:
106
107\begin{memberdesc}[callable]{func}{}
108A callable object or function. Calls to the \class{partial} object will
109be forwarded to \member{func} with new arguments and keywords.
110\end{memberdesc}
111
112\begin{memberdesc}[tuple]{args}{}
113The leftmost positional arguments that will be prepended to the
114positional arguments provided to a \class{partial} object call.
115\end{memberdesc}
116
117\begin{memberdesc}[dict]{keywords}{}
118The keyword arguments that will be supplied when the \class{partial} object
119is called.
120\end{memberdesc}
121
122\class{partial} objects are like \class{function} objects in that they are
123callable, weak referencable, and can have attributes. There are some
124important differences. For instance, the \member{__name__} and
125\member{__doc__} attributes are not created automatically. Also,
126\class{partial} objects defined in classes behave like static methods and
127do not transform into bound methods during instance attribute look-up.
Note: See TracBrowser for help on using the repository browser.