Let's talk about something that sounds quite complicated.
It's called MRO or method resolution order.
What does it mean?
Well, I have an example here for you.
Let's say we have a class of A class of B that inherits from class of A does absolutely nothing.
And then we also have class C that inherits again from A and then finally a Class D, then inherits
from B and C.
So we have two classes that do absolutely nothing, and then a class A and Class C that have the same
class object attribute.
And I've actually drawn out how this relationship looks like.
You see that D has multiple inheritance from B and C and B and C inherit from A.
And Amro or method resolution order is a rule that Python follows to determine when you run a method
which one to run when you have such complicated inheritance structure.
Amro is a rule that says, Hey, do this, do this, and then do that.
And that's the method that you should run.
Let me show you what I mean.
Let's say I wanted to do print dx dot num.
What do you think will happen here?
Let me move this out of the way or comment this out.
If I run this, can you guess what's going to happen?
If I click Run.
I get one?
Is that what you expected?
Let's look through this.
DD inherits from B and C.
And then those inherit from me.
So we get one because the MRO, the method resolution order is that it doesn't go D, b, a.
Instead it goes d.
B.
C.
A.
Hmm.
Let's think about this a little bit more.
And Miro is simply saying what's first in line if things are common between classes, methods or variables
or attributes?
What should I pick?
And there's actually a good way for us to check this if I do, Miro, and then run it like this.
Fake lake run.
Look at that.
This is the order or the Maro of this class.
It says, Hey, I'm going to check D first.
If you call, let's say D dot name or d dot num.
I'm going to check main dot D first.
So I'm going to check this class, then I'm going to check main dot B, so I'm going to check B and
then I'm going to check C.
And then I'm going to check a.
And then finally, I'm going to check the base class object again.
We've seen that before.
It's the base class that all objects in Python inherit from.
So that's the order that it checks for.
Now, if we pass all of these.
And I click Run.
I'll get an air.
D has no attribute numb because it went through all this path, even checked the base object and never
found num.
If I did something like SW tr.
If I click Run.
This works because it goes all the way through the inheritance chain and then gets to the base object.
And the base object actually has this so it doesn't error out.
And yep, you can use the dunder string.
Now.
I found a great example here to show you why inheritances can be confusing and why things like Amro
you might want to avoid or at least be conscious of.
So let's copy this code.
And let's just remove all of this.
And we're going to play a little game.
Now this syntax.
Looks like this.
So this is the inheritance model of what we just created.
Looks pretty confusing, doesn't it?
So if we do em here.
And we check the MRO or method resolution order of M.
What do you think the order is going to be?
Pause the video here and try to guess what the order will be.
Ready.
Let's do m dot amro.
And by the way, another way of doing this is to use the dunder amro and let's print this.
All right, let's check this.
So the first thing we have is em.
All right?
We check him, and then we check B, we check B, all right.
And then we check A.
So we check A here, and then we check x x over here, then y.
Then Z, then the class object.
Is that what you guessed?
Let's look at the code.
Why this order?
Well, the order is because of the way that we're passing in the parameters you see be is passed before
A and Z.
So that means the order of Maro is going to be be then a.
But hold on after Bina.
What was that?
Well, after B and A, we have class x.
Why did X come before Z?
So instead of going to Z, let's make this a little bit bigger.
Instead of going to what we would have thought would be Z, it goes to X, then Y, then Z, then the
object.
This is because of the algorithm that they use for doing Miro, which is called depth for search.
Now this is beyond the scope of the course, but as you can see, it's quite a bit confusing.
And as a matter of fact, Python actually changed the MISO rules from what we have previously in younger
versions of Python.
So this is another one of those things that got updated.
But the reason I'm showing you this is because you're never going to get tested on this stuff.
If you're writing code where the inheritance looks like this, I argue that you're writing bad code
because this is overly complicated.
We haven't even written any code actually inside of these classes, and it's already confusing.
So Amro is there as a way to define what order you're going to inherit it.
And you can always use the MRO function or DUNDAR to check this.
But the reason I brought it here is not just trying to confuse you.
The reason is that it exists.
However, you should not structure your code in this way.
This doesn't mean that this doesn't happen in the wild.
Yes, it does.
But at least you have a way of checking and maybe debugging your code when you see a problem like this.
I'll leave a resource for you about Maro.
If you're really curious and you really want to understand how this algorithm works and how this order
is actually determined.
But the key takeaway is that this is the way that Python works to determine, hey, when you call a
method or an attribute.
This is how I'm going to look up the hierarchy of inheritances.
All right.
If your brain hurts, don't worry.
So does mine.
Take a break and I'll see you in the next video.
Bye bye.