Partialmethod class

It is a method definition of an already defined function for specific arguments like a partial function. However, it is not callable but is only a method descriptor. It returns a new partialmethod descriptor. 

Syntax:

partialmethod(func, *args, **keywords)

Example: 

Python3




from functools import partialmethod
 
class Demo:
    def __init__(self):
        self.color = 'black'
 
    def _color(self, type):
        self.color = type
 
    set_red = partialmethod(_color, type='red')
    set_blue = partialmethod(_color, type='blue')
    set_green = partialmethod(_color, type='green')
 
 
obj = Demo()
print(obj.color)
obj.set_blue()
print(obj.color)


Output :

black
blue

Functools module in Python

Functools module is for higher-order functions that work on other functions. It provides functions for working with other functions and callable objects to use or extend them without completely rewriting them. This module has two classes – partial and partialmethod.

Similar Reads

Partial class

A partial function is an original function for particular argument values. They can be created in Python by using “partial” from the functools library. The __name__ and __doc__ attributes are to be created by the programmer as they are not created automatically. Objects created by partial() have three read-only attributes: Syntax:...

Partialmethod class

...

Functions

It is a method definition of an already defined function for specific arguments like a partial function. However, it is not callable but is only a method descriptor. It returns a new partialmethod descriptor....