Detailed Notes
This episode continues a series of tutorials on classes in Python. Multiple inheritance examples are covered.
Useful Links: https://docs.python.org/3/tutorial/classes.html https://www.python.org/download/releases/2.3/mro/
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] well hello and welcome back we are continuing our season of or our series of python certification tutorials this episode we are going to start into multiple inheritance we talked about classes before and we talked about them as basics about it the properties and collect basically a collection of methods and properties now we can inherit so we can extend classes and we've seen that before we saw that in our if i pop over here we saw this in our little inheritance example now one of the features of python is it allows for multiple inheritance that's what we're going to start playing around with today now in these examples i actually went ahead and threw a few things together and so we have three classes base one base two and base three and oh i expected a couple so we get some blank lines here just so it doesn't complain oh and that needs to be that's not going to be static so each of these i'm keeping it moderately simple what i'm trying to do here is i've got these three classes and each one one of them has sort of the same thing it's just to help us see how multiple inherited multiple parents work with each one we've got base one two and three and it's just gonna say it's got a value by default it's gonna be i am base whatever it is basically my name it's going to be i've got some little bit of output in case a constructor was called and then i've got this display method so then i come down to my class and this is how we do multiple inheritance it's just a comma separated list before if i wanted to inherit just from base 1 then that's how it would look then i'm off and running oh and it doesn't like that but that's okay i'll do it well i will i'll call it this way so it cleans that up there we go so it's not complaining but if i wanted to do multiple inheritance then i can simply add on additional classes and so we're going to start with something simple and so i'm going to do um let's just do it here i'm just going to do it my class equals new my class oh it's not new it's my class and then let's just do print well let's just call because i think i already did the prints yes and then i'm just going to do my class dot display so we're going to just this is a multiple inheritance base one two and three in that order and then we've got some additional stuff but really what matters right now is we're gonna look at the constructors and then we're gonna look at uh we're gonna call display and see what happens so if i go in here and i do it's called multiple inherits so in this case since it's just base one two and three i can see here that it calls the first constructor it doesn't call any others and when i do display it's calling it on display one reason it does this is because it finds the first that matches so i could come in here and it didn't so let's and let me show you that so let me change it let's reverse the order so if i do three two one then it's going to call base three if i do two three one then it calls the second and so this is where it can get can get confusing rather quickly is that what it does when you have multiple inheritance is it walks through these classes and instead of doing some sort of i don't know combination or something like that what it does here is it's going to come in and it's going to say base 1 does it have i hear when i call this when i call display it's going to say does base 1 have a display yes it does and so i'm going to call that now if i do display 2 and display 3 which is normally what you would have you're probably not going to have you have to watch out for it you're probably not going to have collisions across multiples or so let's do that first so i can also because i've done these i can call now display two and display three so all of those are available if you've dealt with uh interfaces that shows up more often than not there's several times that interfaces is used for multiple inheritance much like this so if base one was actually let's say printing base 2 was actually file manipulation and base 3 was i don't know calculations you probably wouldn't have any override overlapping those so you would be able to pull in all of those methods in this case now let's go look at it and we'll see here that display one two and three were all called because i go to uh display two it's gonna go to base one it's gonna say oh i don't see it so that's gonna go base two it's gonna say yes i found it here and so now we're off and running i can use that one now what i could do now this is where it gets a little tricky so let's say i go back here and let's say i do leave those as three well uh yeah let's just leave those as three i know let's do it i'm gonna do it this way so so what happened this is what happens when you do it on the fly so let's say that each of them has a display and i'm gonna do it like this i'll just say [Music] and base three display call like that now what i can do is i can come down in here and i can call if i can call up so let's do this first let's do display and let's look at what super gives us so if i do print super uh and now i'm gonna call i'm just gonna leave it simple like here let's take a look at that so super my class just returns a my class object but let's do uh let's do super dot display and actually i don't need to print that and he's still going to call the first one because he's just going to grab that if i call display two then it's going to call display too so super's actually giving me those three so i can actually call uh super does uh let's see let's do it this way so i'm gonna do super display and then i'm gonna call my class display called i do that then i'm going to see that it's called it but it also called the super [Music] and let's try this for let's see if we get a list back and there it's going to give us a problem because the super is not giving us those separate objects those separate classes it's it's giving us this this amalgamation of those three so while we handle this is we can come in and let's we can actually call up the chain so let me do this so if we want the ins the constructor we can do that here um for me whilst i type a second so i'm going to call each of the three up the chain i'm going to play around with a little bit um let's do we steal the init here oh [Music] and then [Music] okay so what we can do is although we don't necessarily see that in these above but what we can do is we can tell it with a super we can say what is the super that we want to use and so in this case we can say specifically i want to use the like in the init and the constructor i'm going to use a constructor for base 1 then i'm going to call 2 and then i'm going to call 3 and i could actually reverse these so now we're going to see something a little funky because what we're going to do is we're going to see this is actually overwritten and now it's going to force an order on the init but we're going to see a different order when we do the display oops we do the display and so again we're actually calling into we're manually calling into a specific class so let's see how that looks and so it's going to blow up on us here but now we would see is so first we see uh let's see where it blew up on line 63 did i miss something base 3 should have a display what did i miss wait it's died in 68 what it called display and died in 63 i did not so this is pretty interesting this something i had not run into but uh let's see oh let's do i am let's just do it this way because i didn't send that oops that was a mistake uh vowel one equals i am my class oh and it doesn't like that because it uh let's do self dot name equals about one and okay so let's walk through this one a little bit and so from the top uh let's just do the constructor first so looking just at the constructor which if this works should be three two one [Music] and we'll see here three two and then it calls that so it doesn't call this third one it only calls the first two so call it now let's see if we just reverse this just to be safe now it calls two and then one and then it calls the init now if i don't call oh cause it yeah because it's definitely doing it overriding it calls my class init so the interesting thing is it does not call the constructor the third time this may be a little bug that we found and you're not honestly going to do this very often but just so you know yep so it calls one and two oh but that's calling that up the chain it's not actually doing oh because it's doing it oh well let's see if i do say you leave it three two one now it's just doing that it's not calling anything up the chain whereas before we all uh it only called the first that's right and so now let's let's take a look at the display just because now here in line 55 it says that it doesn't have so it's not allowing us to call up to that level let's say if we called three oh i think we need to do it like we did here so let's try this well shoot there's a couple ways we can do it so let's say i'm gonna call this one equals that and then let's try o1 dot display and it's still going to give me that it doesn't find that so that's super object so let's try this and it's giving me a nun so it's not returning it back so when i call oh when i call super it's not actually it's right it's not returning a class it's just calling up and doing something so i could do so it would have to do like a display but it's not going to recognize that so you really get sort of stuck calling up the chain if you want to do if you want to call up the chain if you want to try to somehow break it that is something that's that's a challenge with python but i want to make sure that you sort of looked at that is that there's not a while you can do it to some extent you can do it in an initializer but you have to make sure it has that that constructor otherwise it's just going to go through each of these as we saw um let's see if i just go back to this guy oh well we see this anyway uh let's clear that up uh saved yep so let's just clear that out just to go look back at that is notice that we are only calling the first constructor because constructors exist everywhere and so i'm only going to get the actual constructor built off the first class and that may or may not matter otherwise i'm going to have to do is i would have to go in here and manually call separate pieces so i could come in and well this would be a more complex thing if i wanted to really get into it if i wanted to set some values on each level of these then i would have to do so um so if i did [Music] let's see what happens if i do val2 i don't think that's going to matter for me i still get the same thing because it grabs the first one that matches and i'm off and running and so even if i do my class test it's still calling that base three which is the first one it's going to call so there's a lot of complexity which is why some places don't even deal with it sometimes don't deal with multiple inheritance python does but you have to treat it with a lot of forethought there is not necessarily an easy way to uh to sort of meld stuff and just realize that the probably the most important thing to realize is with multiple inheritance it starts with it goes in order that you see in this declaration here and then you can start moving stuff around i think that's another it doesn't go my understanding is it doesn't go terribly deep on the certification you're not going to have to be to you know be beat up with those kinds of questions but understand that order matters and then that probably will be you know get you through it and know that especially if you have classes that are that have no collisions then it would make a lot of sense but then you're just gonna have to in that case you would you would want to go in and do some work based on the uh the initializer and then it does seem to have some limits as far as calling the the supers so uh definitely your mileage may vary on these i don't think like i said i don't think that's going to run you're going to run into that as much in the certification tests as you are in the real world and i think now it's time for us to uh un knot our brain a little bit and we will come back next time and work on some other class related stuff but until then go out there and have yourself a great day a great week and we'll talk to you next time you
Transcript Segments
[Music]
well hello and welcome back
we are continuing our season of or
our series of python certification
tutorials this episode we are going to
start into
multiple inheritance we talked about
classes before
and we talked about them as
basics about it the properties and
collect
basically a collection of methods and
properties now
we can inherit so we can extend classes
and we've seen that before we saw that
in our if i pop
over here we saw this in our little
inheritance example
now one of the features of python is it
allows for
multiple inheritance that's what we're
going to start playing around with today
now in these examples i actually went
ahead and threw a few things together
and so we have three classes base one
base two and base three
and oh i expected a couple so we get
some blank lines
here just so it doesn't complain
oh and that needs to be that's not going
to be
static so each of these i'm keeping it
moderately simple what i'm trying to do
here
is i've got these three
classes and each one one of them has
sort of the same thing
it's just to help us see how multiple
inherited multiple parents work
with each one we've got base one two and
three and it's just gonna say it's got a
value by default it's gonna be
i am base whatever it is basically my
name
it's going to be i've got some little
bit of output in case a constructor was
called
and then i've got this display method so
then i come down to my class and this is
how we do multiple inheritance it's just
a comma separated list
before if i wanted to inherit just from
base 1 then that's how it would look
then i'm off and running
oh and it doesn't like that but that's
okay i'll do it well i will i'll call it
this way so it cleans that up
there we go so it's not complaining but
if i wanted to
do multiple inheritance then i can
simply add on
additional classes and so we're going to
start with something simple
and so i'm going to do um
let's just do it here i'm just going to
do it
my class equals new
my class
oh it's not new it's my class
and then let's just do print
well let's just call because i think i
already did the prints yes and then i'm
just going to do
my class dot display
so we're going to just this is a
multiple inheritance base one two and
three
in that order and then we've got some
additional stuff but really what matters
right now is we're gonna look at the
constructors
and then we're gonna look at uh we're
gonna call display
and see what happens so if i go in
here and i do
it's called multiple inherits so in this
case
since it's just base one two and three i
can see here that it calls
the first constructor it doesn't call
any others
and when i do display it's calling it on
display one
reason it does this is because it finds
the first
that matches so i could come in here
and it didn't so let's and let me show
you that so let me change it let's
reverse the order
so if i do three two one
then it's going to call base three
if i do two three one
then it calls the second and so this is
where it can get
can get confusing rather quickly
is that what it does when you have
multiple inheritance is it walks through
these classes
and instead of doing some sort of i
don't know combination or something like
that
what it does here is it's going to come
in and it's going to say
base 1 does it have i hear when i call
this when i call display
it's going to say does base 1 have a
display
yes it does and so i'm going to call
that
now if i do display 2 and display 3
which is normally what you would have
you're probably not going to have
you have to watch out for it you're
probably not going to have collisions
across
multiples or so let's do that first so i
can also
because i've done these
i can call now display two
and display three so all of those are
available if you've dealt with
uh interfaces that shows up more often
than not there's several times that
interfaces is used for multiple
inheritance
much like this so if base one was
actually let's say printing
base 2 was actually file manipulation
and base 3 was
i don't know calculations you probably
wouldn't have any override
overlapping those so you would be able
to pull in
all of those methods
in this case now let's go look at it
and we'll see here that display one two
and three were all called
because i go to uh display two
it's gonna go to base one it's gonna say
oh i don't see it so that's gonna go
base two it's gonna say yes
i found it here and so now we're off and
running i can use that one
now what i could do now this is where it
gets a little tricky so let's say i go
back here
and let's say i do leave those as three
well
uh yeah let's just leave those as three
i
know let's do it i'm gonna do it this
way so so what happened
this is what happens when you do it on
the fly so let's say that
each of them has a display
and i'm gonna do it like this i'll just
say
[Music]
and base three display call like that
now what i can do is i can come down in
here
and i can call
if i can call up
so let's do this first let's do display
and let's look at what super gives us
so if i do print super
uh and now i'm gonna call i'm just gonna
leave it simple like here
let's take a look at that
so super my class just returns a my
class object but let's do
uh let's do super dot
display
and actually i don't need to print that
and he's still going to call the first
one because he's just going to
grab that if i call display two
then it's going to call display too so
super's actually giving me
those three so i can actually call
uh super does uh let's see
let's do it this way so i'm gonna do
super display
and then i'm gonna call
my class display called i do that
then i'm going to see that it's called
it but it also called the super
[Music]
and let's try this for
let's see if we get a list back
and there it's going to give us a
problem because the super is not giving
us those separate objects those separate
classes
it's it's giving us this
this amalgamation of those three
so while we handle this is we can come
in and let's we can actually call
up the chain so let me do this
so if we want the ins the constructor
we can do that here
um for me whilst i type a second
so i'm going to call each of the three
up the chain i'm going to play around
with a little bit
um let's do
we steal the init here oh
[Music]
and then
[Music]
okay so
what we can do is although we don't
necessarily see that
in these above but what we can do
is we can tell it with a super we can
say
what is the super that we want to use
and so in this case we can say
specifically i want to use the
like in the init and the constructor i'm
going to use a constructor for
base 1 then i'm going to call 2 and then
i'm going to call 3
and i could actually
reverse these so now we're going to see
something a little funky because what
we're going to do is we're going to see
this is actually overwritten and now
it's going to force an order on the init
but we're going to see a different order
when we do the display oops
we do the display and so again we're
actually
calling into we're manually
calling into a specific class
so let's see how that looks
and so it's going to blow up on us here
but now we would see is so first we see
uh let's see where it blew up on line 63
did i miss something
base 3 should have a display what did i
miss
wait it's died
in 68 what it called display and died in
63
i did not so this is pretty interesting
this something i had not run into
but uh let's see oh
let's do i am let's just do it this way
because i didn't send that oops that was
a mistake
uh vowel one equals i am
my class
oh and it doesn't like that because it
uh let's do
self dot name equals
about one
and okay
so let's walk through this one a little
bit
and so from the top uh let's just do the
constructor
first
so looking just at the constructor which
if this works should be three two one
[Music]
and we'll see here three two and then it
calls that so it doesn't call
this third one it only calls the first
two
so call it now let's see if we just
reverse this just to be safe
now it calls two and then one
and then it calls the init now if i
don't call oh cause it yeah because it's
definitely doing it overriding it calls
my class init
so the interesting thing is it does not
call the constructor the third time
this may be a little bug that we found
and you're not honestly going to do this
very often but just so you know yep so
it calls one and two
oh
but that's calling that up the chain
it's not actually doing oh
because it's doing it oh well let's see
if i do say you leave it three two one
now it's just doing that it's not
calling anything up the chain
whereas before we all
uh it only called the first that's right
and so now let's let's take a look at
the display just because
now here in line 55 it says that it
doesn't have
so it's not allowing us to call up
to that level let's say if we called
three oh i think we need to do it like
we did here so let's try this
well shoot there's a couple ways we can
do it so let's say i'm gonna call this
one
equals that
and then let's try o1 dot display
and it's still going to give me that it
doesn't find that so that's super object
so let's try this
and it's giving me a nun so it's not
returning it back so when i call
oh when i call super it's not actually
it's right it's not returning a class
it's just calling up and doing something
so i could do
so it would have to do like a display
but it's not going to recognize that
so you really get sort of stuck calling
up the chain
if you want to do if you want to call up
the chain
if you want to try to somehow break it
that is something that's
that's a challenge with python but i
want to make sure that you sort of
looked at that is that there's not a
while you can
do it to some extent you can do it in an
initializer but you have to make sure it
has
that that constructor otherwise
it's just going to go through each of
these
as we saw um let's see if i just go back
to
this guy oh well we see this anyway uh
let's clear that up
uh saved yep so let's just clear that
out just to go look back at that
is notice that we are only calling the
first constructor because constructors
exist
everywhere and so i'm only going to get
the actual constructor built off the
first class
and that may or may not matter
otherwise i'm going to have to do is i
would have to go in here
and manually call separate
pieces so i could come in
and well this would be a more complex
thing if i wanted to really get into it
if i wanted to set some values
on each level of these then i would have
to do so
um so if i did
[Music]
let's see what happens if i do val2 i
don't think that's going to matter for
me
i still get the same thing because it
grabs the first one that matches
and i'm off and running
and so even if i do my class
test
it's still calling that base three which
is the first one it's going to call
so there's a lot of complexity which is
why some places don't even deal with it
sometimes don't deal with
multiple inheritance python does but you
have to treat it
with a lot of forethought there is not
necessarily an
easy way to uh
to sort of meld stuff and just realize
that the probably the most important
thing to realize is with multiple
inheritance it starts with it goes in
order
that you see in this declaration here
and then you can
start moving stuff around i think that's
another it doesn't go
my understanding is it doesn't go
terribly deep on the certification
you're not going to have to be to
you know be beat up with those kinds of
questions
but understand that order matters and
then
that probably will be you know get you
through it and know that
especially if you have classes that are
that have no collisions
then it would make a lot of sense but
then you're just gonna have to
in that case you would you would want to
go in and do some work
based on the uh the initializer and then
it does seem to have some limits as far
as calling the
the supers so uh definitely your mileage
may vary on these i don't think like i
said
i don't think that's going to run you're
going to run into that as much in the
certification tests as you are in the
real world
and i think now it's time for us to uh
un
knot our brain a little bit and we will
come back next time
and work on some other class related
stuff
but until then go out there and have
yourself a great day a great week
and we'll talk to you next time
you