How to call method of another class in your class in Python

In this tutorial, I am gonna show you how to access the method of another class from your class or child class by using dot(.) operator.

Call method from another class in a different class in Python

We can call the method of another class by using their class name and function with dot operator.
For Example:-
If the 1st class name is class A and its method is method_A
and the second class is class B and its method is method_B
Then we can call method_A from class B by following way:

class A:
     method_A(self):
      {}
class B:
     method_B(self):
     A.method_A()   (like this ....)
     {}

   
Details:
         A: class name
         .: dot operator
method_A(): method of class A. 

 

Now I am gonna create a 1st parent class which has a method name sum which will return the sum of 2 nos.

#create parent class whose method is called by your class 
class parent:
    def sum(self,a,b):
        return a+b

Now I am going to create another class from there we will call sum method of a parent class.

class your_class:
    def Fun(self,a,b):
        self.a=a
        self.b=b
        '''
        we can call method of another class
        by using their class name and function with dot operator.
        '''
        x=parent.sum(self,a,b)
        print("sum=",x)

Complete code in a single window:

Python program to call a method from another class

#create parent class whose method is called by your class 
class parent:
    def sum(self,a,b):
        return a+b
class your_class:
    def Fun(self,a,b):
        self.a=a
        self.b=b
        '''
        we can call method of another class
        by using their class name and function with dot operator.
        '''
        x=parent.sum(self,a,b)
        print("sum=",x)
#class object of child class or 
ob=your_class()
x=int(input("enter 1st no.\n"))
y=int(input("enter 2nd no.\n"))
#fuction call of your class
ob.Fun(x,y)

Run this code online

Output:

enter 1st no.6
enter 2nd no.5
sum= 11

You may also read:

8 responses to “How to call method of another class in your class in Python”

  1. shaik.mohammed arshad says:

    I think a little bit explanation about how the method from class parent is accessed without
    creating an object to the parent class

  2. Shan says:

    what if these classes are different py files ?

  3. junior tiro says:

    hello, thanks for your tutorial I am viewing it over in Papua new guinea and i am very blessed.

    I have one question. and that is please can you explain the difference between class, main and function and how to combine them self.

    • Tanya says:

      class is a blueprint so everything can be created inside it and later that class can be used any number of times in the same program or even by importing that class in another program(also called as module in python)

      functions are made inside class to use them later on according to our needs.

      for example- class is group of function that an oven performs like -start,stop,timer,temperature ..
      and object is your cake that wants to use all those functions.

  4. marc says:

    this was amazing. Thankyou for your valuable explanation

  5. Sudip says:

    Import this class from another file to current file.

  6. Gautami says:

    without inheritance how is it possible to access the methods from other class.

  7. ashraf says:

    Thanks
    but what if the method is __init__?

Leave a Reply

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