Open In App

Call a Class Method From another Class in Python

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In object-oriented programming, classes play a pivotal role in organizing and structuring code. Python, being an object-oriented language, allows the creation of classes and their methods to facilitate code modularity and reusability. One common scenario in programming is the need to call a method from one class within another class. In this article, we’ll explore how to achieve this in Python and provide examples using the popular GeeksforGeeks platform.

What is Calling a Class Method From Another Class?

Calling a class method from another class refers to the process of invoking a method defined in one class from within a different class. This capability enhances code organization and promotes the reuse of existing functionality. Python provides a straightforward syntax for achieving this, allowing developers to create modular and maintainable code.

Syntax :

# Define ClassA

class ClassA:

@classmethod

def method_in_class_a(cls):

# Method implementation

pass

# Define ClassB

class ClassB:

def some_method(self):

# Calling the class method from ClassA

ClassA.method_in_class_a()

Call A Class Method From Another Class In Python

Below, are the example of Call A Class Method From Another Class In Python

Example 1: Simple Class Method Call

Let’s consider a scenario where we have two classes, ClassA and ClassB. We want to call a method of ClassA from within ClassB. Here’s how you can achieve this: In this example, ClassB has a method called call_method_from_class_a, and within this method, we call the method_in_class_a of ClassA using ClassA.method_in_class_a().

Python3




class ClassA:
    @staticmethod
    def method_in_class_a():
        print("Method in ClassA")
 
class ClassB:
    def call_method_from_class_a(self):
        ClassA.method_in_class_a()
        print("Method in ClassB")
 
# Create an instance of ClassB
obj_b = ClassB()
 
# Call the method in ClassB, which in turn calls the method in ClassA
obj_b.call_method_from_class_a()


Output

Method in ClassA
Method in ClassB


Example 2: Passing an Instance of another Class

Another approach is to create an instance of the class whose method you want to call and then invoke the method on that instance. Let’s illustrate this with an example: In this example, ClassY creates an instance of ClassX within its call_method_from_class_x method and then calls the method of ClassX.

Python3




class ClassX:
    def method_in_class_x(self):
        print("Method in ClassX")
 
class ClassY:
    def call_method_from_class_x(self):
        instance_x = ClassX()
        instance_x.method_in_class_x()
        print("Method in ClassY")
 
# Create an instance of ClassY
obj_y = ClassY()
 
# Call the method in ClassY, which in turn creates an instance of ClassX and calls its method
obj_y.call_method_from_class_x()


Output

Method in ClassX
Method in ClassY


Conclusion

Calling a class method from another class in Python is a common requirement in object-oriented programming. Understanding the relationships between classes and utilizing class methods or creating instances of other classes are effective ways to achieve this. By employing these techniques, you can create well-structured and modular code that reflects the real-world relationships between different entities in your application.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads