Method Resolution Order (MRO) in Python plays a crucial role in allowing the language to leverage inheritance, super functions, and class hierarchies. It handles multiple inheritances by specifying the order in which parent classes are searched when accessing a method.
Python utilizes an algorithm known as C3 linearization, or just C3, to determine the correct method to use in cases of multiple inheritance. This guide will show you how to determine the MRO of a class in Python.
Step 1: Understanding Python Class Definitions
Before we dive into MRO, it’s important that you understand basic Python class definitions. With Python, you can model real-world things by creating a class. Each class has properties and actions, represented by methods. Here’s a basic Python class definition:
1 2 3 |
class MyFirstClass: def greet(self): print('Hello World') |
Step 2: Understanding Inheritance in Python
In Python, one class can inherit from another class. This is known as inheritance and it is a key feature of Object-Oriented Programming.
By leveraging inheritance, we can reduce redundant code and mimic real-world relationships between objects. Here is an example of inheritance where ChildClass
inherits from ParentClass
:
1 2 3 4 5 6 |
class ParentClass: def greet(self): print('Hello World') class ChildClass(ParentClass): pass |
Step 3: Introducing Multiple Inheritance and the Need for MRO
Python supports multiple inheritance, a feature that allows a subclass to inherit features from multiple parent classes. But what happens when the same method is defined in multiple superclasses? That’s where the Method Resolution Order comes in:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Mother(object): def speak(self): print('Mother speaking!') class Father(object): def speak(self): print('Father speaking!') class Child(Mother, Father): pass ch = Child() ch.speak() |
In this case, Python would use the MRO to determine which method to call.
Step 4: Finding the MRO in Python
To get the Method Resolution Order of a class, you can use the mro method or the __mro__ attribute. Here’s how to get the MRO of a class:
1 |
print(Child.mro()) |
Or, you can use the __mro__ attribute:
1 |
print(Child.__mro__) |
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Mother(object): def speak(self): print('Mother speaking!') class Father(object): def speak(self): print('Father speaking!') class Child(Mother, Father): pass ch = Child() print(Child.mro()) print(Child.__mro__) ch.speak() |
[<class '__main__.Child'>, <class '__main__.Mother'>, <class '__main__.Father'>, <class 'object'>] (<class '__main__.Child'>, <class '__main__.Mother'>, <class '__main__.Father'>, <class 'object'>) Mother speaking!
Conclusion
Method Resolution Order (MRO) in Python is a powerful tool that helps Python to handle multiple inheritances effectively. This tutorial has guided you on how to find the MRO of a class in Python using the mro method and the __mro__ attribute.
By understanding the MRO, you can write Python code that leverages the power of inheritance without falling into common obstacles associated with this feature.