ID EN
Functools

partialmethod

Python 3.11

Return a new partialmethod descriptor which behaves like partial except that it is designed to be used as a method definition rather than being directly callable.

Syntax

PYTHON
class functools.partialmethod(func, /, *args, **keywords)

Examples

Example 1
PYTHON
>>> class Cell:
...     def __init__(self):
...         self._alive = False
...     @property
...     def alive(self):
...         return self._alive
...     def set_state(self, state):
...         self._alive = bool(state)
...     set_alive = partialmethod(set_state, True)
...     set_dead = partialmethod(set_state, False)
...
>>> c = Cell()
>>> c.alive
False
>>> c.set_alive()
>>> c.alive
True

See Also

partialmethod partial descriptor classmethod() staticmethod() partialmethod partial object partialmethod