How to Call Class Methods from Other Classes in Python
In Python, calling a method of one class from another is a fundamental concept in object-oriented programming.
This guide explores how to call methods of one class (including instance methods, static methods, and class methods) from within another class, using instantiation and direct class access.
Calling Instance Methods
To call an instance method of one class from another, you typically need an instance of the first class. There are two main ways to achieve this:
Instantiating the Other Class
The simplest approach is to create an instance of the class whose method you want to call, within the method of the calling class:
class A:
def method_a(self, a, b):
return a + b
class B:
def method_b(self, a, b):
instance_of_a = A() # Create an instance of A
return instance_of_a.method_a(a, b) # Call method_a on the instance
b1 = B()
print(b1.method_b(10, 15)) # Output: 25
- In
B.method_b
, we create a new instance ofA
usingA()
. This new instance is completely independent of any otherA
instances that might exist. - Then
instance_of_a.method_a(a, b)
calls the methodmethod_a
from the instanceinstance_of_a
. - This is appropriate when
B
needs to useA
's functionality, but doesn't have a long-term relationship with a specific instance ofA
.
If the class defines an __init__()
method, make sure to provide all required arguments when instantiating it:
class A:
def __init__(self, name, age):
self.name = name
self.age = age
def method_a(self, a, b):
return a + b
class B:
def __init__(self, name, age):
self.name = name
self.age = age
def method_b(self, a, b):
return A(self.name, self.age).method_a(a, b)
b1 = B('tom', 30)
print(b1.method_b(10, 15)) # Output: 25
Passing an Instance as an Argument
A more flexible approach is to pass an instance of class A
to class B
(often through B
's constructor). This establishes a relationship between the objects:
class A:
def method_a(self, a, b):
return a + b
class B:
def __init__(self, instance_of_a):
self.instance_a = instance_of_a # Store the instance of A
def method_b(self, a, b):
return self.instance_a.method_a(a, b) # Call method_a on the *given* instance
a1 = A() # Create an instance of A *outside* of B
b1 = B(a1) # Pass the instance of A to B's constructor
print(b1.method_b(10, 15)) # Output: 25
- This approach is more appropriate when
B
needs to interact with a specific instance ofA
over time, rather than creating a new one every time. It represents a stronger relationship between the objects.
Calling staticmethod
s
staticmethod
s are bound to the class, not an instance. You can call them directly on the class itself, without needing an instance:
class A:
@staticmethod
def method_a(a, b):
return a + b
class B:
def method_b(self, a, b):
return A.method_a(a, b) # Call directly on the class A
b1 = B()
print(b1.method_b(10, 15)) # Output: 25
@staticmethod
decorator is used to define a static method.- Static methods can be called on the class or on an instance.
A.method_a(a, b)
: We call the static method directly on the classA
, not on an instance. This is the key difference from instance methods.
Calling classmethod
s
classmethod
s are also bound to the class, but they receive the class itself (conventionally named cls
) as the first argument:
class A:
_class_variable = 0 # Example class variable
@classmethod
def method_a(cls, a, b):
cls._class_variable = a
return a + b
class B:
def method_b(self, a, b):
return A.method_a(a, b)
b1 = B()
print(b1.method_b(10, 15)) # Output 25
@classmethod
decorator makes the method a class method.cls
: This parameter (conventionally namedcls
) receives the class itself (e.g.,A
) as the first argument. This allows class methods to access and modify class-level state.