Python Glossary Русский

Python 3.4+functoolsfunction

functools.partialmethod

partial application

A descriptor for partial application of a method (like partial, but for methods).

Syntax

attr = functools.partialmethod(method, *args)

Before 3.14 a plain partial did not work inside a class body: partial objects were not descriptors, so self never got bound and calling through an instance dropped the first argument — partialmethod was created for exactly that case. Since 3.14 partial is a descriptor, but it passes the instance after the frozen positional arguments rather than in the self slot, so methods still need partialmethod. The frozen positional arguments land right after self, so they only pin the leading parameters; anything further along has to be fixed by keyword.

Examples

from functools import partialmethod
class Cell:
    def set(self, v): self.value = v
    set_on = partialmethod(set, True)
c = Cell()
c.set_on()
print(c.value)   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy