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 | |
Raymond Hettinger | e0e0822 | 2010-11-06 07:10:31 +0000 | [diff] [blame] | 18 | .. seealso:: |
| 19 | |
| 20 | Latest version of the `functools Python source code |
| 21 | <http://svn.python.org/view/python/branches/release27-maint/Lib/functools.py?view=markup>`_ |
| 22 | |
Georg Brandl | ae0ee8a | 2007-08-28 08:29:08 +0000 | [diff] [blame] | 23 | The :mod:`functools` module defines the following functions: |
| 24 | |
Raymond Hettinger | bb006cf | 2010-04-04 21:45:01 +0000 | [diff] [blame] | 25 | .. function:: cmp_to_key(func) |
Raymond Hettinger | a551f31 | 2010-04-04 18:34:45 +0000 | [diff] [blame] | 26 | |
Benjamin Peterson | d74ca12 | 2010-08-09 02:17:24 +0000 | [diff] [blame] | 27 | Transform an old-style comparison function to a key-function. Used with |
| 28 | tools that accept key functions (such as :func:`sorted`, :func:`min`, |
| 29 | :func:`max`, :func:`heapq.nlargest`, :func:`heapq.nsmallest`, |
| 30 | :func:`itertools.groupby`). This function is primarily used as a transition |
| 31 | tool for programs being converted to Py3.x where comparison functions are no |
| 32 | longer supported. |
Raymond Hettinger | a551f31 | 2010-04-04 18:34:45 +0000 | [diff] [blame] | 33 | |
Benjamin Peterson | d74ca12 | 2010-08-09 02:17:24 +0000 | [diff] [blame] | 34 | A compare function is any callable that accept two arguments, compares them, |
| 35 | and returns a negative number for less-than, zero for equality, or a positive |
| 36 | number for greater-than. A key function is a callable that accepts one |
| 37 | argument and returns another value that indicates the position in the desired |
| 38 | collation sequence. |
Raymond Hettinger | a551f31 | 2010-04-04 18:34:45 +0000 | [diff] [blame] | 39 | |
Benjamin Peterson | d74ca12 | 2010-08-09 02:17:24 +0000 | [diff] [blame] | 40 | Example:: |
Raymond Hettinger | a551f31 | 2010-04-04 18:34:45 +0000 | [diff] [blame] | 41 | |
Benjamin Peterson | d74ca12 | 2010-08-09 02:17:24 +0000 | [diff] [blame] | 42 | sorted(iterable, key=cmp_to_key(locale.strcoll)) # locale-aware sort order |
Raymond Hettinger | a551f31 | 2010-04-04 18:34:45 +0000 | [diff] [blame] | 43 | |
| 44 | .. versionadded:: 2.7 |
| 45 | |
Raymond Hettinger | 20ae90d | 2010-04-04 01:24:59 +0000 | [diff] [blame] | 46 | .. function:: total_ordering(cls) |
| 47 | |
| 48 | Given a class defining one or more rich comparison ordering methods, this |
Andrew M. Kuchling | 884d0a3 | 2010-04-11 12:48:08 +0000 | [diff] [blame] | 49 | class decorator supplies the rest. This simplifies the effort involved |
Raymond Hettinger | 20ae90d | 2010-04-04 01:24:59 +0000 | [diff] [blame] | 50 | in specifying all of the possible rich comparison operations: |
| 51 | |
| 52 | The class must define one of :meth:`__lt__`, :meth:`__le__`, |
| 53 | :meth:`__gt__`, or :meth:`__ge__`. |
| 54 | In addition, the class should supply an :meth:`__eq__` method. |
| 55 | |
| 56 | For example:: |
| 57 | |
| 58 | @total_ordering |
| 59 | class Student: |
| 60 | def __eq__(self, other): |
| 61 | return ((self.lastname.lower(), self.firstname.lower()) == |
| 62 | (other.lastname.lower(), other.firstname.lower())) |
| 63 | def __lt__(self, other): |
| 64 | return ((self.lastname.lower(), self.firstname.lower()) < |
| 65 | (other.lastname.lower(), other.firstname.lower())) |
Georg Brandl | ae0ee8a | 2007-08-28 08:29:08 +0000 | [diff] [blame] | 66 | |
Raymond Hettinger | 0d57caa | 2010-04-04 07:33:46 +0000 | [diff] [blame] | 67 | .. versionadded:: 2.7 |
| 68 | |
Georg Brandl | ae0ee8a | 2007-08-28 08:29:08 +0000 | [diff] [blame] | 69 | .. function:: reduce(function, iterable[, initializer]) |
| 70 | |
| 71 | This is the same function as :func:`reduce`. It is made available in this module |
| 72 | to allow writing code more forward-compatible with Python 3. |
| 73 | |
| 74 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | .. function:: partial(func[,*args][, **keywords]) |
| 78 | |
| 79 | Return a new :class:`partial` object which when called will behave like *func* |
| 80 | called with the positional arguments *args* and keyword arguments *keywords*. If |
| 81 | more arguments are supplied to the call, they are appended to *args*. If |
| 82 | additional keyword arguments are supplied, they extend and override *keywords*. |
| 83 | Roughly equivalent to:: |
| 84 | |
| 85 | def partial(func, *args, **keywords): |
| 86 | def newfunc(*fargs, **fkeywords): |
| 87 | newkeywords = keywords.copy() |
| 88 | newkeywords.update(fkeywords) |
| 89 | return func(*(args + fargs), **newkeywords) |
| 90 | newfunc.func = func |
| 91 | newfunc.args = args |
| 92 | newfunc.keywords = keywords |
| 93 | return newfunc |
| 94 | |
| 95 | The :func:`partial` is used for partial function application which "freezes" |
| 96 | some portion of a function's arguments and/or keywords resulting in a new object |
| 97 | with a simplified signature. For example, :func:`partial` can be used to create |
| 98 | a callable that behaves like the :func:`int` function where the *base* argument |
Georg Brandl | e8f1b00 | 2008-03-22 22:04:10 +0000 | [diff] [blame] | 99 | defaults to two: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 100 | |
Georg Brandl | e8f1b00 | 2008-03-22 22:04:10 +0000 | [diff] [blame] | 101 | >>> from functools import partial |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 102 | >>> basetwo = partial(int, base=2) |
| 103 | >>> basetwo.__doc__ = 'Convert base 2 string to an int.' |
| 104 | >>> basetwo('10010') |
| 105 | 18 |
| 106 | |
| 107 | |
| 108 | .. function:: update_wrapper(wrapper, wrapped[, assigned][, updated]) |
| 109 | |
| 110 | Update a *wrapper* function to look like the *wrapped* function. The optional |
| 111 | arguments are tuples to specify which attributes of the original function are |
| 112 | assigned directly to the matching attributes on the wrapper function and which |
| 113 | attributes of the wrapper function are updated with the corresponding attributes |
| 114 | from the original function. The default values for these arguments are the |
| 115 | module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper |
| 116 | function's *__name__*, *__module__* and *__doc__*, the documentation string) and |
| 117 | *WRAPPER_UPDATES* (which updates the wrapper function's *__dict__*, i.e. the |
| 118 | instance dictionary). |
| 119 | |
Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 120 | The main intended use for this function is in :term:`decorator` functions which |
| 121 | wrap the decorated function and return the wrapper. If the wrapper function is |
| 122 | not updated, the metadata of the returned function will reflect the wrapper |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 123 | definition rather than the original function definition, which is typically less |
| 124 | than helpful. |
| 125 | |
| 126 | |
| 127 | .. function:: wraps(wrapped[, assigned][, updated]) |
| 128 | |
| 129 | This is a convenience function for invoking ``partial(update_wrapper, |
| 130 | wrapped=wrapped, assigned=assigned, updated=updated)`` as a function decorator |
Georg Brandl | e8f1b00 | 2008-03-22 22:04:10 +0000 | [diff] [blame] | 131 | when defining a wrapper function. For example: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 132 | |
Georg Brandl | e8f1b00 | 2008-03-22 22:04:10 +0000 | [diff] [blame] | 133 | >>> from functools import wraps |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 134 | >>> def my_decorator(f): |
| 135 | ... @wraps(f) |
| 136 | ... def wrapper(*args, **kwds): |
| 137 | ... print 'Calling decorated function' |
| 138 | ... return f(*args, **kwds) |
| 139 | ... return wrapper |
| 140 | ... |
| 141 | >>> @my_decorator |
| 142 | ... def example(): |
| 143 | ... """Docstring""" |
| 144 | ... print 'Called example function' |
| 145 | ... |
| 146 | >>> example() |
| 147 | Calling decorated function |
| 148 | Called example function |
| 149 | >>> example.__name__ |
| 150 | 'example' |
| 151 | >>> example.__doc__ |
| 152 | 'Docstring' |
| 153 | |
| 154 | Without the use of this decorator factory, the name of the example function |
| 155 | would have been ``'wrapper'``, and the docstring of the original :func:`example` |
| 156 | would have been lost. |
| 157 | |
| 158 | |
| 159 | .. _partial-objects: |
| 160 | |
| 161 | :class:`partial` Objects |
| 162 | ------------------------ |
| 163 | |
| 164 | :class:`partial` objects are callable objects created by :func:`partial`. They |
| 165 | have three read-only attributes: |
| 166 | |
| 167 | |
| 168 | .. attribute:: partial.func |
| 169 | |
| 170 | A callable object or function. Calls to the :class:`partial` object will be |
| 171 | forwarded to :attr:`func` with new arguments and keywords. |
| 172 | |
| 173 | |
| 174 | .. attribute:: partial.args |
| 175 | |
| 176 | The leftmost positional arguments that will be prepended to the positional |
| 177 | arguments provided to a :class:`partial` object call. |
| 178 | |
| 179 | |
| 180 | .. attribute:: partial.keywords |
| 181 | |
| 182 | The keyword arguments that will be supplied when the :class:`partial` object is |
| 183 | called. |
| 184 | |
| 185 | :class:`partial` objects are like :class:`function` objects in that they are |
| 186 | callable, weak referencable, and can have attributes. There are some important |
| 187 | differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes |
| 188 | are not created automatically. Also, :class:`partial` objects defined in |
| 189 | classes behave like static methods and do not transform into bound methods |
| 190 | during instance attribute look-up. |
| 191 | |