Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. Inheritance / super() / Multiple Inheritance

Inheritance / super() / Multiple Inheritance

Since: Python 2(2000)

In Python, you inherit a class using the syntax class ChildClass(ParentClass). A child class inherits the methods and properties of its parent class and can override them as needed. super() is used to call a method defined in the parent class. Python supports multiple inheritance, which lets you define a class with more than one parent class, but this can make the design more complex and should be used with care.

Syntax

class ChildClass(ParentClass):
    def __init__(self, ...):
        super().__init__(...) # Call the parent __init__

# Method override
    def method(self):
        super().method() # Also call the parent method if needed
        # Additional logic

# Multiple inheritance
class ChildClass(Parent1, Parent2):
    pass

Syntax / Function Reference

Syntax / FunctionDescription
class Child(Parent)Defines a child class that inherits from a parent class.
super()Returns a proxy object that refers to the parent class (or the next class in the MRO).
super().__init__()Calls the initialization method of the parent class.
class Child(Parent1, Parent2)Defines a class that inherits from multiple parent classes.
Class.__mro__An attribute that returns the MRO (Method Resolution Order) as a tuple.

Sample Code

sample_inheritance_super.py
class Fighter:
    def __init__(self, name):
        self.name = name

    def battle(self):
        return f"{self.name} is here"

    def __str__(self):
        return f"{self.__class__.__name__}({self.name})"

class Yakuza(Fighter):
    def __init__(self, name, family):
        super().__init__(name) # Call the parent __init__
        self.family = family # Attribute specific to the child class

    def battle(self): # Override the parent method
        return f"{self.name} is coming at you!"

    def intimidate(self): # Method specific to the child class
        return f"{self.name} goes down!"

class Freelancer(Fighter):
    def battle(self):
        return f"{self.name} fights back Dragon of Dojima style!"

y = Yakuza('Shimano Futoshi', 'Shimano Family')
f = Freelancer('Kiryu Kazuma')

print(y.battle()) # Shimano Futoshi is coming at you!
print(f.battle()) # Kiryu Kazuma fights back Dragon of Dojima style!
print(y.intimidate()) # Shimano Futoshi goes down!
print(y.family) # Shimano Family

# Check inheritance relationships with isinstance
print(isinstance(y, Yakuza)) # True
print(isinstance(y, Fighter)) # True (Fighter is also inherited)

# Override a method while still calling the parent version via super()
class BossYakuza(Yakuza):
    def battle(self):
        base = super().battle() # Call Yakuza's battle
        return f"{base} (The Boss)"

boss = BossYakuza('Hayashi Hiroshi', 'Omi Alliance')
print(boss.battle()) # Hayashi Hiroshi is coming at you! (The Boss)

# Multiple inheritance and MRO (C3 linearization algorithm)
class Flyable:
    def move(self):
        return "Not bad, not bad at all."

class Swimmable:
    def move(self):
        return "A man of my caliber... I'd rather not have to kill you here."

class Duck(Flyable, Swimmable):
    pass

duck = Duck()
print(duck.move()) # Not bad, not bad at all. (Flyable is resolved first)
print(Duck.__mro__) # Check the MRO order
# (<class 'Duck'>, <class 'Flyable'>, <class 'Swimmable'>, <class 'object'>)

Running the code produces the following output:

python3 inheritance_super.py
Shimano Futoshi is coming at you!
Kiryu Kazuma fights back Dragon of Dojima style!
Shimano Futoshi goes down!
Shimano Family
True
True
Hayashi Hiroshi is coming at you! (The Boss)
Not bad, not bad at all.
(<class '__main__.Duck'>, <class '__main__.Flyable'>, <class '__main__.Swimmable'>, <class 'object'>)

Notes

The MRO (Method Resolution Order) determines the order in which Python searches for a method. Python resolves the inheritance tree using the C3 linearization algorithm. You can inspect it with Class.__mro__, which shows which class's method takes priority in a multiple inheritance scenario.

When you define __init__() in a child class, you should generally call the parent's __init__() via super(). If you skip this call, the parent class is not initialized, its instance variables are never set, and calling any parent method that relies on them will raise an error.

Multiple inheritance increases code complexity. Where possible, the "composition over inheritance" pattern (delegation instead of subclassing) is recommended as an alternative. Using a class purely as a Mixin — a parent class that adds a specific, self-contained feature — is a case where multiple inheritance is genuinely useful.

If you find any errors or copyright issues, please .