Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`functools` --- Higher order functions and operations on callable objects |
| 2 | ============================================================================== |
| 3 | |
| 4 | .. module:: functools |
| 5 | :synopsis: Higher order functions and operations on callable objects. |
| 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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | The :mod:`functools` module is for higher-order functions: functions that act on |
| 13 | or return other functions. In general, any callable object can be treated as a |
| 14 | function for the purposes of this module. |
| 15 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 16 | The :mod:`functools` module defines the following functions: |
| 17 | |
| 18 | |
| 19 | .. function:: reduce(function, iterable[, initializer]) |
| 20 | |
| 21 | This is the same function as :func:`reduce`. It is made available in this module |
| 22 | to allow writing code more forward-compatible with Python 3. |
| 23 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
| 25 | .. function:: partial(func[,*args][, **keywords]) |
| 26 | |
| 27 | Return a new :class:`partial` object which when called will behave like *func* |
| 28 | called with the positional arguments *args* and keyword arguments *keywords*. If |
| 29 | more arguments are supplied to the call, they are appended to *args*. If |
| 30 | additional keyword arguments are supplied, they extend and override *keywords*. |
| 31 | Roughly equivalent to:: |
| 32 | |
| 33 | def partial(func, *args, **keywords): |
| 34 | def newfunc(*fargs, **fkeywords): |
| 35 | newkeywords = keywords.copy() |
| 36 | newkeywords.update(fkeywords) |
| 37 | return func(*(args + fargs), **newkeywords) |
| 38 | newfunc.func = func |
| 39 | newfunc.args = args |
| 40 | newfunc.keywords = keywords |
| 41 | return newfunc |
| 42 | |
| 43 | The :func:`partial` is used for partial function application which "freezes" |
| 44 | some portion of a function's arguments and/or keywords resulting in a new object |
| 45 | with a simplified signature. For example, :func:`partial` can be used to create |
| 46 | a callable that behaves like the :func:`int` function where the *base* argument |
| 47 | defaults to two:: |
| 48 | |
| 49 | >>> basetwo = partial(int, base=2) |
| 50 | >>> basetwo.__doc__ = 'Convert base 2 string to an int.' |
| 51 | >>> basetwo('10010') |
| 52 | 18 |
| 53 | |
| 54 | |
| 55 | .. function:: reduce(function, sequence[, initializer]) |
| 56 | |
| 57 | Apply *function* of two arguments cumulatively to the items of *sequence*, from |
| 58 | left to right, so as to reduce the sequence to a single value. For example, |
| 59 | ``reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])`` calculates ``((((1+2)+3)+4)+5)``. |
| 60 | The left argument, *x*, is the accumulated value and the right argument, *y*, is |
| 61 | the update value from the *sequence*. If the optional *initializer* is present, |
| 62 | it is placed before the items of the sequence in the calculation, and serves as |
| 63 | a default when the sequence is empty. If *initializer* is not given and |
| 64 | *sequence* contains only one item, the first item is returned. |
| 65 | |
| 66 | |
| 67 | .. function:: update_wrapper(wrapper, wrapped[, assigned][, updated]) |
| 68 | |
| 69 | Update a *wrapper* function to look like the *wrapped* function. The optional |
| 70 | arguments are tuples to specify which attributes of the original function are |
| 71 | assigned directly to the matching attributes on the wrapper function and which |
| 72 | attributes of the wrapper function are updated with the corresponding attributes |
| 73 | from the original function. The default values for these arguments are the |
| 74 | module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper |
| 75 | function's *__name__*, *__module__* and *__doc__*, the documentation string) and |
| 76 | *WRAPPER_UPDATES* (which updates the wrapper function's *__dict__*, i.e. the |
| 77 | instance dictionary). |
| 78 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 79 | The main intended use for this function is in :term:`decorator` functions which |
| 80 | wrap the decorated function and return the wrapper. If the wrapper function is |
| 81 | not updated, the metadata of the returned function will reflect the wrapper |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | definition rather than the original function definition, which is typically less |
| 83 | than helpful. |
| 84 | |
| 85 | |
| 86 | .. function:: wraps(wrapped[, assigned][, updated]) |
| 87 | |
| 88 | This is a convenience function for invoking ``partial(update_wrapper, |
| 89 | wrapped=wrapped, assigned=assigned, updated=updated)`` as a function decorator |
| 90 | when defining a wrapper function. For example:: |
| 91 | |
| 92 | >>> def my_decorator(f): |
| 93 | ... @wraps(f) |
| 94 | ... def wrapper(*args, **kwds): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 95 | ... print('Calling decorated function') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | ... return f(*args, **kwds) |
| 97 | ... return wrapper |
| 98 | ... |
| 99 | >>> @my_decorator |
| 100 | ... def example(): |
| 101 | ... """Docstring""" |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 102 | ... print('Called example function') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 103 | ... |
| 104 | >>> example() |
| 105 | Calling decorated function |
| 106 | Called example function |
| 107 | >>> example.__name__ |
| 108 | 'example' |
| 109 | >>> example.__doc__ |
| 110 | 'Docstring' |
| 111 | |
| 112 | Without the use of this decorator factory, the name of the example function |
| 113 | would have been ``'wrapper'``, and the docstring of the original :func:`example` |
| 114 | would have been lost. |
| 115 | |
| 116 | |
| 117 | .. _partial-objects: |
| 118 | |
| 119 | :class:`partial` Objects |
| 120 | ------------------------ |
| 121 | |
| 122 | :class:`partial` objects are callable objects created by :func:`partial`. They |
| 123 | have three read-only attributes: |
| 124 | |
| 125 | |
| 126 | .. attribute:: partial.func |
| 127 | |
| 128 | A callable object or function. Calls to the :class:`partial` object will be |
| 129 | forwarded to :attr:`func` with new arguments and keywords. |
| 130 | |
| 131 | |
| 132 | .. attribute:: partial.args |
| 133 | |
| 134 | The leftmost positional arguments that will be prepended to the positional |
| 135 | arguments provided to a :class:`partial` object call. |
| 136 | |
| 137 | |
| 138 | .. attribute:: partial.keywords |
| 139 | |
| 140 | The keyword arguments that will be supplied when the :class:`partial` object is |
| 141 | called. |
| 142 | |
| 143 | :class:`partial` objects are like :class:`function` objects in that they are |
| 144 | callable, weak referencable, and can have attributes. There are some important |
| 145 | differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes |
| 146 | are not created automatically. Also, :class:`partial` objects defined in |
| 147 | classes behave like static methods and do not transform into bound methods |
| 148 | during instance attribute look-up. |
| 149 | |