summaryrefslogtreecommitdiffstats
path: root/Doc/library/functools.rst
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2013-11-03 06:41:46 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2013-11-03 06:41:46 (GMT)
commitf4cb48a72b20b2d166d5303426ab4098243c35e1 (patch)
tree030f58e9648df6a8a92a4f28842dd2393f4b1079 /Doc/library/functools.rst
parentb19ff4174183e1e519c99243cff0e3c30e9ac258 (diff)
downloadcpython-f4cb48a72b20b2d166d5303426ab4098243c35e1.zip
cpython-f4cb48a72b20b2d166d5303426ab4098243c35e1.tar.gz
cpython-f4cb48a72b20b2d166d5303426ab4098243c35e1.tar.bz2
Issue #4331: Added functools.partialmethod
Initial patch by Alon Horev
Diffstat (limited to 'Doc/library/functools.rst')
-rw-r--r--Doc/library/functools.rst43
1 files changed, 42 insertions, 1 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.
-