Content · Programming · Python

Python functools.partialmethod() Function: Usage and Examples

Pre-fill arguments for methods declared inside a class.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Pre-fill arguments for methods declared inside a class.

What You Will Learn


partialmethod is a descriptor intended for class definitions. It creates bound methods that retain the instance argument and pre-apply selected arguments.

Step-by-Step Example


The example below makes the behavior visible with small input values that you can change safely.

from functools import partialmethod
class Light:
    def set_state(self, value):
        return f'State: {value}'
    turn_on = partialmethod(set_state, True)
    turn_off = partialmethod(set_state, False)
light = Light()
print(light.turn_on())
print(light.turn_off())

How the Example Works


  • The imports provide the standard-library tools used in the demonstration: functools.
  • The class Light groups data and behavior; its methods run when called on an instance.
  • The variable light holds the result of Light(). Later lines use that value or inspect how it changes.
  • The print() calls make the return values or state visible so you can compare them with the explanation and check your prediction.

Expected output from the example:

State: True
State: False

When You Would Use This


These tools are useful when composing small reusable functions, adapting callbacks, or eliminating repeated work. Pay attention to whether the operation returns a new object, updates state, or produces lazy results. Test a small success case and a boundary case before integrating it into an application.

Practice: Modify the Code


Create open and close shortcuts for a configurable method.

Verify the changed output against the behavior described above, including the relevant invalid or boundary case.

For complete parameters and version details, consult the official Python documentation.

Common Beginner Mistakes


  • Using partial() in a class definition and assuming it binds self like partialmethod().
  • Using a wrapper without checking which original function, arguments and return values it preserves.

Key Takeaways


  • Pre-fill arguments for methods declared inside a class.
  • partialmethod is a descriptor intended for class definitions.
  • Apply this operation only after checking the assumptions and input conditions shown in the example.
Share this resourceSend this page to someone who may find it useful.