blob: 0f948487466e4601b2cb6b765aaa17ae5a8c7f59 [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
20
21.. function:: reduce(function, iterable[, initializer])
22
23 This is the same function as :func:`reduce`. It is made available in this module
24 to allow writing code more forward-compatible with Python 3.
25
26 .. versionadded:: 2.6
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
28
29.. function:: partial(func[,*args][, **keywords])
30
31 Return a new :class:`partial` object which when called will behave like *func*
32 called with the positional arguments *args* and keyword arguments *keywords*. If
33 more arguments are supplied to the call, they are appended to *args*. If
34 additional keyword arguments are supplied, they extend and override *keywords*.
35 Roughly equivalent to::
36
37 def partial(func, *args, **keywords):
38 def newfunc(*fargs, **fkeywords):
39 newkeywords = keywords.copy()
40 newkeywords.update(fkeywords)
41 return func(*(args + fargs), **newkeywords)
42 newfunc.func = func
43 newfunc.args = args
44 newfunc.keywords = keywords
45 return newfunc
46
47 The :func:`partial` is used for partial function application which "freezes"
48 some portion of a function's arguments and/or keywords resulting in a new object
49 with a simplified signature. For example, :func:`partial` can be used to create
50 a callable that behaves like the :func:`int` function where the *base* argument
51 defaults to two::
52
53 >>> basetwo = partial(int, base=2)
54 >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
55 >>> basetwo('10010')
56 18
57
58
59.. function:: update_wrapper(wrapper, wrapped[, assigned][, updated])
60
61 Update a *wrapper* function to look like the *wrapped* function. The optional
62 arguments are tuples to specify which attributes of the original function are
63 assigned directly to the matching attributes on the wrapper function and which
64 attributes of the wrapper function are updated with the corresponding attributes
65 from the original function. The default values for these arguments are the
66 module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper
67 function's *__name__*, *__module__* and *__doc__*, the documentation string) and
68 *WRAPPER_UPDATES* (which updates the wrapper function's *__dict__*, i.e. the
69 instance dictionary).
70
Georg Brandl584265b2007-12-02 14:58:50 +000071 The main intended use for this function is in :term:`decorator` functions which
72 wrap the decorated function and return the wrapper. If the wrapper function is
73 not updated, the metadata of the returned function will reflect the wrapper
Georg Brandl8ec7f652007-08-15 14:28:01 +000074 definition rather than the original function definition, which is typically less
75 than helpful.
76
77
78.. function:: wraps(wrapped[, assigned][, updated])
79
80 This is a convenience function for invoking ``partial(update_wrapper,
81 wrapped=wrapped, assigned=assigned, updated=updated)`` as a function decorator
82 when defining a wrapper function. For example::
83
84 >>> def my_decorator(f):
85 ... @wraps(f)
86 ... def wrapper(*args, **kwds):
87 ... print 'Calling decorated function'
88 ... return f(*args, **kwds)
89 ... return wrapper
90 ...
91 >>> @my_decorator
92 ... def example():
93 ... """Docstring"""
94 ... print 'Called example function'
95 ...
96 >>> example()
97 Calling decorated function
98 Called example function
99 >>> example.__name__
100 'example'
101 >>> example.__doc__
102 'Docstring'
103
104 Without the use of this decorator factory, the name of the example function
105 would have been ``'wrapper'``, and the docstring of the original :func:`example`
106 would have been lost.
107
108
109.. _partial-objects:
110
111:class:`partial` Objects
112------------------------
113
114:class:`partial` objects are callable objects created by :func:`partial`. They
115have three read-only attributes:
116
117
118.. attribute:: partial.func
119
120 A callable object or function. Calls to the :class:`partial` object will be
121 forwarded to :attr:`func` with new arguments and keywords.
122
123
124.. attribute:: partial.args
125
126 The leftmost positional arguments that will be prepended to the positional
127 arguments provided to a :class:`partial` object call.
128
129
130.. attribute:: partial.keywords
131
132 The keyword arguments that will be supplied when the :class:`partial` object is
133 called.
134
135:class:`partial` objects are like :class:`function` objects in that they are
136callable, weak referencable, and can have attributes. There are some important
137differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes
138are not created automatically. Also, :class:`partial` objects defined in
139classes behave like static methods and do not transform into bound methods
140during instance attribute look-up.
141