Partial methods in Python, enabled by the functools.partialmethod function, provides a way to create modified versions of existing functions. These modified versions, known as partial methods, are intentionally designed to serve as method definitions rather than being callable directly. They come in handy when customizing a function by fixing certain parameters.

functools.partialmethod in Python

In Python, functools.partialmethod is a function within the functools module. It returns a new partial method descriptor that behaves similarly to a partial function but is meant for method definitions. The resulting object is callable and can be treated as if it were the original function.

For example, if a function requires two parameters, we can create a partial function from it that takes the first parameter as an argument. Later, we can call it using the other value as the parameter, making it easier to create modified versions of existing functions.

Syntax

The signature for the partialmethod function is as follows. This function returns a new partial method descriptor that, when called, behaves like a normal function, with ‘self’ as the first positional argument, ‘args’ as in a partial function, and keyword arguments as keywords. If additional arguments are passed to the call, they will be appended to ‘args’. Similarly, if additional keyword arguments are provided, they will extend and override the existing keywords.

partial(func,/,*args,*keywords)

Python partialmethod Examples:

Example 1:

In this scenario, we set the city to “Hyderabad” during object creation. Later on, we have the flexibility to change this value by assigning any desired city name of our choice.

from functools import partialmethod

class PartialDemo:
    def __init__(self):
        self.city = 'Hyderabad'
    # Defining method
    def _city(self, type):
        self.city = type
  
  #Using partialmethod
    set_mumbai = partialmethod(_city, type ='mumbai')
    set_chennai = partialmethod(_city, type ='chennai')
      
#Object creation  
obj = PartialDemo()
print(obj.city)

#Resetting city value
obj.set_chennai()
print(obj.city)

Output:

Hyderabad
chennai

Example 2:

In this instance, we are calculating the simple interest for both a savings account and a current account, each with its own distinct rate of interest.

from functools import partialmethod

class PartialDemo:
    def __init__(self):
        self.interest = 0
        self.principal=1000
    # Defining method
    def _si(self, r,n):
        self.interest=self.principal*r*n/100.0
  
  #Using partialmethod
    set_current = partialmethod(_si, r=3,n=3)
    set_saving = partialmethod(_si, r=7,n=3)
  
#Object creation  
obj = PartialDemo()
print(obj.interest)

#computing for current account 
obj.set_current()
print("Interest on current account for principal of 1000 will be",obj.interest)

#computing for saving account
obj.set_saving()
print("Interest on current account for principal of 1000 will be",obj.interest)

Output:

0
Interest on current account for principal of 1000 will be 90.0
Interest on current account for principal of 1000 will be 210.0

Example 3:

In this scenario, we are calculating the summation of numbers in the series up to the given number ‘n’.

from functools import partialmethod
 
class PartialDemo:
    def __init__(self):
        self.sum=0
    # Defining method
    def _series(self, n):
        for i in range(1,n+1):
            self.sum=self.sum+i
  
  #Using partialmethod
    set_series=partialmethod(_series,5)
 
#Object creation  
obj = PartialDemo()
print(obj.sum)

#computing for current account 
obj.set_series()
print(obj.sum)

Output:

0
15

Conclusion:

Therefore, this method provides a descriptor that is not intended to be directly callable. Instead, it serves as a means to define new methods.

References

Happy Learning 🙂