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)
Output:
enter 1st no.6 enter 2nd no.5 sum= 11
You may also read:
- An introduction to classes and objects in python
- Concept of Inheritance, Superclass and Subclass in Python
I think a little bit explanation about how the method from class parent is accessed without
creating an object to the parent class
what if these classes are different py files ?
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.
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.
this was amazing. Thankyou for your valuable explanation
Import this class from another file to current file.
without inheritance how is it possible to access the methods from other class.
Thanks
but what if the method is __init__?