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