Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

functools.partialmethod

Defines a method variant that pre-binds selected arguments while preserving normal instance binding.

Version: Python 3.14; repository image, production runtime not attested

Syntax

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

Defines a method variant that pre-binds selected arguments while preserving normal instance binding.

The instance is inserted before the frozen positional arguments, allowing several named operations to share one implementation.

Put a partialmethod descriptor on a class to expose a variant of an operation. For a method descriptor it delegates binding to the underlying __get__. For a non-descriptor callable it supplies self before the stored positional arguments. Python 3.14 positional Placeholder slots can defer selected values. The sample's enable binds True to Switch.set for that particular instance.

from functools import partialmethod, Placeholder
class Gauge:
    def pair(self, left, right):
        return self, left, right
    right_zero = partialmethod(pair, Placeholder, 0)
g = Gauge()
assert g.right_zero(3) == (g, 3, 0)

Examples

import functools
class Switch:
    def set(self, on): self.on = on
    enable = functools.partialmethod(set, True)
    disable = functools.partialmethod(set, False)

s = Switch()
s.enable()
print(s.on)
s.disable()
print(s.on)

Result

True
False

Put a partialmethod descriptor on a class to expose a variant of an operation. For a method descriptor it delegates binding to the underlying `__get__`. For a non-descriptor callable it supplies self before the stored positional arguments. Python 3.14 positional Placeholder slots can defer selected values. The sample's enable binds True to Switch.set for that particular instance.

Sources