Slide 1

Slide 1 text

Method Resolution Order (MRO) in Python Sanyam Khurana DjangoCon US 2022 | San Diego, CA, USA ErSanyamKhurana CuriousLearner

Slide 2

Slide 2 text

ErSanyamKhurana CuriousLearner def __who_am_i__(): • One of You - A part of the community • Masters in CS (ML Specialization) from Georgia Tech. • Maintainer Django Phone Verify, Django-sites, DjangoProject.com • Individual member of DSF (Django Software Foundation) • Bug-Triager for CPython • Contributor to Mozilla Firefox Browser 👋

Slide 3

Slide 3 text

ErSanyamKhurana CuriousLearner Method Resolution Order https://speakerdeck.com/CuriousLearner/ method-resolution-order-mro-python/

Slide 4

Slide 4 text

ErSanyamKhurana CuriousLearner Goal of the talk 🤔 • Understand how & why MRO evolved. • Do not ever get tripped by Multiple inheritance. • Always use inheritance in a more meaningful way — going from specialized to generic classes.

Slide 5

Slide 5 text

ErSanyamKhurana CuriousLearner Multiple Inheritance 👪 • Languages like Java / C# do not support Multiple Inheritance. • They don’t want to get caught by the Diamond problem. • Python solves this problem!

Slide 6

Slide 6 text

ErSanyamKhurana CuriousLearner The Diamond Problem💎

Slide 7

Slide 7 text

ErSanyamKhurana CuriousLearner The Diamond Problem💎 A B D C

Slide 8

Slide 8 text

ErSanyamKhurana CuriousLearner The Diamond Problem💎 A B D C

Slide 9

Slide 9 text

ErSanyamKhurana CuriousLearner The Diamond Problem💎 A B D C

Slide 10

Slide 10 text

ErSanyamKhurana CuriousLearner The Diamond Problem💎 A B D C Which method to call?

Slide 11

Slide 11 text

ErSanyamKhurana CuriousLearner The Solution ✅

Slide 12

Slide 12 text

ErSanyamKhurana CuriousLearner The Solution ✅ Method Resolution Order (MRO) de fi nes the class search path used by Python to search for the right attribute / method to use in classes having multi-inheritance.

Slide 13

Slide 13 text

ErSanyamKhurana CuriousLearner The Solution ✅ Method Resolution Order (MRO) de fi nes the class search path used by Python to search for the right attribute / method to use in classes having multi-inheritance.

Slide 14

Slide 14 text

ErSanyamKhurana CuriousLearner The Solution ✅

Slide 15

Slide 15 text

ErSanyamKhurana CuriousLearner The Solution ✅ Two Algorithms to fi nd MRO

Slide 16

Slide 16 text

ErSanyamKhurana CuriousLearner The Solution ✅ Old MRO Two Algorithms to fi nd MRO

Slide 17

Slide 17 text

ErSanyamKhurana CuriousLearner The Solution ✅ Old MRO New MRO Two Algorithms to fi nd MRO

Slide 18

Slide 18 text

ErSanyamKhurana CuriousLearner The Solution ✅ Old MRO New MRO DLR Algorithm (Depth-First Left to Right) Used prior to Python 2.2 Used in Old style classes Two Algorithms to fi nd MRO

Slide 19

Slide 19 text

ErSanyamKhurana CuriousLearner The Solution ✅ Old MRO New MRO DLR Algorithm (Depth-First Left to Right) Used prior to Python 2.2 Used in Old style classes C3 Linearization Algorithm (Searching the “Good Heads”) Introduced in Python 2.3 Used in New style classes Two Algorithms to fi nd MRO

Slide 20

Slide 20 text

ErSanyamKhurana CuriousLearner The Solution ✅ Old Style Classes Uses old MRO New Style Classes Base classes inherits from object Uses C3 Linearization Algorithm

Slide 21

Slide 21 text

ErSanyamKhurana CuriousLearner The Solution ✅ Old Style Classes Uses old MRO New Style Classes Base classes inherits from object Uses C3 Linearization Algorithm

Slide 22

Slide 22 text

ErSanyamKhurana CuriousLearner The Solution ✅ Old Style Classes Uses old MRO New Style Classes Base classes inherits from object Uses C3 Linearization Algorithm

Slide 23

Slide 23 text

ErSanyamKhurana CuriousLearner Gotcha 👌 If you’re using old-style classes in Python >= 2.3 and Python < 3, they’ll be using the DLR (Old-MRO Algorithm) Only new style classes on Python >= 2.3 will be using C3 Linearization algorithm

Slide 24

Slide 24 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem

Slide 25

Slide 25 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C

Slide 26

Slide 26 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C

Slide 27

Slide 27 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right

Slide 28

Slide 28 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right D Look in class D If not found in D, look at fi rst parent of D, i.e. B If not found in B, look at it’s parent, A If not found, look at B’s other parents If not found, then look at D’s other parents I.e. C

Slide 29

Slide 29 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right D B Look in class D If not found in D, look at fi rst parent of D, i.e. B If not found in B, look at it’s parent, A If not found, look at B’s other parents If not found, then look at D’s other parents I.e. C

Slide 30

Slide 30 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right D B A Look in class D If not found in D, look at fi rst parent of D, i.e. B If not found in B, look at it’s parent, A If not found, look at B’s other parents If not found, then look at D’s other parents I.e. C

Slide 31

Slide 31 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right D B A Look in class D If not found in D, look at fi rst parent of D, i.e. B If not found in B, look at it’s parent, A If not found, look at B’s other parents If not found, then look at D’s other parents I.e. C

Slide 32

Slide 32 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right D B A C Look in class D If not found in D, look at fi rst parent of D, i.e. B If not found in B, look at it’s parent, A If not found, look at B’s other parents If not found, then look at D’s other parents I.e. C

Slide 33

Slide 33 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right D B A C A Look in class D If not found in D, look at fi rst parent of D, i.e. B If not found in B, look at it’s parent, A If not found, look at B’s other parents If not found, then look at D’s other parents I.e. C

Slide 34

Slide 34 text

ErSanyamKhurana CuriousLearner Old MRO: DLR on Diamond problem A B D C Rule: Depth fi rst, left to right D B A C Look in class D If not found in D, look at fi rst parent of D, i.e. B If not found in B, look at it’s parent, A If not found, look at B’s other parents If not found, then look at D’s other parents I.e. C (Remove duplicates from end)

Slide 35

Slide 35 text

ErSanyamKhurana CuriousLearner History of MRO Samuele Pedroni wrote a mail in python-dev mailing list in 2002

Slide 36

Slide 36 text

ErSanyamKhurana CuriousLearner History of MRO

Slide 37

Slide 37 text

ErSanyamKhurana CuriousLearner History of MRO

Slide 38

Slide 38 text

ErSanyamKhurana CuriousLearner History of MRO

Slide 39

Slide 39 text

ErSanyamKhurana CuriousLearner Monotonicity Python 2.2 DLR algorithm is not monotonic i.e. not consistent

Slide 40

Slide 40 text

ErSanyamKhurana CuriousLearner Monotonicity A MRO is monotonic when the following is true: if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C.

Slide 41

Slide 41 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C.

Slide 42

Slide 42 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A MRO[B] = B A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C.

Slide 43

Slide 43 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A MRO[B] = B MRO[C] = C B A A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C.

Slide 44

Slide 44 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A MRO[B] = B MRO[C] = C B A MRO[D] = D B C A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C.

Slide 45

Slide 45 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A MRO[B] = B MRO[C] = C B A MRO[D] = D B C B A A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C.

Slide 46

Slide 46 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A MRO[B] = B MRO[C] = C B A MRO[D] = D B C B A A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C.

Slide 47

Slide 47 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A MRO[B] = B MRO[C] = C B A MRO[D] = D B C B A A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C. B is parent of C, Yet appears before C in MRO of D 😱

Slide 48

Slide 48 text

ErSanyamKhurana CuriousLearner Monotonicity MRO[A] = A MRO[B] = B MRO[C] = C B A MRO[D] = D B C B A A B D C if C1 precedes C2 in the linearization(class precedence list) of C, then C1 precedes C2 in the linearization of any subclass of C. Non-montonic 😱

Slide 49

Slide 49 text

ErSanyamKhurana CuriousLearner C3 Linearization Algorithm C3 superclass linearization is called C3 because it is consistent with three properties • a consistent extended precedence graph • preservation of local precedence order • fi tting a monotonicity criterion.

Slide 50

Slide 50 text

ErSanyamKhurana CuriousLearner C3 Linearization Algorithm The list of the ancestors of a class C, including the class itself, ordered from the nearest ancestor to the furthest, is called the class precedence list or the linearization of C.

Slide 51

Slide 51 text

ErSanyamKhurana CuriousLearner C3 Linearization Algorithm or MRO of class C The list of the ancestors of a class C, including the class itself, ordered from the nearest ancestor to the furthest, is called the class precedence list or the linearization of C.

Slide 52

Slide 52 text

ErSanyamKhurana CuriousLearner Notations for MRO of class C1 C1 C2 C3 …. CN

Slide 53

Slide 53 text

ErSanyamKhurana CuriousLearner Notations for MRO of class C1 C1 C2 C3 …. CN List of Classes

Slide 54

Slide 54 text

ErSanyamKhurana CuriousLearner Notations for MRO of class C1 C1 C2 C3 …. CN List of Classes Head

Slide 55

Slide 55 text

ErSanyamKhurana CuriousLearner Notations for MRO of class C1 C1 C2 C3 …. CN List of Classes Head Tail

Slide 56

Slide 56 text

ErSanyamKhurana CuriousLearner Notation for adding two lists C

Slide 57

Slide 57 text

ErSanyamKhurana CuriousLearner Notation for adding two lists C + (C1 C2 … CN)

Slide 58

Slide 58 text

ErSanyamKhurana CuriousLearner Notation for adding two lists C + (C1 C2 … CN) = C C1 C2 … CN

Slide 59

Slide 59 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? C B1 B2 BN ...

Slide 60

Slide 60 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? The linearization of C is the sum of C plus the merge of the linearizations of the parents and the list of the parents. C B1 B2 BN ...

Slide 61

Slide 61 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? The linearization of C is the sum of C plus the merge of the linearizations of the parents and the list of the parents. L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN) C B1 B2 BN ...

Slide 62

Slide 62 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? The linearization of C is the sum of C plus the merge of the linearizations of the parents and the list of the parents. L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN) C B1 B2 BN ...

Slide 63

Slide 63 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? The linearization of C is the sum of C plus the merge of the linearizations of the parents and the list of the parents. L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN) C B1 B2 BN ...

Slide 64

Slide 64 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? The linearization of C is the sum of C plus the merge of the linearizations of the parents and the list of the parents. L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN) C B1 B2 BN ...

Slide 65

Slide 65 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? The linearization of C is the sum of C plus the merge of the linearizations of the parents and the list of the parents. L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN) C B1 B2 BN ...

Slide 66

Slide 66 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN) Linearization of a class C inheriting from B1 … BN classes will be Class C itself, plus the merge of linearization of all it’s parent classes and the list of parent of classes

Slide 67

Slide 67 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? Linearization of a class C inheriting from B1 … BN classes will be Class C itself, plus the merge of linearization of all it’s parent classes and the list of parent of classes If C is object class, then L[object] = object L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN)

Slide 68

Slide 68 text

ErSanyamKhurana CuriousLearner How to fi nd MRO in C3 algo? L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) How to fi nd merge? L[C(B1 ... BN)] = C + merge(L[B1], ... L[BN], B1 ... BN)

Slide 69

Slide 69 text

ErSanyamKhurana CuriousLearner How to fi nd merge? L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) • Take the head of the fi rst list • If the head is not in the tail of any other list, add it to linearization of C and remove it from the lists in merge • Otherwise look at the head of next list to see if it is a good head • Repeat until all classes removed or it is impossible to fi nd good head • Python2.3 will raise an exception in case there are no good heads and will not let you create a class with such a heirarchy

Slide 70

Slide 70 text

ErSanyamKhurana CuriousLearner How to fi nd merge? L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) • Take the head of the fi rst list • If the head is not in the tail of any other list, add it to linearization of C and remove it from the lists in merge • Otherwise look at the head of next list to see if it is a good head • Repeat until all classes removed or it is impossible to fi nd good head • Python2.3 will raise an exception in case there are no good heads and will not let you create a class with such a heirarchy

Slide 71

Slide 71 text

ErSanyamKhurana CuriousLearner How to fi nd merge? L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) • Take the head of the fi rst list • If the head is not in the tail of any other list, add it to linearization of C and remove it from the lists in merge • Otherwise look at the head of next list to see if it is a good head • Repeat until all classes removed or it is impossible to fi nd good head • Python2.3 will raise an exception in case there are no good heads and will not let you create a class with such a heirarchy

Slide 72

Slide 72 text

ErSanyamKhurana CuriousLearner How to fi nd merge? L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) • Take the head of the fi rst list • If the head is not in the tail of any other list, add it to linearization of C and remove it from the lists in merge • Otherwise look at the head of next list to see if it is a good head • Repeat until all classes removed or it is impossible to fi nd good head • Python2.3 will raise an exception in case there are no good heads and will not let you create a class with such a heirarchy

Slide 73

Slide 73 text

ErSanyamKhurana CuriousLearner How to fi nd merge? L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) • Take the head of the fi rst list • If the head is not in the tail of any other list, add it to linearization of C and remove it from the lists in merge • Otherwise look at the head of next list to see if it is a good head • Repeat until all classes removed or it is impossible to fi nd good head • Python2.3 will raise an exception in case there are no good heads and will not let you create a class with such a hierarchy

Slide 74

Slide 74 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[A] = A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN)

Slide 75

Slide 75 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[B] = B + merge(L[A], A) L[B] = B + merge(A, A) L[B] = B A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A

Slide 76

Slide 76 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[B] = B + merge(L[A], A) L[B] = B + merge(A, A) L[B] = B A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A

Slide 77

Slide 77 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[B] = B + merge(L[A], A) L[B] = B + merge(A, A) L[B] = B A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A No Tail here!

Slide 78

Slide 78 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[B] = B + merge(L[A], A) L[B] = B + merge(A, A) L[B] = B A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A

Slide 79

Slide 79 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[C] = C + merge(L[A], A) L[C] = C + merge(A, A) L[C] = C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A

Slide 80

Slide 80 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[C] = C + merge(L[A], A) L[C] = C + merge(A, A) L[C] = C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A

Slide 81

Slide 81 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[C] = C + merge(L[A], A) L[C] = C + merge(A, A) L[C] = C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A

Slide 82

Slide 82 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[C] = C + merge(L[A], A) L[C] = C + merge(A, A) L[C] = C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 83

Slide 83 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 84

Slide 84 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 85

Slide 85 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 86

Slide 86 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 87

Slide 87 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 88

Slide 88 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 89

Slide 89 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 90

Slide 90 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A A is in tail of list, we’ll try to search for next “good” head

Slide 91

Slide 91 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 92

Slide 92 text

ErSanyamKhurana CuriousLearner L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A C3 on Diamond Problem A B D C L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A No tail here!

Slide 93

Slide 93 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 94

Slide 94 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B A, C A, B C) L[D] = D B + merge(A, C A, C) L[D] = D B C + merge(A, A) L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 95

Slide 95 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A

Slide 96

Slide 96 text

ErSanyamKhurana CuriousLearner C3 on Diamond Problem A B D C L[D] = D B C A L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN) L[A] = A L[B] = B A L[C] = C A Note that C3 MRO always traverses parents at same level before going through their ancestors further up the hierarchy

Slide 97

Slide 97 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[A] = A L[B] = B A B D C

Slide 98

Slide 98 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[C] = C + merge(L[B], L[A], BA) L[C] = C + merge(B, A, BA) L[C] = C B + merge(A, A) L[C] = C B A A B D C L[A] = A L[B] = B

Slide 99

Slide 99 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[C] = C + merge(L[B], L[A], BA) L[C] = C + merge(B, A, BA) L[C] = C B + merge(A, A) L[C] = C B A A B D C L[A] = A L[B] = B

Slide 100

Slide 100 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[C] = C + merge(L[B], L[A], BA) L[C] = C + merge(B, A, BA) L[C] = C B + merge(A, A) L[C] = C B A A B D C L[A] = A L[B] = B

Slide 101

Slide 101 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[C] = C + merge(L[B], L[A], BA) L[C] = C + merge(B, A, BA) L[C] = C B + merge(A, A) L[C] = C B A A B D C L[A] = A L[B] = B

Slide 102

Slide 102 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A

Slide 103

Slide 103 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A

Slide 104

Slide 104 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A B appears in tail of other list, So, it’s not a good head Let’s move to head of the next list

Slide 105

Slide 105 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A

Slide 106

Slide 106 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A C appears in tail of other list, So, it’s not a good head We’ve exhausted the lists

Slide 107

Slide 107 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A No more good heads 😱

Slide 108

Slide 108 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A Python >= 2.3 raises TypeError 😱

Slide 109

Slide 109 text

ErSanyamKhurana CuriousLearner C3 on Example 2 L[D] = D + merge(L[B], L[C], B C) L[D] = D + merge(B, C B A, B C) A B D C L[A] = A L[B] = B L[C] = C B A Python > 2.3 raises TypeError 😱

Slide 110

Slide 110 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄

Slide 111

Slide 111 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄 A B D C

Slide 112

Slide 112 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄 A B D C class.__mro__¶ This attribute is a tuple of classes that are considered when looking for base classes during method resolution.

Slide 113

Slide 113 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄 A B D C class.__mro__¶ This attribute is a tuple of classes that are considered when looking for base classes during method resolution.

Slide 114

Slide 114 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄 A B D C class.__bases__ The tuple of base classes of a class object.

Slide 115

Slide 115 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄 A B D C class.__bases__ The tuple of base classes of a class object.

Slide 116

Slide 116 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄 A B D C class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in __mro__.

Slide 117

Slide 117 text

ErSanyamKhurana CuriousLearner Python Special Class Attributes 😄 A B D C class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in __mro__.

Slide 118

Slide 118 text

ErSanyamKhurana CuriousLearner MRO in Django 💘 Forms Views Models

Slide 119

Slide 119 text

ErSanyamKhurana CuriousLearner MRO in Django Models 💘 Multi-table inheritance

Slide 120

Slide 120 text

ErSanyamKhurana CuriousLearner Gotcha 👌 Model fi elds inherited from multiple abstract parent models are resolved in a strict depth- fi rst order as opposed to the Python’s MRO.

Slide 121

Slide 121 text

ErSanyamKhurana CuriousLearner Recap • What is MRO • Old MRO / C3 Linearization (New MRO) • History of MRO • Examples on new MRO • TypeError on inconsistent C3 Linearization • Special class attributes related to MRO • MRO in Django

Slide 122

Slide 122 text

ErSanyamKhurana CuriousLearner Reference • Absolutely amazing explanation on MRO: https://www.python.org/ download/releases/2.3/mro/ • https://en.wikipedia.org/wiki/C3_linearization

Slide 123

Slide 123 text

ErSanyamKhurana CuriousLearner Many Thanks! Reach out to me on ErSanyamKhurana CuriousLearner 📨 [email protected]