Georg Brandl | 6c89a79 | 2012-01-25 22:36:25 +0100 | [diff] [blame] | 1 | :mod:`functools` --- Higher-order functions and operations on callable objects |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | ============================================================================== |
| 3 | |
| 4 | .. module:: functools |
Georg Brandl | 6c89a79 | 2012-01-25 22:36:25 +0100 | [diff] [blame] | 5 | :synopsis: Higher-order functions and operations on callable objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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> |
Łukasz Langa | 6f69251 | 2013-06-05 12:20:24 +0200 | [diff] [blame] | 9 | .. moduleauthor:: Łukasz Langa <lukasz@langa.pl> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | .. sectionauthor:: Peter Harris <scav@blueyonder.co.uk> |
| 11 | |
Raymond Hettinger | 05ce079 | 2011-01-10 21:16:07 +0000 | [diff] [blame] | 12 | **Source code:** :source:`Lib/functools.py` |
| 13 | |
| 14 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | The :mod:`functools` module is for higher-order functions: functions that act on |
| 17 | or return other functions. In general, any callable object can be treated as a |
| 18 | function for the purposes of this module. |
| 19 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 20 | The :mod:`functools` module defines the following functions: |
| 21 | |
Éric Araujo | b10089e | 2010-11-18 14:22:08 +0000 | [diff] [blame] | 22 | .. function:: cmp_to_key(func) |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 23 | |
Raymond Hettinger | 86e9b6b | 2014-11-09 17:20:56 -0800 | [diff] [blame] | 24 | Transform an old-style comparison function to a :term:`key function`. Used |
| 25 | with tools that accept key functions (such as :func:`sorted`, :func:`min`, |
Benjamin Peterson | cca6531 | 2010-08-09 02:13:10 +0000 | [diff] [blame] | 26 | :func:`max`, :func:`heapq.nlargest`, :func:`heapq.nsmallest`, |
| 27 | :func:`itertools.groupby`). This function is primarily used as a transition |
Ezio Melotti | 9ecb6be | 2012-01-16 08:28:54 +0200 | [diff] [blame] | 28 | tool for programs being converted from Python 2 which supported the use of |
Benjamin Peterson | cca6531 | 2010-08-09 02:13:10 +0000 | [diff] [blame] | 29 | comparison functions. |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 6c89a79 | 2012-01-25 22:36:25 +0100 | [diff] [blame] | 31 | A comparison function is any callable that accept two arguments, compares them, |
Benjamin Peterson | cca6531 | 2010-08-09 02:13:10 +0000 | [diff] [blame] | 32 | and returns a negative number for less-than, zero for equality, or a positive |
| 33 | number for greater-than. A key function is a callable that accepts one |
Raymond Hettinger | 86e9b6b | 2014-11-09 17:20:56 -0800 | [diff] [blame] | 34 | argument and returns another value to be used as the sort key. |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 35 | |
Benjamin Peterson | cca6531 | 2010-08-09 02:13:10 +0000 | [diff] [blame] | 36 | Example:: |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 37 | |
Benjamin Peterson | cca6531 | 2010-08-09 02:13:10 +0000 | [diff] [blame] | 38 | sorted(iterable, key=cmp_to_key(locale.strcoll)) # locale-aware sort order |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 39 | |
Raymond Hettinger | 86e9b6b | 2014-11-09 17:20:56 -0800 | [diff] [blame] | 40 | For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`. |
| 41 | |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 42 | .. versionadded:: 3.2 |
| 43 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 44 | |
Raymond Hettinger | 010ce32 | 2012-05-19 21:20:48 -0700 | [diff] [blame] | 45 | .. decorator:: lru_cache(maxsize=128, typed=False) |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 46 | |
| 47 | Decorator to wrap a function with a memoizing callable that saves up to the |
| 48 | *maxsize* most recent calls. It can save time when an expensive or I/O bound |
| 49 | function is periodically called with the same arguments. |
| 50 | |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 51 | Since a dictionary is used to cache results, the positional and keyword |
| 52 | arguments to the function must be hashable. |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 53 | |
Raymond Hettinger | 7d74eff | 2012-06-04 00:32:15 -0700 | [diff] [blame] | 54 | If *maxsize* is set to None, the LRU feature is disabled and the cache can |
| 55 | grow without bound. The LRU feature performs best when *maxsize* is a |
| 56 | power-of-two. |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 57 | |
Raymond Hettinger | cd9fdfd | 2011-10-20 08:57:45 -0700 | [diff] [blame] | 58 | If *typed* is set to True, function arguments of different types will be |
| 59 | cached separately. For example, ``f(3)`` and ``f(3.0)`` will be treated |
| 60 | as distinct calls with distinct results. |
| 61 | |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 62 | To help measure the effectiveness of the cache and tune the *maxsize* |
| 63 | parameter, the wrapped function is instrumented with a :func:`cache_info` |
| 64 | function that returns a :term:`named tuple` showing *hits*, *misses*, |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 65 | *maxsize* and *currsize*. In a multi-threaded environment, the hits |
| 66 | and misses are approximate. |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 67 | |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 68 | The decorator also provides a :func:`cache_clear` function for clearing or |
| 69 | invalidating the cache. |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 70 | |
Raymond Hettinger | 3fccfcb | 2010-08-17 19:19:29 +0000 | [diff] [blame] | 71 | The original underlying function is accessible through the |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 72 | :attr:`__wrapped__` attribute. This is useful for introspection, for |
| 73 | bypassing the cache, or for rewrapping the function with a different cache. |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 74 | |
Raymond Hettinger | cc03858 | 2010-11-30 20:02:57 +0000 | [diff] [blame] | 75 | An `LRU (least recently used) cache |
Georg Brandl | 525d355 | 2014-10-29 10:26:56 +0100 | [diff] [blame] | 76 | <http://en.wikipedia.org/wiki/Cache_algorithms#Examples>`_ works |
Raymond Hettinger | cd9fdfd | 2011-10-20 08:57:45 -0700 | [diff] [blame] | 77 | best when the most recent calls are the best predictors of upcoming calls (for |
| 78 | example, the most popular articles on a news server tend to change each day). |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 79 | The cache's size limit assures that the cache does not grow without bound on |
| 80 | long-running processes such as web servers. |
| 81 | |
Raymond Hettinger | cc03858 | 2010-11-30 20:02:57 +0000 | [diff] [blame] | 82 | Example of an LRU cache for static web content:: |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 83 | |
Raymond Hettinger | 17328e4 | 2013-04-06 20:27:33 -0700 | [diff] [blame] | 84 | @lru_cache(maxsize=32) |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 85 | def get_pep(num): |
| 86 | 'Retrieve text of a Python Enhancement Proposal' |
| 87 | resource = 'http://www.python.org/dev/peps/pep-%04d/' % num |
| 88 | try: |
| 89 | with urllib.request.urlopen(resource) as s: |
| 90 | return s.read() |
| 91 | except urllib.error.HTTPError: |
| 92 | return 'Not Found' |
| 93 | |
| 94 | >>> for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991: |
| 95 | ... pep = get_pep(n) |
| 96 | ... print(n, len(pep)) |
| 97 | |
Raymond Hettinger | 17328e4 | 2013-04-06 20:27:33 -0700 | [diff] [blame] | 98 | >>> get_pep.cache_info() |
| 99 | CacheInfo(hits=3, misses=8, maxsize=32, currsize=8) |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 100 | |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 101 | Example of efficiently computing |
| 102 | `Fibonacci numbers <http://en.wikipedia.org/wiki/Fibonacci_number>`_ |
| 103 | using a cache to implement a |
| 104 | `dynamic programming <http://en.wikipedia.org/wiki/Dynamic_programming>`_ |
| 105 | technique:: |
| 106 | |
| 107 | @lru_cache(maxsize=None) |
| 108 | def fib(n): |
| 109 | if n < 2: |
| 110 | return n |
| 111 | return fib(n-1) + fib(n-2) |
| 112 | |
Raymond Hettinger | 17328e4 | 2013-04-06 20:27:33 -0700 | [diff] [blame] | 113 | >>> [fib(n) for n in range(16)] |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 114 | [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] |
| 115 | |
Raymond Hettinger | 17328e4 | 2013-04-06 20:27:33 -0700 | [diff] [blame] | 116 | >>> fib.cache_info() |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 117 | CacheInfo(hits=28, misses=16, maxsize=None, currsize=16) |
| 118 | |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 119 | .. versionadded:: 3.2 |
| 120 | |
Raymond Hettinger | cd9fdfd | 2011-10-20 08:57:45 -0700 | [diff] [blame] | 121 | .. versionchanged:: 3.3 |
| 122 | Added the *typed* option. |
| 123 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 124 | .. decorator:: total_ordering |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 125 | |
| 126 | Given a class defining one or more rich comparison ordering methods, this |
Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 127 | class decorator supplies the rest. This simplifies the effort involved |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 128 | in specifying all of the possible rich comparison operations: |
| 129 | |
| 130 | The class must define one of :meth:`__lt__`, :meth:`__le__`, |
| 131 | :meth:`__gt__`, or :meth:`__ge__`. |
| 132 | In addition, the class should supply an :meth:`__eq__` method. |
| 133 | |
| 134 | For example:: |
| 135 | |
| 136 | @total_ordering |
| 137 | class Student: |
Nick Coghlan | f05d981 | 2013-10-02 00:02:03 +1000 | [diff] [blame] | 138 | def _is_valid_operand(self, other): |
| 139 | return (hasattr(other, "lastname") and |
| 140 | hasattr(other, "firstname")) |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 141 | def __eq__(self, other): |
Nick Coghlan | f05d981 | 2013-10-02 00:02:03 +1000 | [diff] [blame] | 142 | if not self._is_valid_operand(other): |
| 143 | return NotImplemented |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 144 | return ((self.lastname.lower(), self.firstname.lower()) == |
| 145 | (other.lastname.lower(), other.firstname.lower())) |
| 146 | def __lt__(self, other): |
Nick Coghlan | f05d981 | 2013-10-02 00:02:03 +1000 | [diff] [blame] | 147 | if not self._is_valid_operand(other): |
| 148 | return NotImplemented |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 149 | return ((self.lastname.lower(), self.firstname.lower()) < |
| 150 | (other.lastname.lower(), other.firstname.lower())) |
| 151 | |
Nick Coghlan | f05d981 | 2013-10-02 00:02:03 +1000 | [diff] [blame] | 152 | .. note:: |
| 153 | |
| 154 | While this decorator makes it easy to create well behaved totally |
| 155 | ordered types, it *does* come at the cost of slower execution and |
| 156 | more complex stack traces for the derived comparison methods. If |
| 157 | performance benchmarking indicates this is a bottleneck for a given |
| 158 | application, implementing all six rich comparison methods instead is |
| 159 | likely to provide an easy speed boost. |
| 160 | |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 161 | .. versionadded:: 3.2 |
| 162 | |
Nick Coghlan | f05d981 | 2013-10-02 00:02:03 +1000 | [diff] [blame] | 163 | .. versionchanged:: 3.4 |
| 164 | Returning NotImplemented from the underlying comparison function for |
| 165 | unrecognised types is now supported. |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 166 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 167 | .. function:: partial(func, *args, **keywords) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
| 169 | Return a new :class:`partial` object which when called will behave like *func* |
| 170 | called with the positional arguments *args* and keyword arguments *keywords*. If |
| 171 | more arguments are supplied to the call, they are appended to *args*. If |
| 172 | additional keyword arguments are supplied, they extend and override *keywords*. |
| 173 | Roughly equivalent to:: |
| 174 | |
| 175 | def partial(func, *args, **keywords): |
| 176 | def newfunc(*fargs, **fkeywords): |
| 177 | newkeywords = keywords.copy() |
| 178 | newkeywords.update(fkeywords) |
| 179 | return func(*(args + fargs), **newkeywords) |
| 180 | newfunc.func = func |
| 181 | newfunc.args = args |
| 182 | newfunc.keywords = keywords |
| 183 | return newfunc |
| 184 | |
| 185 | The :func:`partial` is used for partial function application which "freezes" |
| 186 | some portion of a function's arguments and/or keywords resulting in a new object |
| 187 | with a simplified signature. For example, :func:`partial` can be used to create |
| 188 | a callable that behaves like the :func:`int` function where the *base* argument |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 189 | defaults to two: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 191 | >>> from functools import partial |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | >>> basetwo = partial(int, base=2) |
| 193 | >>> basetwo.__doc__ = 'Convert base 2 string to an int.' |
| 194 | >>> basetwo('10010') |
| 195 | 18 |
| 196 | |
| 197 | |
Nick Coghlan | f4cb48a | 2013-11-03 16:41:46 +1000 | [diff] [blame] | 198 | .. class:: partialmethod(func, *args, **keywords) |
| 199 | |
| 200 | Return a new :class:`partialmethod` descriptor which behaves |
| 201 | like :class:`partial` except that it is designed to be used as a method |
| 202 | definition rather than being directly callable. |
| 203 | |
| 204 | *func* must be a :term:`descriptor` or a callable (objects which are both, |
| 205 | like normal functions, are handled as descriptors). |
| 206 | |
| 207 | When *func* is a descriptor (such as a normal Python function, |
| 208 | :func:`classmethod`, :func:`staticmethod`, :func:`abstractmethod` or |
| 209 | another instance of :class:`partialmethod`), calls to ``__get__`` are |
| 210 | delegated to the underlying descriptor, and an appropriate |
| 211 | :class:`partial` object returned as the result. |
| 212 | |
| 213 | When *func* is a non-descriptor callable, an appropriate bound method is |
| 214 | created dynamically. This behaves like a normal Python function when |
| 215 | used as a method: the *self* argument will be inserted as the first |
| 216 | positional argument, even before the *args* and *keywords* supplied to |
| 217 | the :class:`partialmethod` constructor. |
| 218 | |
| 219 | Example:: |
| 220 | |
| 221 | >>> class Cell(object): |
Benjamin Peterson | 3a43403 | 2014-03-30 15:07:09 -0400 | [diff] [blame] | 222 | ... def __init__(self): |
| 223 | ... self._alive = False |
Nick Coghlan | f4cb48a | 2013-11-03 16:41:46 +1000 | [diff] [blame] | 224 | ... @property |
| 225 | ... def alive(self): |
| 226 | ... return self._alive |
| 227 | ... def set_state(self, state): |
| 228 | ... self._alive = bool(state) |
Nick Coghlan | 3daaf5f | 2013-11-04 23:32:16 +1000 | [diff] [blame] | 229 | ... set_alive = partialmethod(set_state, True) |
| 230 | ... set_dead = partialmethod(set_state, False) |
Nick Coghlan | f4cb48a | 2013-11-03 16:41:46 +1000 | [diff] [blame] | 231 | ... |
| 232 | >>> c = Cell() |
| 233 | >>> c.alive |
| 234 | False |
| 235 | >>> c.set_alive() |
| 236 | >>> c.alive |
| 237 | True |
| 238 | |
| 239 | .. versionadded:: 3.4 |
| 240 | |
| 241 | |
Georg Brandl | 58f9e4f | 2008-04-19 22:18:33 +0000 | [diff] [blame] | 242 | .. function:: reduce(function, iterable[, initializer]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 | |
| 244 | Apply *function* of two arguments cumulatively to the items of *sequence*, from |
| 245 | left to right, so as to reduce the sequence to a single value. For example, |
| 246 | ``reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])`` calculates ``((((1+2)+3)+4)+5)``. |
| 247 | The left argument, *x*, is the accumulated value and the right argument, *y*, is |
| 248 | the update value from the *sequence*. If the optional *initializer* is present, |
| 249 | it is placed before the items of the sequence in the calculation, and serves as |
| 250 | a default when the sequence is empty. If *initializer* is not given and |
| 251 | *sequence* contains only one item, the first item is returned. |
| 252 | |
Raymond Hettinger | 558dcf3 | 2014-12-16 18:16:57 -0800 | [diff] [blame] | 253 | Roughly equivalent to:: |
Raymond Hettinger | 6480168 | 2013-10-12 16:04:17 -0700 | [diff] [blame] | 254 | |
| 255 | def reduce(function, iterable, initializer=None): |
| 256 | it = iter(iterable) |
| 257 | if initializer is None: |
| 258 | value = next(it) |
| 259 | else: |
| 260 | value = initializer |
| 261 | for element in it: |
| 262 | value = function(value, element) |
| 263 | return value |
| 264 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | |
Łukasz Langa | 6f69251 | 2013-06-05 12:20:24 +0200 | [diff] [blame] | 266 | .. decorator:: singledispatch(default) |
| 267 | |
Łukasz Langa | fdcf2b7 | 2013-06-07 22:54:03 +0200 | [diff] [blame] | 268 | Transforms a function into a :term:`single-dispatch <single |
| 269 | dispatch>` :term:`generic function`. |
Łukasz Langa | 6f69251 | 2013-06-05 12:20:24 +0200 | [diff] [blame] | 270 | |
| 271 | To define a generic function, decorate it with the ``@singledispatch`` |
| 272 | decorator. Note that the dispatch happens on the type of the first argument, |
| 273 | create your function accordingly:: |
| 274 | |
| 275 | >>> from functools import singledispatch |
| 276 | >>> @singledispatch |
| 277 | ... def fun(arg, verbose=False): |
| 278 | ... if verbose: |
| 279 | ... print("Let me just say,", end=" ") |
| 280 | ... print(arg) |
| 281 | |
| 282 | To add overloaded implementations to the function, use the :func:`register` |
| 283 | attribute of the generic function. It is a decorator, taking a type |
| 284 | parameter and decorating a function implementing the operation for that |
| 285 | type:: |
| 286 | |
| 287 | >>> @fun.register(int) |
| 288 | ... def _(arg, verbose=False): |
| 289 | ... if verbose: |
| 290 | ... print("Strength in numbers, eh?", end=" ") |
| 291 | ... print(arg) |
| 292 | ... |
| 293 | >>> @fun.register(list) |
| 294 | ... def _(arg, verbose=False): |
| 295 | ... if verbose: |
| 296 | ... print("Enumerate this:") |
| 297 | ... for i, elem in enumerate(arg): |
| 298 | ... print(i, elem) |
| 299 | |
| 300 | To enable registering lambdas and pre-existing functions, the |
| 301 | :func:`register` attribute can be used in a functional form:: |
| 302 | |
| 303 | >>> def nothing(arg, verbose=False): |
| 304 | ... print("Nothing.") |
| 305 | ... |
| 306 | >>> fun.register(type(None), nothing) |
| 307 | |
| 308 | The :func:`register` attribute returns the undecorated function which |
| 309 | enables decorator stacking, pickling, as well as creating unit tests for |
| 310 | each variant independently:: |
| 311 | |
| 312 | >>> @fun.register(float) |
| 313 | ... @fun.register(Decimal) |
| 314 | ... def fun_num(arg, verbose=False): |
| 315 | ... if verbose: |
| 316 | ... print("Half of your number:", end=" ") |
| 317 | ... print(arg / 2) |
| 318 | ... |
| 319 | >>> fun_num is fun |
| 320 | False |
| 321 | |
| 322 | When called, the generic function dispatches on the type of the first |
| 323 | argument:: |
| 324 | |
| 325 | >>> fun("Hello, world.") |
| 326 | Hello, world. |
| 327 | >>> fun("test.", verbose=True) |
| 328 | Let me just say, test. |
| 329 | >>> fun(42, verbose=True) |
| 330 | Strength in numbers, eh? 42 |
| 331 | >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True) |
| 332 | Enumerate this: |
| 333 | 0 spam |
| 334 | 1 spam |
| 335 | 2 eggs |
| 336 | 3 spam |
| 337 | >>> fun(None) |
| 338 | Nothing. |
| 339 | >>> fun(1.23) |
| 340 | 0.615 |
| 341 | |
| 342 | Where there is no registered implementation for a specific type, its |
| 343 | method resolution order is used to find a more generic implementation. |
| 344 | The original function decorated with ``@singledispatch`` is registered |
| 345 | for the base ``object`` type, which means it is used if no better |
| 346 | implementation is found. |
| 347 | |
| 348 | To check which implementation will the generic function choose for |
| 349 | a given type, use the ``dispatch()`` attribute:: |
| 350 | |
| 351 | >>> fun.dispatch(float) |
| 352 | <function fun_num at 0x1035a2840> |
| 353 | >>> fun.dispatch(dict) # note: default implementation |
| 354 | <function fun at 0x103fe0000> |
| 355 | |
| 356 | To access all registered implementations, use the read-only ``registry`` |
| 357 | attribute:: |
| 358 | |
| 359 | >>> fun.registry.keys() |
| 360 | dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>, |
| 361 | <class 'decimal.Decimal'>, <class 'list'>, |
| 362 | <class 'float'>]) |
| 363 | >>> fun.registry[float] |
| 364 | <function fun_num at 0x1035a2840> |
| 365 | >>> fun.registry[object] |
| 366 | <function fun at 0x103fe0000> |
| 367 | |
| 368 | .. versionadded:: 3.4 |
| 369 | |
| 370 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 371 | .. function:: update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 372 | |
| 373 | Update a *wrapper* function to look like the *wrapped* function. The optional |
| 374 | arguments are tuples to specify which attributes of the original function are |
| 375 | assigned directly to the matching attributes on the wrapper function and which |
| 376 | attributes of the wrapper function are updated with the corresponding attributes |
| 377 | from the original function. The default values for these arguments are the |
| 378 | module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper |
Antoine Pitrou | 560f764 | 2010-08-04 18:28:02 +0000 | [diff] [blame] | 379 | function's *__name__*, *__module__*, *__annotations__* and *__doc__*, the |
| 380 | documentation string) and *WRAPPER_UPDATES* (which updates the wrapper |
| 381 | function's *__dict__*, i.e. the instance dictionary). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 383 | To allow access to the original function for introspection and other purposes |
| 384 | (e.g. bypassing a caching decorator such as :func:`lru_cache`), this function |
Nick Coghlan | 24c05bc | 2013-07-15 21:13:08 +1000 | [diff] [blame] | 385 | automatically adds a ``__wrapped__`` attribute to the wrapper that refers to |
| 386 | the function being wrapped. |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 387 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 388 | The main intended use for this function is in :term:`decorator` functions which |
| 389 | wrap the decorated function and return the wrapper. If the wrapper function is |
| 390 | not updated, the metadata of the returned function will reflect the wrapper |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 391 | definition rather than the original function definition, which is typically less |
| 392 | than helpful. |
| 393 | |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 394 | :func:`update_wrapper` may be used with callables other than functions. Any |
| 395 | attributes named in *assigned* or *updated* that are missing from the object |
| 396 | being wrapped are ignored (i.e. this function will not attempt to set them |
| 397 | on the wrapper function). :exc:`AttributeError` is still raised if the |
| 398 | wrapper function itself is missing any attributes named in *updated*. |
| 399 | |
| 400 | .. versionadded:: 3.2 |
Georg Brandl | 9e25701 | 2010-08-17 14:11:59 +0000 | [diff] [blame] | 401 | Automatic addition of the ``__wrapped__`` attribute. |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 402 | |
| 403 | .. versionadded:: 3.2 |
Georg Brandl | 9e25701 | 2010-08-17 14:11:59 +0000 | [diff] [blame] | 404 | Copying of the ``__annotations__`` attribute by default. |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 405 | |
| 406 | .. versionchanged:: 3.2 |
Georg Brandl | 9e25701 | 2010-08-17 14:11:59 +0000 | [diff] [blame] | 407 | Missing attributes no longer trigger an :exc:`AttributeError`. |
| 408 | |
Nick Coghlan | 24c05bc | 2013-07-15 21:13:08 +1000 | [diff] [blame] | 409 | .. versionchanged:: 3.4 |
| 410 | The ``__wrapped__`` attribute now always refers to the wrapped |
| 411 | function, even if that function defined a ``__wrapped__`` attribute. |
| 412 | (see :issue:`17482`) |
| 413 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 414 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 415 | .. decorator:: wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 416 | |
Ezio Melotti | 67f6d5f | 2014-08-05 08:14:28 +0300 | [diff] [blame] | 417 | This is a convenience function for invoking :func:`update_wrapper` as a |
| 418 | function decorator when defining a wrapper function. It is equivalent to |
| 419 | ``partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)``. |
| 420 | For example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 422 | >>> from functools import wraps |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | >>> def my_decorator(f): |
| 424 | ... @wraps(f) |
| 425 | ... def wrapper(*args, **kwds): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 426 | ... print('Calling decorated function') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | ... return f(*args, **kwds) |
| 428 | ... return wrapper |
| 429 | ... |
| 430 | >>> @my_decorator |
| 431 | ... def example(): |
| 432 | ... """Docstring""" |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 433 | ... print('Called example function') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 | ... |
| 435 | >>> example() |
| 436 | Calling decorated function |
| 437 | Called example function |
| 438 | >>> example.__name__ |
| 439 | 'example' |
| 440 | >>> example.__doc__ |
| 441 | 'Docstring' |
| 442 | |
| 443 | Without the use of this decorator factory, the name of the example function |
| 444 | would have been ``'wrapper'``, and the docstring of the original :func:`example` |
| 445 | would have been lost. |
| 446 | |
| 447 | |
| 448 | .. _partial-objects: |
| 449 | |
| 450 | :class:`partial` Objects |
| 451 | ------------------------ |
| 452 | |
| 453 | :class:`partial` objects are callable objects created by :func:`partial`. They |
| 454 | have three read-only attributes: |
| 455 | |
| 456 | |
| 457 | .. attribute:: partial.func |
| 458 | |
| 459 | A callable object or function. Calls to the :class:`partial` object will be |
| 460 | forwarded to :attr:`func` with new arguments and keywords. |
| 461 | |
| 462 | |
| 463 | .. attribute:: partial.args |
| 464 | |
| 465 | The leftmost positional arguments that will be prepended to the positional |
| 466 | arguments provided to a :class:`partial` object call. |
| 467 | |
| 468 | |
| 469 | .. attribute:: partial.keywords |
| 470 | |
| 471 | The keyword arguments that will be supplied when the :class:`partial` object is |
| 472 | called. |
| 473 | |
| 474 | :class:`partial` objects are like :class:`function` objects in that they are |
| 475 | callable, weak referencable, and can have attributes. There are some important |
| 476 | differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes |
| 477 | are not created automatically. Also, :class:`partial` objects defined in |
| 478 | classes behave like static methods and do not transform into bound methods |
| 479 | during instance attribute look-up. |