/Doc/library/functools.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 143 lines · 104 code · 39 blank · 0 comment · 0 complexity · d309f9acb16591fd8ebd2eaf92ece31b MD5 · raw file

  1. :mod:`functools` --- Higher order functions and operations on callable objects
  2. ==============================================================================
  3. .. module:: functools
  4. :synopsis: Higher order functions and operations on callable objects.
  5. .. moduleauthor:: Peter Harris <scav@blueyonder.co.uk>
  6. .. moduleauthor:: Raymond Hettinger <python@rcn.com>
  7. .. moduleauthor:: Nick Coghlan <ncoghlan@gmail.com>
  8. .. sectionauthor:: Peter Harris <scav@blueyonder.co.uk>
  9. .. versionadded:: 2.5
  10. The :mod:`functools` module is for higher-order functions: functions that act on
  11. or return other functions. In general, any callable object can be treated as a
  12. function for the purposes of this module.
  13. The :mod:`functools` module defines the following functions:
  14. .. function:: reduce(function, iterable[, initializer])
  15. This is the same function as :func:`reduce`. It is made available in this module
  16. to allow writing code more forward-compatible with Python 3.
  17. .. versionadded:: 2.6
  18. .. function:: partial(func[,*args][, **keywords])
  19. Return a new :class:`partial` object which when called will behave like *func*
  20. called with the positional arguments *args* and keyword arguments *keywords*. If
  21. more arguments are supplied to the call, they are appended to *args*. If
  22. additional keyword arguments are supplied, they extend and override *keywords*.
  23. Roughly equivalent to::
  24. def partial(func, *args, **keywords):
  25. def newfunc(*fargs, **fkeywords):
  26. newkeywords = keywords.copy()
  27. newkeywords.update(fkeywords)
  28. return func(*(args + fargs), **newkeywords)
  29. newfunc.func = func
  30. newfunc.args = args
  31. newfunc.keywords = keywords
  32. return newfunc
  33. The :func:`partial` is used for partial function application which "freezes"
  34. some portion of a function's arguments and/or keywords resulting in a new object
  35. with a simplified signature. For example, :func:`partial` can be used to create
  36. a callable that behaves like the :func:`int` function where the *base* argument
  37. defaults to two:
  38. >>> from functools import partial
  39. >>> basetwo = partial(int, base=2)
  40. >>> basetwo.__doc__ = 'Convert base 2 string to an int.'
  41. >>> basetwo('10010')
  42. 18
  43. .. function:: update_wrapper(wrapper, wrapped[, assigned][, updated])
  44. Update a *wrapper* function to look like the *wrapped* function. The optional
  45. arguments are tuples to specify which attributes of the original function are
  46. assigned directly to the matching attributes on the wrapper function and which
  47. attributes of the wrapper function are updated with the corresponding attributes
  48. from the original function. The default values for these arguments are the
  49. module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper
  50. function's *__name__*, *__module__* and *__doc__*, the documentation string) and
  51. *WRAPPER_UPDATES* (which updates the wrapper function's *__dict__*, i.e. the
  52. instance dictionary).
  53. The main intended use for this function is in :term:`decorator` functions which
  54. wrap the decorated function and return the wrapper. If the wrapper function is
  55. not updated, the metadata of the returned function will reflect the wrapper
  56. definition rather than the original function definition, which is typically less
  57. than helpful.
  58. .. function:: wraps(wrapped[, assigned][, updated])
  59. This is a convenience function for invoking ``partial(update_wrapper,
  60. wrapped=wrapped, assigned=assigned, updated=updated)`` as a function decorator
  61. when defining a wrapper function. For example:
  62. >>> from functools import wraps
  63. >>> def my_decorator(f):
  64. ... @wraps(f)
  65. ... def wrapper(*args, **kwds):
  66. ... print 'Calling decorated function'
  67. ... return f(*args, **kwds)
  68. ... return wrapper
  69. ...
  70. >>> @my_decorator
  71. ... def example():
  72. ... """Docstring"""
  73. ... print 'Called example function'
  74. ...
  75. >>> example()
  76. Calling decorated function
  77. Called example function
  78. >>> example.__name__
  79. 'example'
  80. >>> example.__doc__
  81. 'Docstring'
  82. Without the use of this decorator factory, the name of the example function
  83. would have been ``'wrapper'``, and the docstring of the original :func:`example`
  84. would have been lost.
  85. .. _partial-objects:
  86. :class:`partial` Objects
  87. ------------------------
  88. :class:`partial` objects are callable objects created by :func:`partial`. They
  89. have three read-only attributes:
  90. .. attribute:: partial.func
  91. A callable object or function. Calls to the :class:`partial` object will be
  92. forwarded to :attr:`func` with new arguments and keywords.
  93. .. attribute:: partial.args
  94. The leftmost positional arguments that will be prepended to the positional
  95. arguments provided to a :class:`partial` object call.
  96. .. attribute:: partial.keywords
  97. The keyword arguments that will be supplied when the :class:`partial` object is
  98. called.
  99. :class:`partial` objects are like :class:`function` objects in that they are
  100. callable, weak referencable, and can have attributes. There are some important
  101. differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes
  102. are not created automatically. Also, :class:`partial` objects defined in
  103. classes behave like static methods and do not transform into bound methods
  104. during instance attribute look-up.