Extend Class Method in Python
Last Updated :
10 May, 2025
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 method in a subclass. Example:
Python
class Vehicle:
@classmethod
def start(cls):
return "Vehicle starting"
class Car(Vehicle):
@classmethod
def start(cls):
return f"{super().start()} → Car starting"
print(Car.start())
OutputVehicle starting → Car starting
Explanation:
- Vehicle Class defines a class method start() that returns "Vehicle starting".
- Car Class inherits from Vehicle and overrides the start() method.
- super().start() calls the start() method from Vehicle, returning "Vehicle starting" and appends " → Car starting".
Why extend a class method?
Extending a class method allows you to:
- Reuse existing logic from the parent class.
- Add or modify behavior specifically for the subclass.
- Keep your code DRY (Don't Repeat Yourself) and more maintainable.
- Follow OOP principles, especially inheritance and polymorphism.
By using super(), you can call the method from the parent class and build upon it.
Syntax
class Parent:
@classmethod
def method(cls):
# base class method logic
class Child(Parent):
@classmethod
def method(cls):
super().method() # extend base class method
# child class method logic
Examples of extending class methods
Example 1: In this example, we are extending and modifying the behavior of a parent class method in the subclass, while preserving the original behavior by using super().
Python
class Animal:
@classmethod
def sound(cls):
print("Animal sound")
class Dog(Animal):
@classmethod
def sound(cls):
super().sound()
print("Dog bark")
Dog.sound()
OutputAnimal sound
Dog bark
Explanation:
- Animal Class defines a class method sound() that prints "Animal sound".
- Dog Class inherits from Animal and overrides the sound() method.
- super().sound() in the Dog class, it calls the sound() method from Animal using super(), printing "Animal sound".
Example 2: This example defines a Vehicle class with a method that returns the string "Vehicle", a Car class that extends Vehicle and modifies the start method and an ElectricCar class that further extends Car and overrides the start method.
Python
class Vehicle:
@classmethod
def start(cls):
return "Vehicle"
class Car(Vehicle):
@classmethod
def start(cls):
return f"{super().start()} → Car"
class ElectricCar(Car):
@classmethod
def start(cls):
print(f"{super().start()} → ElectricCar")
ElectricCar.start()
OutputVehicle → Car → ElectricCar
Explanation:
- Vehicle Class defines a class method start() that returns "Vehicle".
- Car Class inherits from Vehicle and overrides start() to call super().start() (from Vehicle) and adds " → Car".
- ElectricCar Class inherits from Car and overrides start() to call super().start() (from Car) and prints " → ElectricCar".
Example 3: This code defines a Triangle class that tracks the number of instances created, and a Perimeter class that calculates and displays the perimeter of the triangle.
Python
class Triangle:
count = 0
def __init__(self, name, a, b, c):
self.name, self.a, self.b, self.c = name, a, b, c
Triangle.count += 1
class Perimeter(Triangle):
def display(self):
return f"{self.name}: {self.a}, {self.b}, {self.c} | Perimeter: {self.a + self.b + self.c}"
print(Perimeter("PQR", 2, 3, 4).display())
OutputPQR: 2, 3, 4 | Perimeter: 9
Explanation:
- Triangle Class initializes a triangle with a name, sides a, b, c and increments the count of instances.
- Perimeter Class inherits from Triangle and defines display() to show the triangle’s name, sides and perimeter.
Similar Reads
Python List extend() Method In Python, extend() method is used to add items from one list to the end of another list. This method modifies the original list by appending all items from the given iterable. Using extend() method is easy and efficient way to merge two lists or add multiple elements at once.Letâs look at a simple
2 min read
Python List append() Method append() method in Python is used to add a single item to the end of list. This method modifies the original list and does not return a new list. Let's look at an example to better understand this.Pythona = [2, 5, 6, 7] # Use append() to add the element 8 to the end of the list a.append(8) print(a)O
3 min read
Extending a list in Python In Python, a list is one of the most widely used data structures for storing multiple items in a single variable. Often, we need to extend a list by adding one or more elements, either from another list or other iterable objects. Python provides several ways to achieve this. In this article, we will
2 min read
Python __len__() magic method Python __len__ is one of the various magic methods in Python programming language, it is basically used to implement the len() function in Python because whenever we call the len() function then internally __len__ magic method is called. It finally returns an integer value that is greater than or eq
2 min read
classmethod() in Python The classmethod() is an inbuilt function in Python, which returns a class method for a given function. This means that classmethod() is a built-in Python function that transforms a regular method into a class method. When a method is defined using the @classmethod decorator (which internally calls c
8 min read
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 List methods Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, weâll explore all Python list methods with a simple example.List MethodsLet's look at different list methods in Python:append(): Adds an
3 min read
Python __add__() magic method Python __add__() function is one of the magic methods in Python that returns a new object(third) i.e. the addition of the other two objects. It implements the addition operator "+" in Python. Python __add__() Syntax Syntax: obj1.__add__(self, obj2) obj1: First object to add in the second object.obj2
1 min read
Python Metaclass __new__() Method In Python, metaclasses provide a powerful way to customize the creation of classes. One essential method in metaclasses is __new__, which is responsible for creating a new instance of a class before __init__ is called. Understanding the return value of __new__ in metaclasses is crucial for implement
3 min read
Instance method in Python A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to
2 min read