Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
| 12 | .. versionadded:: 2.5 |
| 13 | |
| 14 | The :mod:`functools` module is for higher-order functions: functions that act on |
| 15 | or return other functions. In general, any callable object can be treated as a |
| 16 | function for the purposes of this module. |
| 17 | |
| 18 | The :mod:`functools` module defines the following function: |
| 19 | |
| 20 | |
| 21 | .. function:: partial(func[,*args][, **keywords]) |
| 22 | |
| 23 | Return a new :class:`partial` object which when called will behave like *func* |
| 24 | called with the positional arguments *args* and keyword arguments *keywords*. If |
| 25 | more arguments are supplied to the call, they are appended to *args*. If |
| 26 | additional keyword arguments are supplied, they extend and override *keywords*. |
| 27 | Roughly equivalent to:: |
| 28 | |
| 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 | |
| 39 | The :func:`partial` is used for partial function application which "freezes" |
| 40 | some portion of a function's arguments and/or keywords resulting in a new object |
| 41 | with a simplified signature. For example, :func:`partial` can be used to create |
| 42 | a callable that behaves like the :func:`int` function where the *base* argument |
| 43 | defaults to two:: |
| 44 | |
| 45 | >>> basetwo = partial(int, base=2) |
| 46 | >>> basetwo.__doc__ = 'Convert base 2 string to an int.' |
| 47 | >>> basetwo('10010') |
| 48 | 18 |
| 49 | |
| 50 | |
| 51 | .. function:: update_wrapper(wrapper, wrapped[, assigned][, updated]) |
| 52 | |
| 53 | Update a *wrapper* function to look like the *wrapped* function. The optional |
| 54 | arguments are tuples to specify which attributes of the original function are |
| 55 | assigned directly to the matching attributes on the wrapper function and which |
| 56 | attributes of the wrapper function are updated with the corresponding attributes |
| 57 | from the original function. The default values for these arguments are the |
| 58 | module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper |
| 59 | function's *__name__*, *__module__* and *__doc__*, the documentation string) and |
| 60 | *WRAPPER_UPDATES* (which updates the wrapper function's *__dict__*, i.e. the |
| 61 | instance dictionary). |
| 62 | |
| 63 | The main intended use for this function is in decorator functions which wrap the |
| 64 | decorated function and return the wrapper. If the wrapper function is not |
| 65 | updated, the metadata of the returned function will reflect the wrapper |
| 66 | definition rather than the original function definition, which is typically less |
| 67 | than helpful. |
| 68 | |
| 69 | |
| 70 | .. function:: wraps(wrapped[, assigned][, updated]) |
| 71 | |
| 72 | This is a convenience function for invoking ``partial(update_wrapper, |
| 73 | wrapped=wrapped, assigned=assigned, updated=updated)`` as a function decorator |
| 74 | when defining a wrapper function. For example:: |
| 75 | |
| 76 | >>> def my_decorator(f): |
| 77 | ... @wraps(f) |
| 78 | ... def wrapper(*args, **kwds): |
| 79 | ... print 'Calling decorated function' |
| 80 | ... return f(*args, **kwds) |
| 81 | ... return wrapper |
| 82 | ... |
| 83 | >>> @my_decorator |
| 84 | ... def example(): |
| 85 | ... """Docstring""" |
| 86 | ... print 'Called example function' |
| 87 | ... |
| 88 | >>> example() |
| 89 | Calling decorated function |
| 90 | Called example function |
| 91 | >>> example.__name__ |
| 92 | 'example' |
| 93 | >>> example.__doc__ |
| 94 | 'Docstring' |
| 95 | |
| 96 | Without the use of this decorator factory, the name of the example function |
| 97 | would have been ``'wrapper'``, and the docstring of the original :func:`example` |
| 98 | would have been lost. |
| 99 | |
| 100 | |
| 101 | .. _partial-objects: |
| 102 | |
| 103 | :class:`partial` Objects |
| 104 | ------------------------ |
| 105 | |
| 106 | :class:`partial` objects are callable objects created by :func:`partial`. They |
| 107 | have three read-only attributes: |
| 108 | |
| 109 | |
| 110 | .. attribute:: partial.func |
| 111 | |
| 112 | A callable object or function. Calls to the :class:`partial` object will be |
| 113 | forwarded to :attr:`func` with new arguments and keywords. |
| 114 | |
| 115 | |
| 116 | .. attribute:: partial.args |
| 117 | |
| 118 | The leftmost positional arguments that will be prepended to the positional |
| 119 | arguments provided to a :class:`partial` object call. |
| 120 | |
| 121 | |
| 122 | .. attribute:: partial.keywords |
| 123 | |
| 124 | The keyword arguments that will be supplied when the :class:`partial` object is |
| 125 | called. |
| 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 important |
| 129 | differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes |
| 130 | are not created automatically. Also, :class:`partial` objects defined in |
| 131 | classes behave like static methods and do not transform into bound methods |
| 132 | during instance attribute look-up. |
| 133 | |