blob: 6ae175a8ec83edc3d5d22f6384af3a6b3540d889 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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
Georg Brandl116aa622007-08-15 14:28:22 +000012The :mod:`functools` module is for higher-order functions: functions that act on
13or return other functions. In general, any callable object can be treated as a
14function for the purposes of this module.
15
Thomas Woutersed03b412007-08-28 21:37:11 +000016The :mod:`functools` module defines the following functions:
17
Raymond Hettingerc50846a2010-04-05 18:56:31 +000018.. function:: cmp_to_key(func)
19
20 Transform an old-style comparison function to a key-function. Used with
21 tools that accept key functions (such as :func:`sorted`, :func:`min`,
22 :func:`max`, :func:`heapq.nlargest`, :func:`heapq.nsmallest`,
23 :func:`itertools.groupby`).
24 This function is primarily used as a transition tool for programs
25 being converted from Py2.x which supported the use of comparison
26 functions.
27
28 A compare function is any callable that accept two arguments, compares
29 them, and returns a negative number for less-than, zero for equality,
30 or a positive number for greater-than. A key function is a callable
31 that accepts one argument and returns another value that indicates
32 the position in the desired collation sequence.
33
34 Example::
35
36 sorted(iterable, key=cmp_to_key(locale.strcoll)) # locale-aware sort order
37
38 .. versionadded:: 3.2
39
40.. function:: total_ordering(cls)
41
42 Given a class defining one or more rich comparison ordering methods, this
Benjamin Peterson08bf91c2010-04-11 16:12:57 +000043 class decorator supplies the rest. This simplifies the effort involved
Raymond Hettingerc50846a2010-04-05 18:56:31 +000044 in specifying all of the possible rich comparison operations:
45
46 The class must define one of :meth:`__lt__`, :meth:`__le__`,
47 :meth:`__gt__`, or :meth:`__ge__`.
48 In addition, the class should supply an :meth:`__eq__` method.
49
50 For example::
51
52 @total_ordering
53 class Student:
54 def __eq__(self, other):
55 return ((self.lastname.lower(), self.firstname.lower()) ==
56 (other.lastname.lower(), other.firstname.lower()))
57 def __lt__(self, other):
58 return ((self.lastname.lower(), self.firstname.lower()) <
59 (other.lastname.lower(), other.firstname.lower()))
60
61 .. versionadded:: 3.2
62
Georg Brandl036490d2009-05-17 13:00:36 +000063.. function:: partial(func, *args, **keywords)
Georg Brandl116aa622007-08-15 14:28:22 +000064
65 Return a new :class:`partial` object which when called will behave like *func*
66 called with the positional arguments *args* and keyword arguments *keywords*. If
67 more arguments are supplied to the call, they are appended to *args*. If
68 additional keyword arguments are supplied, they extend and override *keywords*.
69 Roughly equivalent to::
70
71 def partial(func, *args, **keywords):
72 def newfunc(*fargs, **fkeywords):
73 newkeywords = keywords.copy()
74 newkeywords.update(fkeywords)
75 return func(*(args + fargs), **newkeywords)
76 newfunc.func = func
77 newfunc.args = args
78 newfunc.keywords = keywords
79 return newfunc
80
81 The :func:`partial` is used for partial function application which "freezes"
82 some portion of a function's arguments and/or keywords resulting in a new object
83 with a simplified signature. For example, :func:`partial` can be used to create
84 a callable that behaves like the :func:`int` function where the *base* argument
Christian Heimesfe337bf2008-03-23 21:54:12 +000085 defaults to two:
Georg Brandl116aa622007-08-15 14:28:22 +000086
Christian Heimesfe337bf2008-03-23 21:54:12 +000087 >>> from functools import partial
Georg Brandl116aa622007-08-15 14:28:22 +000088 >>> basetwo = partial(int, base=2)
89 >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
90 >>> basetwo('10010')
91 18
92
93
Georg Brandl58f9e4f2008-04-19 22:18:33 +000094.. function:: reduce(function, iterable[, initializer])
Georg Brandl116aa622007-08-15 14:28:22 +000095
96 Apply *function* of two arguments cumulatively to the items of *sequence*, from
97 left to right, so as to reduce the sequence to a single value. For example,
98 ``reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])`` calculates ``((((1+2)+3)+4)+5)``.
99 The left argument, *x*, is the accumulated value and the right argument, *y*, is
100 the update value from the *sequence*. If the optional *initializer* is present,
101 it is placed before the items of the sequence in the calculation, and serves as
102 a default when the sequence is empty. If *initializer* is not given and
103 *sequence* contains only one item, the first item is returned.
104
105
Georg Brandl036490d2009-05-17 13:00:36 +0000106.. function:: update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
Georg Brandl116aa622007-08-15 14:28:22 +0000107
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
Christian Heimesd8654cf2007-12-02 15:22:16 +0000118 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 Brandl116aa622007-08-15 14:28:22 +0000121 definition rather than the original function definition, which is typically less
122 than helpful.
123
124
Georg Brandl036490d2009-05-17 13:00:36 +0000125.. function:: wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127 This is a convenience function for invoking ``partial(update_wrapper,
128 wrapped=wrapped, assigned=assigned, updated=updated)`` as a function decorator
Christian Heimesfe337bf2008-03-23 21:54:12 +0000129 when defining a wrapper function. For example:
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Christian Heimesfe337bf2008-03-23 21:54:12 +0000131 >>> from functools import wraps
Georg Brandl116aa622007-08-15 14:28:22 +0000132 >>> def my_decorator(f):
133 ... @wraps(f)
134 ... def wrapper(*args, **kwds):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000135 ... print('Calling decorated function')
Georg Brandl116aa622007-08-15 14:28:22 +0000136 ... return f(*args, **kwds)
137 ... return wrapper
138 ...
139 >>> @my_decorator
140 ... def example():
141 ... """Docstring"""
Georg Brandl6911e3c2007-09-04 07:15:32 +0000142 ... print('Called example function')
Georg Brandl116aa622007-08-15 14:28:22 +0000143 ...
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
163have 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
184callable, weak referencable, and can have attributes. There are some important
185differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes
186are not created automatically. Also, :class:`partial` objects defined in
187classes behave like static methods and do not transform into bound methods
188during instance attribute look-up.
189