Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 1 | \section{\module{functional} --- |
| 2 | Higher order functions and operations on callable objects.} |
| 3 | |
| 4 | \declaremodule{standard}{functional} % standard library, in Python |
| 5 | |
| 6 | \moduleauthor{Peter Harris}{scav@blueyonder.co.uk} |
| 7 | \moduleauthor{Raymond Hettinger}{python@rcn.com} |
| 8 | \sectionauthor{Peter Harris}{scav@blueyonder.co.uk} |
| 9 | |
| 10 | \modulesynopsis{Higher-order functions and operations on callable objects.} |
| 11 | |
Raymond Hettinger | 6a458e9 | 2005-03-02 15:10:38 +0000 | [diff] [blame] | 12 | \versionadded{2.5} |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 13 | |
| 14 | The \module{functional} module is for higher-order functions: functions |
| 15 | that act on or return other functions. In general, any callable object can |
| 16 | be treated as a function for the purposes of this module. |
| 17 | |
| 18 | |
| 19 | The \module{functional} module defines the following function: |
| 20 | |
| 21 | \begin{funcdesc}{partial}{func\optional{,*args}\optional{, **keywords}} |
| 22 | Return a new \class{partial} object which when called will behave like |
| 23 | \var{func} called with the positional arguments \var{args} and keyword |
| 24 | arguments \var{keywords}. If more arguments are supplied to the call, they |
| 25 | are appended to \var{args}. If additional keyword arguments are supplied, |
| 26 | they extend and override \var{keywords}. Roughly equivalent to: |
| 27 | \begin{verbatim} |
| 28 | def partial(func, *args, **keywords): |
| 29 | def newfunc(*fargs, **fkeywords): |
| 30 | newkeywords = keywords.copy() |
| 31 | newkeywords.update(fkeywords) |
| 32 | return func(*(args + fargs), **newkeywords) |
| 33 | newfunc.func = func |
| 34 | newfunc.args = args |
| 35 | newfunc.keywords = keywords |
| 36 | return newfunc |
| 37 | \end{verbatim} |
| 38 | |
| 39 | The \function{partial} is used for partial function application which |
| 40 | ``freezes'' some portion of a function's arguments and/or keywords |
Walter Dörwald | 769f821 | 2005-04-14 20:08:59 +0000 | [diff] [blame] | 41 | resulting in a new object with a simplified signature. For example, |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 42 | \function{partial} can be used to create a callable that behaves like |
| 43 | the \function{int} function where the \var{base} argument defaults to |
| 44 | two: |
| 45 | \begin{verbatim} |
| 46 | >>> basetwo = partial(int, base=2) |
Raymond Hettinger | 3e1dd3b | 2005-03-08 07:15:36 +0000 | [diff] [blame] | 47 | >>> basetwo.__doc__('Convert base 2 string to an int.') |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 48 | >>> basetwo('10010') |
| 49 | 18 |
| 50 | \end{verbatim} |
| 51 | \end{funcdesc} |
| 52 | |
| 53 | |
| 54 | |
| 55 | \subsection{\class{partial} Objects \label{partial-objects}} |
| 56 | |
| 57 | |
| 58 | \class{partial} objects are callable objects created by \function{partial()}. |
| 59 | They have three read-only attributes: |
| 60 | |
| 61 | \begin{memberdesc}[callable]{func}{} |
| 62 | A callable object or function. Calls to the \class{partial} object will |
| 63 | be forwarded to \member{func} with new arguments and keywords. |
| 64 | \end{memberdesc} |
| 65 | |
| 66 | \begin{memberdesc}[tuple]{args}{} |
| 67 | The leftmost positional arguments that will be prepended to the |
| 68 | positional arguments provided to a \class{partial} object call. |
| 69 | \end{memberdesc} |
| 70 | |
| 71 | \begin{memberdesc}[dict]{keywords}{} |
| 72 | The keyword arguments that will be supplied when the \class{partial} object |
| 73 | is called. |
| 74 | \end{memberdesc} |
Raymond Hettinger | 3e1dd3b | 2005-03-08 07:15:36 +0000 | [diff] [blame] | 75 | |
| 76 | \class{partial} objects are like \class{function} objects in that they are |
| 77 | callable, weak referencable, and can have attributes. There are some |
| 78 | important differences. For instance, the \member{__name__} and |
| 79 | \member{__doc__} attributes are not created automatically. Also, |
| 80 | \class{partial} objects defined in classes behave like static methods and |
| 81 | do not transform into bound methods during instance attribute look-up. |