Skip to content

Commit bbb847b

Browse files
author
Vimal
committed
* Updated README.md with notes on inheritance and MRO.
* Updated 15-multiple-inheritance-2.py
1 parent c4b71ac commit bbb847b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

15-multiple-inheritance-2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
# Python supports multiple inheritance
66
# and uses a depth-first order when searching for methods.
7-
87
# This search pattern is call MRO (Method Resolution Order)
98

109
# This is a second example, which shows the lookup of 'dothis()'.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,32 @@
4747
-------------
4848
#### 10. Multiple Inheritance and how lookup works
4949

50+
* Any class can inherit from other classes.
51+
* Any python class can inherit from multiple classes at the same time.
52+
* The class that inherits another class is called the Base/Child class.
53+
* The class being inherited by the Child class is called the Parent class.
54+
* The child class inherits any methods and attributes defined in the parent classes.
55+
5056
-------------
5157
#### 11. Method Resolution Order (MRO)
5258

59+
* Python has a method lookup order, called `MRO` (Method Resolution Order)
60+
* The MRO path can be printed to stdout using `<class-name>.mro()`
61+
* Python, by default, uses a depth-first ordering while following MRO.
62+
63+
* ie.. imaging you have four classes, A, B, C, D.
64+
65+
1. You instance is created from `D`.
66+
2. `D` inherits from `B` and `C`
67+
3. `B` inherits from `A`.
68+
4. Both `C` and `A` has a method with the same name.
69+
5. Since python follows a depth-first MRO, the method is called from `A`
70+
71+
REFERENCE: Check the code examples in:
72+
* 14-multiple-inheritance-1.py
73+
* 15-multiple-inheritance-2.py
74+
* 16-multiple-inheritance-3.py
75+
5376
--------------
5477
#### 12. Decorators
5578

0 commit comments

Comments
 (0)