diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2013-11-03 06:41:46 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2013-11-03 06:41:46 (GMT) |
commit | f4cb48a72b20b2d166d5303426ab4098243c35e1 (patch) | |
tree | 030f58e9648df6a8a92a4f28842dd2393f4b1079 /Doc | |
parent | b19ff4174183e1e519c99243cff0e3c30e9ac258 (diff) | |
download | cpython-f4cb48a72b20b2d166d5303426ab4098243c35e1.zip cpython-f4cb48a72b20b2d166d5303426ab4098243c35e1.tar.gz cpython-f4cb48a72b20b2d166d5303426ab4098243c35e1.tar.bz2 |
Issue #4331: Added functools.partialmethod
Initial patch by Alon Horev
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/functools.rst | 43 | ||||
-rw-r--r-- | Doc/whatsnew/3.4.rst | 20 |
2 files changed, 61 insertions, 2 deletions
diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 14e1351..4eb263a 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -194,6 +194,48 @@ The :mod:`functools` module defines the following functions: 18 +.. class:: partialmethod(func, *args, **keywords) + + Return a new :class:`partialmethod` descriptor which behaves + like :class:`partial` except that it is designed to be used as a method + definition rather than being directly callable. + + *func* must be a :term:`descriptor` or a callable (objects which are both, + like normal functions, are handled as descriptors). + + When *func* is a descriptor (such as a normal Python function, + :func:`classmethod`, :func:`staticmethod`, :func:`abstractmethod` or + another instance of :class:`partialmethod`), calls to ``__get__`` are + delegated to the underlying descriptor, and an appropriate + :class:`partial` object returned as the result. + + When *func* is a non-descriptor callable, an appropriate bound method is + created dynamically. This behaves like a normal Python function when + used as a method: the *self* argument will be inserted as the first + positional argument, even before the *args* and *keywords* supplied to + the :class:`partialmethod` constructor. + + Example:: + + >>> class Cell(object): + ... @property + ... def alive(self): + ... return self._alive + ... def set_state(self, state): + ... self._alive = bool(state) + ... set_alive = partialmethod(set_alive, True) + ... set_dead = partialmethod(set_alive, False) + ... + >>> c = Cell() + >>> c.alive + False + >>> c.set_alive() + >>> c.alive + True + + .. versionadded:: 3.4 + + .. function:: reduce(function, iterable[, initializer]) Apply *function* of two arguments cumulatively to the items of *sequence*, from @@ -431,4 +473,3 @@ differences. For instance, the :attr:`__name__` and :attr:`__doc__` attributes are not created automatically. Also, :class:`partial` objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up. - diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst index a570af8..ed6daee 100644 --- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -342,7 +342,25 @@ handling). functools --------- -New :func:`functools.singledispatch` decorator: see the :pep:`443`. +The new :func:`~functools.partialmethod` descriptor bring partial argument +application to descriptors, just as :func:`~functools.partial` provides +for normal callables. The new descriptor also makes it easier to get +arbitrary callables (including :func:`~functools.partial` instances) +to behave like normal instance methods when included in a class definition. + +(Contributed by Alon Horev and Nick Coghlan in :issue:`4331`) + +The new :func:`~functools.singledispatch` decorator brings support for +single-dispatch generic functions to the Python standard library. Where +object oriented programming focuses on grouping multiple operations on a +common set of data into a class, a generic function focuses on grouping +multiple implementations of an operation that allows it to work with +*different* kinds of data. + +.. seealso:: + + :pep:`443` - Single-dispatch generic functions + PEP written and implemented by Ćukasz Langa. hashlib |