Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 1 | \section{\module{functools} --- |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 2 | Higher order functions and operations on callable objects.} |
| 3 | |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{functools} % standard library, in Python |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 5 | |
| 6 | \moduleauthor{Peter Harris}{scav@blueyonder.co.uk} |
| 7 | \moduleauthor{Raymond Hettinger}{python@rcn.com} |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 8 | \moduleauthor{Nick Coghlan}{ncoghlan@gmail.com} |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 9 | \sectionauthor{Peter Harris}{scav@blueyonder.co.uk} |
| 10 | |
| 11 | \modulesynopsis{Higher-order functions and operations on callable objects.} |
| 12 | |
Raymond Hettinger | 6a458e9 | 2005-03-02 15:10:38 +0000 | [diff] [blame] | 13 | \versionadded{2.5} |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 14 | |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 15 | The \module{functools} module is for higher-order functions: functions |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 16 | that act on or return other functions. In general, any callable object can |
| 17 | be treated as a function for the purposes of this module. |
| 18 | |
| 19 | |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 20 | The \module{functools} module defines the following function: |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 21 | |
| 22 | \begin{funcdesc}{partial}{func\optional{,*args}\optional{, **keywords}} |
| 23 | Return a new \class{partial} object which when called will behave like |
| 24 | \var{func} called with the positional arguments \var{args} and keyword |
| 25 | arguments \var{keywords}. If more arguments are supplied to the call, they |
| 26 | are appended to \var{args}. If additional keyword arguments are supplied, |
| 27 | they 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 | |
| 40 | The \function{partial} is used for partial function application which |
| 41 | ``freezes'' some portion of a function's arguments and/or keywords |
Walter Dörwald | 769f821 | 2005-04-14 20:08:59 +0000 | [diff] [blame] | 42 | resulting in a new object with a simplified signature. For example, |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 43 | \function{partial} can be used to create a callable that behaves like |
| 44 | the \function{int} function where the \var{base} argument defaults to |
| 45 | two: |
| 46 | \begin{verbatim} |
| 47 | >>> basetwo = partial(int, base=2) |
Armin Rigo | 5ed262b | 2005-12-06 18:32:37 +0000 | [diff] [blame] | 48 | >>> basetwo.__doc__ = 'Convert base 2 string to an int.' |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 49 | >>> basetwo('10010') |
| 50 | 18 |
| 51 | \end{verbatim} |
| 52 | \end{funcdesc} |
| 53 | |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 54 | \begin{funcdesc}{update_wrapper} |
| 55 | {wrapper, wrapped\optional{, assigned}\optional{, updated}} |
Georg Brandl | d2bf6c0 | 2007-02-15 10:38:03 +0000 | [diff] [blame^] | 56 | Update a \var{wrapper} function to look like the \var{wrapped} function. |
| 57 | The optional arguments are tuples to specify which attributes of the original |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 58 | function are assigned directly to the matching attributes on the wrapper |
| 59 | function and which attributes of the wrapper function are updated with |
| 60 | the corresponding attributes from the original function. The default |
| 61 | values for these arguments are the module level constants |
Georg Brandl | d2bf6c0 | 2007-02-15 10:38:03 +0000 | [diff] [blame^] | 62 | \var{WRAPPER_ASSIGNMENTS} (which assigns to the wrapper function's |
| 63 | \var{__name__}, \var{__module__} and \var{__doc__}, the documentation string) |
| 64 | and \var{WRAPPER_UPDATES} (which updates the wrapper function's \var{__dict__}, |
| 65 | i.e. the instance dictionary). |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 66 | |
| 67 | The main intended use for this function is in decorator functions |
| 68 | which wrap the decorated function and return the wrapper. If the |
| 69 | wrapper function is not updated, the metadata of the returned function |
| 70 | will reflect the wrapper definition rather than the original function |
| 71 | definition, which is typically less than helpful. |
| 72 | \end{funcdesc} |
| 73 | |
| 74 | \begin{funcdesc}{wraps} |
| 75 | {wrapped\optional{, assigned}\optional{, updated}} |
| 76 | This is a convenience function for invoking |
| 77 | \code{partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)} |
| 78 | as a function decorator when defining a wrapper function. For example: |
| 79 | \begin{verbatim} |
| 80 | >>> def my_decorator(f): |
| 81 | ... @wraps(f) |
| 82 | ... def wrapper(*args, **kwds): |
| 83 | ... print 'Calling decorated function' |
| 84 | ... return f(*args, **kwds) |
| 85 | ... return wrapper |
| 86 | ... |
| 87 | >>> @my_decorator |
| 88 | ... def example(): |
Georg Brandl | d2bf6c0 | 2007-02-15 10:38:03 +0000 | [diff] [blame^] | 89 | ... """Docstring""" |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 90 | ... print 'Called example function' |
| 91 | ... |
| 92 | >>> example() |
| 93 | Calling decorated function |
| 94 | Called example function |
| 95 | >>> example.__name__ |
| 96 | 'example' |
Georg Brandl | d2bf6c0 | 2007-02-15 10:38:03 +0000 | [diff] [blame^] | 97 | >>> example.__doc__ |
| 98 | 'Docstring' |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 99 | \end{verbatim} |
| 100 | Without the use of this decorator factory, the name of the example |
Georg Brandl | d2bf6c0 | 2007-02-15 10:38:03 +0000 | [diff] [blame^] | 101 | function would have been \code{'wrapper'}, and the docstring of the |
| 102 | original \function{example()} would have been lost. |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | \subsection{\class{partial} Objects \label{partial-objects}} |
| 107 | |
| 108 | |
| 109 | \class{partial} objects are callable objects created by \function{partial()}. |
| 110 | They have three read-only attributes: |
| 111 | |
| 112 | \begin{memberdesc}[callable]{func}{} |
| 113 | A callable object or function. Calls to the \class{partial} object will |
| 114 | be forwarded to \member{func} with new arguments and keywords. |
| 115 | \end{memberdesc} |
| 116 | |
| 117 | \begin{memberdesc}[tuple]{args}{} |
| 118 | The leftmost positional arguments that will be prepended to the |
| 119 | positional arguments provided to a \class{partial} object call. |
| 120 | \end{memberdesc} |
| 121 | |
| 122 | \begin{memberdesc}[dict]{keywords}{} |
| 123 | The keyword arguments that will be supplied when the \class{partial} object |
| 124 | is called. |
| 125 | \end{memberdesc} |
Raymond Hettinger | 3e1dd3b | 2005-03-08 07:15:36 +0000 | [diff] [blame] | 126 | |
| 127 | \class{partial} objects are like \class{function} objects in that they are |
| 128 | callable, weak referencable, and can have attributes. There are some |
| 129 | important differences. For instance, the \member{__name__} and |
| 130 | \member{__doc__} attributes are not created automatically. Also, |
| 131 | \class{partial} objects defined in classes behave like static methods and |
| 132 | do not transform into bound methods during instance attribute look-up. |