blob: 7fd7d5826b66f213dfb5c2dcf0b37c309d46c47e [file] [log] [blame]
Georg Brandl6c89a792012-01-25 22:36:25 +01001:mod:`functools` --- Higher-order functions and operations on callable objects
Georg Brandl116aa622007-08-15 14:28:22 +00002==============================================================================
3
4.. module:: functools
Georg Brandl6c89a792012-01-25 22:36:25 +01005 :synopsis: Higher-order functions and operations on callable objects.
Georg Brandl116aa622007-08-15 14:28:22 +00006.. moduleauthor:: Peter Harris <scav@blueyonder.co.uk>
7.. moduleauthor:: Raymond Hettinger <python@rcn.com>
8.. moduleauthor:: Nick Coghlan <ncoghlan@gmail.com>
Łukasz Langa6f692512013-06-05 12:20:24 +02009.. moduleauthor:: Łukasz Langa <lukasz@langa.pl>
Georg Brandl116aa622007-08-15 14:28:22 +000010.. sectionauthor:: Peter Harris <scav@blueyonder.co.uk>
11
Raymond Hettinger05ce0792011-01-10 21:16:07 +000012**Source code:** :source:`Lib/functools.py`
13
14--------------
Georg Brandl116aa622007-08-15 14:28:22 +000015
Georg Brandl116aa622007-08-15 14:28:22 +000016The :mod:`functools` module is for higher-order functions: functions that act on
17or return other functions. In general, any callable object can be treated as a
18function for the purposes of this module.
19
Thomas Woutersed03b412007-08-28 21:37:11 +000020The :mod:`functools` module defines the following functions:
21
Éric Araujob10089e2010-11-18 14:22:08 +000022.. function:: cmp_to_key(func)
Raymond Hettingerc50846a2010-04-05 18:56:31 +000023
Georg Brandl3b65fd72012-01-23 20:19:33 +010024 Transform an old-style comparison function to a key function. Used with
Benjamin Petersoncca65312010-08-09 02:13:10 +000025 tools that accept key functions (such as :func:`sorted`, :func:`min`,
26 :func:`max`, :func:`heapq.nlargest`, :func:`heapq.nsmallest`,
27 :func:`itertools.groupby`). This function is primarily used as a transition
Ezio Melotti9ecb6be2012-01-16 08:28:54 +020028 tool for programs being converted from Python 2 which supported the use of
Benjamin Petersoncca65312010-08-09 02:13:10 +000029 comparison functions.
Raymond Hettingerc50846a2010-04-05 18:56:31 +000030
Georg Brandl6c89a792012-01-25 22:36:25 +010031 A comparison function is any callable that accept two arguments, compares them,
Benjamin Petersoncca65312010-08-09 02:13:10 +000032 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 Hettingerc79fb0e2010-12-01 03:45:41 +000034 argument and returns another value indicating the position in the desired
Benjamin Petersoncca65312010-08-09 02:13:10 +000035 collation sequence.
Raymond Hettingerc50846a2010-04-05 18:56:31 +000036
Benjamin Petersoncca65312010-08-09 02:13:10 +000037 Example::
Raymond Hettingerc50846a2010-04-05 18:56:31 +000038
Benjamin Petersoncca65312010-08-09 02:13:10 +000039 sorted(iterable, key=cmp_to_key(locale.strcoll)) # locale-aware sort order
Raymond Hettingerc50846a2010-04-05 18:56:31 +000040
41 .. versionadded:: 3.2
42
Georg Brandl67b21b72010-08-17 15:07:14 +000043
Raymond Hettinger010ce322012-05-19 21:20:48 -070044.. decorator:: lru_cache(maxsize=128, typed=False)
Georg Brandl2e7346a2010-07-31 18:09:23 +000045
46 Decorator to wrap a function with a memoizing callable that saves up to the
47 *maxsize* most recent calls. It can save time when an expensive or I/O bound
48 function is periodically called with the same arguments.
49
Raymond Hettinger7496b412010-11-30 19:15:45 +000050 Since a dictionary is used to cache results, the positional and keyword
51 arguments to the function must be hashable.
Georg Brandl2e7346a2010-07-31 18:09:23 +000052
Raymond Hettinger7d74eff2012-06-04 00:32:15 -070053 If *maxsize* is set to None, the LRU feature is disabled and the cache can
54 grow without bound. The LRU feature performs best when *maxsize* is a
55 power-of-two.
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +000056
Raymond Hettingercd9fdfd2011-10-20 08:57:45 -070057 If *typed* is set to True, function arguments of different types will be
58 cached separately. For example, ``f(3)`` and ``f(3.0)`` will be treated
59 as distinct calls with distinct results.
60
Raymond Hettinger7496b412010-11-30 19:15:45 +000061 To help measure the effectiveness of the cache and tune the *maxsize*
62 parameter, the wrapped function is instrumented with a :func:`cache_info`
63 function that returns a :term:`named tuple` showing *hits*, *misses*,
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +000064 *maxsize* and *currsize*. In a multi-threaded environment, the hits
65 and misses are approximate.
Nick Coghlan234515a2010-11-30 06:19:46 +000066
Raymond Hettinger7496b412010-11-30 19:15:45 +000067 The decorator also provides a :func:`cache_clear` function for clearing or
68 invalidating the cache.
Georg Brandl2e7346a2010-07-31 18:09:23 +000069
Raymond Hettinger3fccfcb2010-08-17 19:19:29 +000070 The original underlying function is accessible through the
Raymond Hettinger7496b412010-11-30 19:15:45 +000071 :attr:`__wrapped__` attribute. This is useful for introspection, for
72 bypassing the cache, or for rewrapping the function with a different cache.
Nick Coghlan98876832010-08-17 06:17:18 +000073
Raymond Hettingercc038582010-11-30 20:02:57 +000074 An `LRU (least recently used) cache
Georg Brandl525d3552014-10-29 10:26:56 +010075 <http://en.wikipedia.org/wiki/Cache_algorithms#Examples>`_ works
Raymond Hettingercd9fdfd2011-10-20 08:57:45 -070076 best when the most recent calls are the best predictors of upcoming calls (for
77 example, the most popular articles on a news server tend to change each day).
Raymond Hettinger7496b412010-11-30 19:15:45 +000078 The cache's size limit assures that the cache does not grow without bound on
79 long-running processes such as web servers.
80
Raymond Hettingercc038582010-11-30 20:02:57 +000081 Example of an LRU cache for static web content::
Raymond Hettinger7496b412010-11-30 19:15:45 +000082
Raymond Hettinger17328e42013-04-06 20:27:33 -070083 @lru_cache(maxsize=32)
Raymond Hettinger7496b412010-11-30 19:15:45 +000084 def get_pep(num):
85 'Retrieve text of a Python Enhancement Proposal'
86 resource = 'http://www.python.org/dev/peps/pep-%04d/' % num
87 try:
88 with urllib.request.urlopen(resource) as s:
89 return s.read()
90 except urllib.error.HTTPError:
91 return 'Not Found'
92
93 >>> for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:
94 ... pep = get_pep(n)
95 ... print(n, len(pep))
96
Raymond Hettinger17328e42013-04-06 20:27:33 -070097 >>> get_pep.cache_info()
98 CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)
Georg Brandl2e7346a2010-07-31 18:09:23 +000099
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000100 Example of efficiently computing
101 `Fibonacci numbers <http://en.wikipedia.org/wiki/Fibonacci_number>`_
102 using a cache to implement a
103 `dynamic programming <http://en.wikipedia.org/wiki/Dynamic_programming>`_
104 technique::
105
106 @lru_cache(maxsize=None)
107 def fib(n):
108 if n < 2:
109 return n
110 return fib(n-1) + fib(n-2)
111
Raymond Hettinger17328e42013-04-06 20:27:33 -0700112 >>> [fib(n) for n in range(16)]
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000113 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
114
Raymond Hettinger17328e42013-04-06 20:27:33 -0700115 >>> fib.cache_info()
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000116 CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
117
Georg Brandl2e7346a2010-07-31 18:09:23 +0000118 .. versionadded:: 3.2
119
Raymond Hettingercd9fdfd2011-10-20 08:57:45 -0700120 .. versionchanged:: 3.3
121 Added the *typed* option.
122
Georg Brandl8a1caa22010-07-29 16:01:11 +0000123.. decorator:: total_ordering
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000124
125 Given a class defining one or more rich comparison ordering methods, this
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000126 class decorator supplies the rest. This simplifies the effort involved
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000127 in specifying all of the possible rich comparison operations:
128
129 The class must define one of :meth:`__lt__`, :meth:`__le__`,
130 :meth:`__gt__`, or :meth:`__ge__`.
131 In addition, the class should supply an :meth:`__eq__` method.
132
133 For example::
134
135 @total_ordering
136 class Student:
Nick Coghlanf05d9812013-10-02 00:02:03 +1000137 def _is_valid_operand(self, other):
138 return (hasattr(other, "lastname") and
139 hasattr(other, "firstname"))
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000140 def __eq__(self, other):
Nick Coghlanf05d9812013-10-02 00:02:03 +1000141 if not self._is_valid_operand(other):
142 return NotImplemented
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000143 return ((self.lastname.lower(), self.firstname.lower()) ==
144 (other.lastname.lower(), other.firstname.lower()))
145 def __lt__(self, other):
Nick Coghlanf05d9812013-10-02 00:02:03 +1000146 if not self._is_valid_operand(other):
147 return NotImplemented
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000148 return ((self.lastname.lower(), self.firstname.lower()) <
149 (other.lastname.lower(), other.firstname.lower()))
150
Nick Coghlanf05d9812013-10-02 00:02:03 +1000151 .. note::
152
153 While this decorator makes it easy to create well behaved totally
154 ordered types, it *does* come at the cost of slower execution and
155 more complex stack traces for the derived comparison methods. If
156 performance benchmarking indicates this is a bottleneck for a given
157 application, implementing all six rich comparison methods instead is
158 likely to provide an easy speed boost.
159
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000160 .. versionadded:: 3.2
161
Nick Coghlanf05d9812013-10-02 00:02:03 +1000162 .. versionchanged:: 3.4
163 Returning NotImplemented from the underlying comparison function for
164 unrecognised types is now supported.
Georg Brandl67b21b72010-08-17 15:07:14 +0000165
Georg Brandl036490d2009-05-17 13:00:36 +0000166.. function:: partial(func, *args, **keywords)
Georg Brandl116aa622007-08-15 14:28:22 +0000167
168 Return a new :class:`partial` object which when called will behave like *func*
169 called with the positional arguments *args* and keyword arguments *keywords*. If
170 more arguments are supplied to the call, they are appended to *args*. If
171 additional keyword arguments are supplied, they extend and override *keywords*.
172 Roughly equivalent to::
173
174 def partial(func, *args, **keywords):
175 def newfunc(*fargs, **fkeywords):
176 newkeywords = keywords.copy()
177 newkeywords.update(fkeywords)
178 return func(*(args + fargs), **newkeywords)
179 newfunc.func = func
180 newfunc.args = args
181 newfunc.keywords = keywords
182 return newfunc
183
184 The :func:`partial` is used for partial function application which "freezes"
185 some portion of a function's arguments and/or keywords resulting in a new object
186 with a simplified signature. For example, :func:`partial` can be used to create
187 a callable that behaves like the :func:`int` function where the *base* argument
Christian Heimesfe337bf2008-03-23 21:54:12 +0000188 defaults to two:
Georg Brandl116aa622007-08-15 14:28:22 +0000189
Christian Heimesfe337bf2008-03-23 21:54:12 +0000190 >>> from functools import partial
Georg Brandl116aa622007-08-15 14:28:22 +0000191 >>> basetwo = partial(int, base=2)
192 >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
193 >>> basetwo('10010')
194 18
195
196
Nick Coghlanf4cb48a2013-11-03 16:41:46 +1000197.. class:: partialmethod(func, *args, **keywords)
198
199 Return a new :class:`partialmethod` descriptor which behaves
200 like :class:`partial` except that it is designed to be used as a method
201 definition rather than being directly callable.
202
203 *func* must be a :term:`descriptor` or a callable (objects which are both,
204 like normal functions, are handled as descriptors).
205
206 When *func* is a descriptor (such as a normal Python function,
207 :func:`classmethod`, :func:`staticmethod`, :func:`abstractmethod` or
208 another instance of :class:`partialmethod`), calls to ``__get__`` are
209 delegated to the underlying descriptor, and an appropriate
210 :class:`partial` object returned as the result.
211
212 When *func* is a non-descriptor callable, an appropriate bound method is
213 created dynamically. This behaves like a normal Python function when
214 used as a method: the *self* argument will be inserted as the first
215 positional argument, even before the *args* and *keywords* supplied to
216 the :class:`partialmethod` constructor.
217
218 Example::
219
220 >>> class Cell(object):
Benjamin Peterson3a434032014-03-30 15:07:09 -0400221 ... def __init__(self):
222 ... self._alive = False
Nick Coghlanf4cb48a2013-11-03 16:41:46 +1000223 ... @property
224 ... def alive(self):
225 ... return self._alive
226 ... def set_state(self, state):
227 ... self._alive = bool(state)
Nick Coghlan3daaf5f2013-11-04 23:32:16 +1000228 ... set_alive = partialmethod(set_state, True)
229 ... set_dead = partialmethod(set_state, False)
Nick Coghlanf4cb48a2013-11-03 16:41:46 +1000230 ...
231 >>> c = Cell()
232 >>> c.alive
233 False
234 >>> c.set_alive()
235 >>> c.alive
236 True
237
238 .. versionadded:: 3.4
239
240
Georg Brandl58f9e4f2008-04-19 22:18:33 +0000241.. function:: reduce(function, iterable[, initializer])
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243 Apply *function* of two arguments cumulatively to the items of *sequence*, from
244 left to right, so as to reduce the sequence to a single value. For example,
245 ``reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])`` calculates ``((((1+2)+3)+4)+5)``.
246 The left argument, *x*, is the accumulated value and the right argument, *y*, is
247 the update value from the *sequence*. If the optional *initializer* is present,
248 it is placed before the items of the sequence in the calculation, and serves as
249 a default when the sequence is empty. If *initializer* is not given and
250 *sequence* contains only one item, the first item is returned.
251
Raymond Hettinger64801682013-10-12 16:04:17 -0700252 Equivalent to::
253
254 def reduce(function, iterable, initializer=None):
255 it = iter(iterable)
256 if initializer is None:
257 value = next(it)
258 else:
259 value = initializer
260 for element in it:
261 value = function(value, element)
262 return value
263
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Łukasz Langa6f692512013-06-05 12:20:24 +0200265.. decorator:: singledispatch(default)
266
Łukasz Langafdcf2b72013-06-07 22:54:03 +0200267 Transforms a function into a :term:`single-dispatch <single
268 dispatch>` :term:`generic function`.
Łukasz Langa6f692512013-06-05 12:20:24 +0200269
270 To define a generic function, decorate it with the ``@singledispatch``
271 decorator. Note that the dispatch happens on the type of the first argument,
272 create your function accordingly::
273
274 >>> from functools import singledispatch
275 >>> @singledispatch
276 ... def fun(arg, verbose=False):
277 ... if verbose:
278 ... print("Let me just say,", end=" ")
279 ... print(arg)
280
281 To add overloaded implementations to the function, use the :func:`register`
282 attribute of the generic function. It is a decorator, taking a type
283 parameter and decorating a function implementing the operation for that
284 type::
285
286 >>> @fun.register(int)
287 ... def _(arg, verbose=False):
288 ... if verbose:
289 ... print("Strength in numbers, eh?", end=" ")
290 ... print(arg)
291 ...
292 >>> @fun.register(list)
293 ... def _(arg, verbose=False):
294 ... if verbose:
295 ... print("Enumerate this:")
296 ... for i, elem in enumerate(arg):
297 ... print(i, elem)
298
299 To enable registering lambdas and pre-existing functions, the
300 :func:`register` attribute can be used in a functional form::
301
302 >>> def nothing(arg, verbose=False):
303 ... print("Nothing.")
304 ...
305 >>> fun.register(type(None), nothing)
306
307 The :func:`register` attribute returns the undecorated function which
308 enables decorator stacking, pickling, as well as creating unit tests for
309 each variant independently::
310
311 >>> @fun.register(float)
312 ... @fun.register(Decimal)
313 ... def fun_num(arg, verbose=False):
314 ... if verbose:
315 ... print("Half of your number:", end=" ")
316 ... print(arg / 2)
317 ...
318 >>> fun_num is fun
319 False
320
321 When called, the generic function dispatches on the type of the first
322 argument::
323
324 >>> fun("Hello, world.")
325 Hello, world.
326 >>> fun("test.", verbose=True)
327 Let me just say, test.
328 >>> fun(42, verbose=True)
329 Strength in numbers, eh? 42
330 >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
331 Enumerate this:
332 0 spam
333 1 spam
334 2 eggs
335 3 spam
336 >>> fun(None)
337 Nothing.
338 >>> fun(1.23)
339 0.615
340
341 Where there is no registered implementation for a specific type, its
342 method resolution order is used to find a more generic implementation.
343 The original function decorated with ``@singledispatch`` is registered
344 for the base ``object`` type, which means it is used if no better
345 implementation is found.
346
347 To check which implementation will the generic function choose for
348 a given type, use the ``dispatch()`` attribute::
349
350 >>> fun.dispatch(float)
351 <function fun_num at 0x1035a2840>
352 >>> fun.dispatch(dict) # note: default implementation
353 <function fun at 0x103fe0000>
354
355 To access all registered implementations, use the read-only ``registry``
356 attribute::
357
358 >>> fun.registry.keys()
359 dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>,
360 <class 'decimal.Decimal'>, <class 'list'>,
361 <class 'float'>])
362 >>> fun.registry[float]
363 <function fun_num at 0x1035a2840>
364 >>> fun.registry[object]
365 <function fun at 0x103fe0000>
366
367 .. versionadded:: 3.4
368
369
Georg Brandl036490d2009-05-17 13:00:36 +0000370.. function:: update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
372 Update a *wrapper* function to look like the *wrapped* function. The optional
373 arguments are tuples to specify which attributes of the original function are
374 assigned directly to the matching attributes on the wrapper function and which
375 attributes of the wrapper function are updated with the corresponding attributes
376 from the original function. The default values for these arguments are the
377 module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper
Antoine Pitrou560f7642010-08-04 18:28:02 +0000378 function's *__name__*, *__module__*, *__annotations__* and *__doc__*, the
379 documentation string) and *WRAPPER_UPDATES* (which updates the wrapper
380 function's *__dict__*, i.e. the instance dictionary).
Georg Brandl116aa622007-08-15 14:28:22 +0000381
Nick Coghlan98876832010-08-17 06:17:18 +0000382 To allow access to the original function for introspection and other purposes
383 (e.g. bypassing a caching decorator such as :func:`lru_cache`), this function
Nick Coghlan24c05bc2013-07-15 21:13:08 +1000384 automatically adds a ``__wrapped__`` attribute to the wrapper that refers to
385 the function being wrapped.
Nick Coghlan98876832010-08-17 06:17:18 +0000386
Christian Heimesd8654cf2007-12-02 15:22:16 +0000387 The main intended use for this function is in :term:`decorator` functions which
388 wrap the decorated function and return the wrapper. If the wrapper function is
389 not updated, the metadata of the returned function will reflect the wrapper
Georg Brandl116aa622007-08-15 14:28:22 +0000390 definition rather than the original function definition, which is typically less
391 than helpful.
392
Nick Coghlan98876832010-08-17 06:17:18 +0000393 :func:`update_wrapper` may be used with callables other than functions. Any
394 attributes named in *assigned* or *updated* that are missing from the object
395 being wrapped are ignored (i.e. this function will not attempt to set them
396 on the wrapper function). :exc:`AttributeError` is still raised if the
397 wrapper function itself is missing any attributes named in *updated*.
398
399 .. versionadded:: 3.2
Georg Brandl9e257012010-08-17 14:11:59 +0000400 Automatic addition of the ``__wrapped__`` attribute.
Nick Coghlan98876832010-08-17 06:17:18 +0000401
402 .. versionadded:: 3.2
Georg Brandl9e257012010-08-17 14:11:59 +0000403 Copying of the ``__annotations__`` attribute by default.
Nick Coghlan98876832010-08-17 06:17:18 +0000404
405 .. versionchanged:: 3.2
Georg Brandl9e257012010-08-17 14:11:59 +0000406 Missing attributes no longer trigger an :exc:`AttributeError`.
407
Nick Coghlan24c05bc2013-07-15 21:13:08 +1000408 .. versionchanged:: 3.4
409 The ``__wrapped__`` attribute now always refers to the wrapped
410 function, even if that function defined a ``__wrapped__`` attribute.
411 (see :issue:`17482`)
412
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Georg Brandl8a1caa22010-07-29 16:01:11 +0000414.. decorator:: wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Ezio Melotti67f6d5f2014-08-05 08:14:28 +0300416 This is a convenience function for invoking :func:`update_wrapper` as a
417 function decorator when defining a wrapper function. It is equivalent to
418 ``partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)``.
419 For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Christian Heimesfe337bf2008-03-23 21:54:12 +0000421 >>> from functools import wraps
Georg Brandl116aa622007-08-15 14:28:22 +0000422 >>> def my_decorator(f):
423 ... @wraps(f)
424 ... def wrapper(*args, **kwds):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000425 ... print('Calling decorated function')
Georg Brandl116aa622007-08-15 14:28:22 +0000426 ... return f(*args, **kwds)
427 ... return wrapper
428 ...
429 >>> @my_decorator
430 ... def example():
431 ... """Docstring"""
Georg Brandl6911e3c2007-09-04 07:15:32 +0000432 ... print('Called example function')
Georg Brandl116aa622007-08-15 14:28:22 +0000433 ...
434 >>> example()
435 Calling decorated function
436 Called example function
437 >>> example.__name__
438 'example'
439 >>> example.__doc__
440 'Docstring'
441
442 Without the use of this decorator factory, the name of the example function
443 would have been ``'wrapper'``, and the docstring of the original :func:`example`
444 would have been lost.
445
446
447.. _partial-objects:
448
449:class:`partial` Objects
450------------------------
451
452:class:`partial` objects are callable objects created by :func:`partial`. They
453have three read-only attributes:
454
455
456.. attribute:: partial.func
457
458 A callable object or function. Calls to the :class:`partial` object will be
459 forwarded to :attr:`func` with new arguments and keywords.
460
461
462.. attribute:: partial.args
463
464 The leftmost positional arguments that will be prepended to the positional
465 arguments provided to a :class:`partial` object call.
466
467
468.. attribute:: partial.keywords
469
470 The keyword arguments that will be supplied when the :class:`partial` object is
471 called.
472
473:class:`partial` objects are like :class:`function` objects in that they are
474callable, weak referencable, and can have attributes. There are some important
475differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes
476are not created automatically. Also, :class:`partial` objects defined in
477classes behave like static methods and do not transform into bound methods
478during instance attribute look-up.