rest25/library/functools.rst => rest262/library/functools.rst
n1- 
2-:mod:`functools` --- Higher order functions and operations on callable objects.
1+:mod:`functools` --- Higher order functions and operations on callable objects
3-===============================================================================
2+==============================================================================
4
5.. module:: functools
n5+   :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>
n10- 
11- 
12-.. % standard library, in Python
13- 
14
15
16.. versionadded:: 2.5
17
18The :mod:`functools` module is for higher-order functions: functions that act on
19or return other functions. In general, any callable object can be treated as a
20function for the purposes of this module.
21
n22-The :mod:`functools` module defines the following function:
n18+The :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
23
24
25.. function:: partial(func[,*args][, **keywords])
26
27   Return a new :class:`partial` object which when called will behave like *func*
28   called with the positional arguments *args* and keyword arguments *keywords*. If
29   more arguments are supplied to the call, they are appended to *args*. If
30   additional keyword arguments are supplied, they extend and override *keywords*.
39          newfunc.args = args
40          newfunc.keywords = keywords
41          return newfunc
42
43   The :func:`partial` is used for partial function application which "freezes"
44   some portion of a function's arguments and/or keywords resulting in a new object
45   with a simplified signature.  For example, :func:`partial` can be used to create
46   a callable that behaves like the :func:`int` function where the *base* argument
n47-   defaults to two::
n51+   defaults to two:
48
n53+      >>> from functools import partial
49      >>> basetwo = partial(int, base=2)
50      >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
51      >>> basetwo('10010')
52      18
53
54
55.. function:: update_wrapper(wrapper, wrapped[, assigned][, updated])
56
n57-   Update a wrapper function to look like the wrapped function. The optional
n62+   Update a *wrapper* function to look like the *wrapped* function. The optional
58   arguments are tuples to specify which attributes of the original function are
59   assigned directly to the matching attributes on the wrapper function and which
60   attributes of the wrapper function are updated with the corresponding attributes
61   from the original function. The default values for these arguments are the
62   module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper
n63-   function's name, module and documentation string) and *WRAPPER_UPDATES* (which
n68+   function's *__name__*, *__module__* and *__doc__*, the documentation string) and
64-   updates the wrapper function's instance dictionary).
69+   *WRAPPER_UPDATES* (which updates the wrapper function's *__dict__*, i.e. the
70+   instance dictionary).
65
n66-   The main intended use for this function is in decorator functions which wrap the
n72+   The main intended use for this function is in :term:`decorator` functions which
67-   decorated function and return the wrapper. If the wrapper function is not
73+   wrap the decorated function and return the wrapper. If the wrapper function is
68-   updated, the metadata of the returned function will reflect the wrapper
74+   not updated, the metadata of the returned function will reflect the wrapper
69   definition rather than the original function definition, which is typically less
70   than helpful.
71
72
73.. function:: wraps(wrapped[, assigned][, updated])
74
75   This is a convenience function for invoking ``partial(update_wrapper,
76   wrapped=wrapped, assigned=assigned, updated=updated)`` as a function decorator
n77-   when defining a wrapper function. For example::
n83+   when defining a wrapper function. For example:
78
n85+      >>> from functools import wraps
79      >>> def my_decorator(f):
80      ...     @wraps(f)
81      ...     def wrapper(*args, **kwds):
82      ...         print 'Calling decorated function'
83      ...         return f(*args, **kwds)
84      ...     return wrapper
85      ...
86      >>> @my_decorator
87      ... def example():
n95+      ...     """Docstring"""
88      ...     print 'Called example function'
89      ...
90      >>> example()
91      Calling decorated function
92      Called example function
93      >>> example.__name__
94      'example'
n103+      >>> example.__doc__
104+      'Docstring'
95
96   Without the use of this decorator factory, the name of the example function
n97-   would have been ``'wrapper'``.
n107+   would have been ``'wrapper'``, and the docstring of the original :func:`example`
108+   would have been lost.
98
99
100.. _partial-objects:
101
102:class:`partial` Objects
103------------------------
104
105:class:`partial` objects are callable objects created by :func:`partial`. They
106have three read-only attributes:
107
108
n109-.. attribute:: callable.func
n120+.. attribute:: partial.func
110
111   A callable object or function.  Calls to the :class:`partial` object will be
112   forwarded to :attr:`func` with new arguments and keywords.
113
114
n115-.. attribute:: tuple.args
n126+.. attribute:: partial.args
116
117   The leftmost positional arguments that will be prepended to the positional
118   arguments provided to a :class:`partial` object call.
119
120
t121-.. attribute:: dict.keywords
t132+.. attribute:: partial.keywords
122
123   The keyword arguments that will be supplied when the :class:`partial` object is
124   called.
125
126:class:`partial` objects are like :class:`function` objects in that they are
127callable, weak referencable, and can have attributes.  There are some important
128differences.  For instance, the :attr:`__name__` and :attr:`__doc__` attributes
129are not created automatically.  Also, :class:`partial` objects defined in
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op