Diamond problem is also known as the "Deadly Diamond of Death", This problem mainly occurs in object-oriented programming in Python. It mainly arises when a class inherits from two or more classes that themselves share a common superclass. This situation creates ambiguity in the inheritance hierarchy, leading to confusion about which properties or methods of the shared superclass should be inherited.
In this article, we will understand What is Diamond Problem in Python and How we can solve this problem.
Diamond Shape inheritance

- Shape: The common superclass representing general properties and behaviors of a shape.
- Circle: Intermediate subclasses that inherit from
Shape, each adding specialized attributes or behaviors - Square: Intermediate subclasses that inherit from
Shape, each adding specialized attributes or behaviors. - Cylinder: A subclass that inherits from both
CircleandSquare, potentially combining their attributes and behaviors.
Here's how this hierarchy looks in Python:
class shape:
def display(self):
print("class shape")
class circle(shape):
def display(self):
print("class circle")
class square(shape):
def display(self):
print("class square")
class cylinder(circle, square):
pass
obj = cylinder()
obj.display() # Calls circle's display method
Output
class circle
Explanation:
- The given code does not give Diamond Problem in python because the python resolves multiple inheritance dispute using the Method Resolution order in short we can call it MRO .
Problem caused by Diamond Pattern
The diamond problem in Python is a problem that can occur when a class inherits from multiple classes that share a common ancestor:
- Ambiguity : If
Cylindercalls a method or accesses an attribute fromShape, it is unclear whether to use the version inherited throughCircleorSquare. - Duplication : When both subclasses are called , they may both call the base class method, resulting in duplicate calls.
Solving Diamond problem in Python
Python uses method resolution Order (MRO). The MRO provides a clear and consistent sequence in which classes are searched for methods or attributes, ensuring no ambiguity. The MRO is computed using the C3 Linearization Algorithm, which respects the order of inheritance while avoiding duplication.
Example of how python handles Diamond problem
Using Method Resolution order:
Python addresses the Diamond Problem by using the Method Resolution Order (MRO). The MRO ensures that methods are resolved in a predictable and consistent order, eliminating ambiguity and duplication.
class Shape:
def draw(self):
print("class shape")
class Circle(Shape):
def draw(self):
print("class circle")
class Square(Shape):
def draw(self):
print("class square")
class Cylinder(Circle, Square):
pass
obj = Cylinder()
obj.draw() # Resolves using the MRO
print(Cylinder.mro()) # Displays the MRO
Output
class circle [<class '__main__.Cylinder'>, <class '__main__.Circle'>, <class '__main__.Square'>, <class '__main__.Shape'>, <class 'object'>]
Using Super():
Python's super() function works with the MRO to ensure that methods in a diamond hierarchy are called in a predictable order. This allows cooperative behavior among classes.
class shape:
def greet(self):
print("class shape called")
class circle(shape):
def greet(self):
super().greet()
print("class circle called")
class square(shape):
def greet(self):
super().greet()
print("class square called")
class cylinder(circle, square):
def greet(self):
super().greet()
print("class cylinder called")
d = cylinder()
d.greet()
Output
class shape called class square called class circle called class cylinder called
Explanation:
- The
super()function respects the MRO, ensuring the correct order of method calls.