How To Add A Method To An Existing Object Instance In Python?

Add a method to an existing object instance in Python

If you are looking for an instruction to add a method to an existing object instance in Python, keep reading our article. Today, we will provide some methods to show you how to do it.

Add a method to an existing object instance in Python

Use the function __get__()

In the Python programming language, the function __get__() can be used to define a dynamic method, attribute, or value. When declaring the function in a class, its performance is to return the value. We can add a method to an existing object when declaring the function outer the class.

Syntax:

object.newMethod = newMethod.__get__(object)

Detailed implementation in the following code:

# Create an object class
class Student:
  def __init__(self, name, age, subject):
    self.name = name
    self.age = age
    self. subject = subject
  
  def getName(self):
    return self.name
 
  def getAge(self):
    return self.age
  
# Initialize an object
std1 = Student("John", 15, "Calculus")
 
# Create a new method
def getSubject(self):
  return self.subject
 
# Add the method to the object std1
std1.getSubject = getSubject.__get__(std1)
 
print(std1.getSubject())

Result:

Calculus

Use types.MethodType

Types.MethodType is a method that belongs to the library types and enables users to define a new method for an existing object. This way, an existing object can use a new-defined method as the initial method in the object class.  

Syntax:

object.newMethod = types.MethodType(newMethod, object)

Code:

import types
 
# Create an object class
class Student:
  def __init__(self, name, age, subject):
    self.name = name
    self.age = age
    self. subject = subject
  
  def getName(self):
    return self.name
 
  def getAge(self):
    return self.age
  
# Initialize an object
std1 = Student("John", 15, "Calculus")
 
# Create a new method
def getSubject(self):
  return self.subject
 
# Add the method to the object std1
std1.getSubject = types.MethodType(getSubject, std1)
 
print(std1.getSubject())

Result:

Calculus

Use functools.partial

The module “functools” provides the type partial returning the object that operates as a function called. When using this way, a partial function is applied to the object and makes the object have a method similar to methods inside the class object.

Code:

from functools import partial
 
# Create an object class
class Student:
  def __init__(self, name, age, subject):
    self.name = name
    self.age = age
    self. subject = subject
  
  def getName(self):
    return self.name
 
  def getAge(self):
    return self.age
  
# Initialize an object
std1 = Student("John", 15, "Calculus")
 
# Create a new method
def getSubject(self):
  return self.subject
 
# Add the method to the object std1
std1.getSubject = partial(getSubject, std1)
 
print(std1.getSubject())

Result:

Calculus

Summary

Our tutorial provides some detailed examples to add a method to an existing object instance in Python. We hope you gain much knowledge from our article. If you have any troubles, do not hesitate to give us your questions. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *