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