Call a Class Method From another Class in Python
Last Updated :
17 May, 2024
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().
Python
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()
OutputMethod 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.
Python
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()
OutputMethod 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.
Similar Reads
Define and Call Methods in a Python Class In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, prov
3 min read
Python - Call function from another file Given a Python file, we need to call a function in it defined in any other Python file. Example: Suppose there is a file test.py which contains the definition of the function displayText(). #test.py>def displayText(): print( "Geeks 4 Geeks!")We need to call the function displayText() in any other
5 min read
How to import a class from another file in Python ? In this article, we will see How to import a class from another file in Python.Import in Python is analogous to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is that the most common way of in
4 min read
Call Parent class method - Python In object-oriented programming in Python, the child class will inherit all the properties and methods of the parent class when the child class inherits the parent class. But there may be some cases when the child class has to call the methods of the parent class directly. Python has an easy and effe
5 min read
Extend Class Method in Python In Python, class methods are functions that are bound to the class rather than the instance of the class. This means they receive the class (cls) as the first argument instead of the instance (self). Extending a class method refers to enhancing or customizing the behavior of an inherited class metho
3 min read
How to clone a method code in Python? With the help of methodName.__code__.replace() method, we can clone the code of builtin method as well as any other defined method and we can also fix the positional only arguments in any of the cloned method code by using methodName.__code__.replace() method. Syntax : methodName.__code__.replace()
1 min read
Calling a Super Class Constructor in Python Classes are like creating a blueprint for an object. If we want to build a building then we must have the blueprint for that, like how many rooms will be there, its dimensions and many more, so here the actual building is an object and blueprint of the building is a class. A Class is a user-defined
4 min read
Accessing Attributes and Methods in Python In Python, attributes and methods define an object's behavior and encapsulate data within a class. Attributes represent the properties or characteristics of an object, while methods define the actions or behaviors that an object can perform. Understanding how to access and manipulate both attributes
3 min read
How to Get a List of Class Attributes in Python? Getting a list of class attributes in Python means identifying all variables defined at the class level, excluding instance attributes and methods. For example, a class might have attributes like name, age and location. The output will be a list or dictionary showing these attribute names and their
3 min read