📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Multiple Inheritance

2021-08-17 •Youtube

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
0.46

[Music]

26.16

well hello and welcome back

27.76

we are continuing our season of or

30.8

our series of python certification

34.239

tutorials this episode we are going to

36.8

start into

37.84

multiple inheritance we talked about

40.559

classes before

42.079

and we talked about them as

45.44

basics about it the properties and

48.32

collect

48.8

basically a collection of methods and

50.079

properties now

52.16

we can inherit so we can extend classes

54.8

and we've seen that before we saw that

56.559

in our if i pop

58.559

over here we saw this in our little

60

inheritance example

61.68

now one of the features of python is it

63.92

allows for

64.96

multiple inheritance that's what we're

67.76

going to start playing around with today

69.84

now in these examples i actually went

71.439

ahead and threw a few things together

73.92

and so we have three classes base one

78.08

base two and base three

82.799

and oh i expected a couple so we get

84.88

some blank lines

86

here just so it doesn't complain

91.6

oh and that needs to be that's not going

94

to be

96.24

static so each of these i'm keeping it

100.72

moderately simple what i'm trying to do

104.56

here

105.2

is i've got these three

109.84

classes and each one one of them has

111.68

sort of the same thing

113.36

it's just to help us see how multiple

116.799

inherited multiple parents work

119.84

with each one we've got base one two and

121.439

three and it's just gonna say it's got a

122.96

value by default it's gonna be

124.88

i am base whatever it is basically my

126.84

name

128.64

it's going to be i've got some little

130.8

bit of output in case a constructor was

132.64

called

133.68

and then i've got this display method so

136.959

then i come down to my class and this is

140

how we do multiple inheritance it's just

141.76

a comma separated list

143.36

before if i wanted to inherit just from

146.8

base 1 then that's how it would look

150

then i'm off and running

156.16

oh and it doesn't like that but that's

157.68

okay i'll do it well i will i'll call it

160.08

this way so it cleans that up

161.44

there we go so it's not complaining but

163.599

if i wanted to

165.12

do multiple inheritance then i can

166.8

simply add on

168.48

additional classes and so we're going to

171.84

start with something simple

173.92

and so i'm going to do um

178.8

let's just do it here i'm just going to

180.4

do it

182.4

my class equals new

187.28

my class

191.44

oh it's not new it's my class

195.04

and then let's just do print

198.319

well let's just call because i think i

199.76

already did the prints yes and then i'm

201.12

just going to do

202.64

my class dot display

208.56

so we're going to just this is a

210.56

multiple inheritance base one two and

212.159

three

212.72

in that order and then we've got some

214.799

additional stuff but really what matters

216.239

right now is we're gonna look at the

217.28

constructors

218.72

and then we're gonna look at uh we're

220.159

gonna call display

222.4

and see what happens so if i go in

225.76

here and i do

229.519

it's called multiple inherits so in this

232.72

case

234.64

since it's just base one two and three i

237.68

can see here that it calls

240.319

the first constructor it doesn't call

243.439

any others

245.2

and when i do display it's calling it on

248.319

display one

250.48

reason it does this is because it finds

252.4

the first

253.84

that matches so i could come in here

259.519

and it didn't so let's and let me show

261.919

you that so let me change it let's

263.44

reverse the order

264.479

so if i do three two one

269.28

then it's going to call base three

273.68

if i do two three one

280.639

then it calls the second and so this is

283.12

where it can get

284.32

can get confusing rather quickly

287.919

is that what it does when you have

289.84

multiple inheritance is it walks through

291.759

these classes

293.199

and instead of doing some sort of i

295.68

don't know combination or something like

297.12

that

298.479

what it does here is it's going to come

300.639

in and it's going to say

301.84

base 1 does it have i hear when i call

304.16

this when i call display

305.84

it's going to say does base 1 have a

307.919

display

308.88

yes it does and so i'm going to call

311.759

that

312.24

now if i do display 2 and display 3

316.639

which is normally what you would have

318

you're probably not going to have

319.84

you have to watch out for it you're

320.88

probably not going to have collisions

322.8

across

323.36

multiples or so let's do that first so i

327.28

can also

328.24

because i've done these

331.52

i can call now display two

335.199

and display three so all of those are

336.96

available if you've dealt with

339.199

uh interfaces that shows up more often

342.24

than not there's several times that

344

interfaces is used for multiple

345.919

inheritance

347.039

much like this so if base one was

350.639

actually let's say printing

354.639

base 2 was actually file manipulation

357.759

and base 3 was

359.759

i don't know calculations you probably

362

wouldn't have any override

363.68

overlapping those so you would be able

365.6

to pull in

367.12

all of those methods

372.08

in this case now let's go look at it

376.479

and we'll see here that display one two

378.8

and three were all called

380.56

because i go to uh display two

384.08

it's gonna go to base one it's gonna say

385.6

oh i don't see it so that's gonna go

387.28

base two it's gonna say yes

389.199

i found it here and so now we're off and

391.68

running i can use that one

395.12

now what i could do now this is where it

397.919

gets a little tricky so let's say i go

399.36

back here

401.84

and let's say i do leave those as three

404.08

well

406.08

uh yeah let's just leave those as three

408.8

i

409.12

know let's do it i'm gonna do it this

410.319

way so so what happened

412.72

this is what happens when you do it on

414.24

the fly so let's say that

416.8

each of them has a display

424.319

and i'm gonna do it like this i'll just

426.16

say

430.38

[Music]

433.599

and base three display call like that

437.68

now what i can do is i can come down in

439.84

here

445.12

and i can call

450.24

if i can call up

456.319

so let's do this first let's do display

459.599

and let's look at what super gives us

461.52

so if i do print super

466.72

uh and now i'm gonna call i'm just gonna

468.879

leave it simple like here

470.08

let's take a look at that

476.8

so super my class just returns a my

479.039

class object but let's do

480.879

uh let's do super dot

484.4

display

490.72

and actually i don't need to print that

497.44

and he's still going to call the first

499.44

one because he's just going to

500.639

grab that if i call display two

507.28

then it's going to call display too so

508.879

super's actually giving me

510.96

those three so i can actually call

514.32

uh super does uh let's see

517.44

let's do it this way so i'm gonna do

518.56

super display

520.56

and then i'm gonna call

527.04

my class display called i do that

534.64

then i'm going to see that it's called

536

it but it also called the super

541.73

[Music]

543.68

and let's try this for

551.36

let's see if we get a list back

559.279

and there it's going to give us a

560.32

problem because the super is not giving

562.72

us those separate objects those separate

566.48

classes

568

it's it's giving us this

571.92

this amalgamation of those three

577.279

so while we handle this is we can come

580.32

in and let's we can actually call

584.48

up the chain so let me do this

590.399

so if we want the ins the constructor

594.16

we can do that here

597.76

um for me whilst i type a second

602.079

so i'm going to call each of the three

603.68

up the chain i'm going to play around

604.959

with a little bit

606.8

um let's do

613.44

we steal the init here oh

622.17

[Music]

625.12

and then

635.38

[Music]

637.92

okay so

641.12

what we can do is although we don't

643.279

necessarily see that

644.56

in these above but what we can do

647.6

is we can tell it with a super we can

649.76

say

650.64

what is the super that we want to use

654.56

and so in this case we can say

657.44

specifically i want to use the

659.2

like in the init and the constructor i'm

661.279

going to use a constructor for

662.399

base 1 then i'm going to call 2 and then

664.079

i'm going to call 3

665.839

and i could actually

668.88

reverse these so now we're going to see

671.04

something a little funky because what

672.399

we're going to do is we're going to see

674.72

this is actually overwritten and now

676

it's going to force an order on the init

679.36

but we're going to see a different order

680.8

when we do the display oops

685.519

we do the display and so again we're

688.64

actually

689.04

calling into we're manually

692.16

calling into a specific class

695.279

so let's see how that looks

700.24

and so it's going to blow up on us here

701.68

but now we would see is so first we see

705.76

uh let's see where it blew up on line 63

708.24

did i miss something

713.04

base 3 should have a display what did i

716.079

miss

719.04

wait it's died

722.24

in 68 what it called display and died in

726.079

63

728.56

i did not so this is pretty interesting

731.04

this something i had not run into

733.2

but uh let's see oh

737.519

let's do i am let's just do it this way

740.079

because i didn't send that oops that was

741.519

a mistake

742.639

uh vowel one equals i am

749.6

my class

752.8

oh and it doesn't like that because it

755.12

uh let's do

757.2

self dot name equals

760.72

about one

766.32

and okay

770.399

so let's walk through this one a little

771.92

bit

773.76

and so from the top uh let's just do the

776.8

constructor

777.68

first

781.839

so looking just at the constructor which

784.56

if this works should be three two one

792.7

[Music]

794.16

and we'll see here three two and then it

797.2

calls that so it doesn't call

800.32

this third one it only calls the first

803.12

two

806.8

so call it now let's see if we just

808.48

reverse this just to be safe

816.399

now it calls two and then one

821.839

and then it calls the init now if i

824.72

don't call oh cause it yeah because it's

826.16

definitely doing it overriding it calls

827.519

my class init

828.8

so the interesting thing is it does not

832.079

call the constructor the third time

840.079

this may be a little bug that we found

843.6

and you're not honestly going to do this

844.959

very often but just so you know yep so

847.04

it calls one and two

848.959

oh

852.16

but that's calling that up the chain

854.56

it's not actually doing oh

856

because it's doing it oh well let's see

860.72

if i do say you leave it three two one

864.399

now it's just doing that it's not

865.519

calling anything up the chain

868.399

whereas before we all

872.24

uh it only called the first that's right

877.279

and so now let's let's take a look at

880

the display just because

888.639

now here in line 55 it says that it

891.76

doesn't have

893.04

so it's not allowing us to call up

896.48

to that level let's say if we called

899.839

three oh i think we need to do it like

903.6

we did here so let's try this

906.72

well shoot there's a couple ways we can

908.16

do it so let's say i'm gonna call this

910.839

one

912.8

equals that

919.04

and then let's try o1 dot display

929.279

and it's still going to give me that it

930.959

doesn't find that so that's super object

936.959

so let's try this

941.199

and it's giving me a nun so it's not

942.639

returning it back so when i call

944.24

oh when i call super it's not actually

946.88

it's right it's not returning a class

949.279

it's just calling up and doing something

950.88

so i could do

952.399

so it would have to do like a display

956.56

but it's not going to recognize that

959.839

so you really get sort of stuck calling

962.639

up the chain

964.959

if you want to do if you want to call up

966.639

the chain

968

if you want to try to somehow break it

970.72

that is something that's

971.92

that's a challenge with python but i

974.959

want to make sure that you sort of

976.32

looked at that is that there's not a

979.04

while you can

980.079

do it to some extent you can do it in an

982

initializer but you have to make sure it

983.92

has

984.48

that that constructor otherwise

988.079

it's just going to go through each of

990.16

these

991.36

as we saw um let's see if i just go back

994.959

to

995.36

this guy oh well we see this anyway uh

999.36

let's clear that up

1000.32

uh saved yep so let's just clear that

1003.199

out just to go look back at that

1004.959

is notice that we are only calling the

1010.24

first constructor because constructors

1011.839

exist

1012.24

everywhere and so i'm only going to get

1015.199

the actual constructor built off the

1016.959

first class

1018.24

and that may or may not matter

1022

otherwise i'm going to have to do is i

1023.44

would have to go in here

1025.52

and manually call separate

1028.799

pieces so i could come in

1032.48

and well this would be a more complex

1035.439

thing if i wanted to really get into it

1036.799

if i wanted to set some values

1038.16

on each level of these then i would have

1041.76

to do so

1042.88

um so if i did

1046.01

[Music]

1048.319

let's see what happens if i do val2 i

1050.08

don't think that's going to matter for

1051.2

me

1054.96

i still get the same thing because it

1056.48

grabs the first one that matches

1058.96

and i'm off and running

1062.88

and so even if i do my class

1068.08

test

1071.84

it's still calling that base three which

1075.12

is the first one it's going to call

1077.36

so there's a lot of complexity which is

1079.76

why some places don't even deal with it

1081.28

sometimes don't deal with

1083.28

multiple inheritance python does but you

1086.32

have to treat it

1089.12

with a lot of forethought there is not

1091.36

necessarily an

1092.32

easy way to uh

1095.36

to sort of meld stuff and just realize

1097.919

that the probably the most important

1099.2

thing to realize is with multiple

1100.4

inheritance it starts with it goes in

1102.72

order

1103.36

that you see in this declaration here

1106.24

and then you can

1107.039

start moving stuff around i think that's

1109.039

another it doesn't go

1110.64

my understanding is it doesn't go

1111.76

terribly deep on the certification

1113.6

you're not going to have to be to

1114.64

you know be beat up with those kinds of

1117.44

questions

1119.039

but understand that order matters and

1121.76

then

1122.48

that probably will be you know get you

1124.559

through it and know that

1126.559

especially if you have classes that are

1128.88

that have no collisions

1130.559

then it would make a lot of sense but

1132.72

then you're just gonna have to

1135.039

in that case you would you would want to

1136.48

go in and do some work

1138.72

based on the uh the initializer and then

1141.84

it does seem to have some limits as far

1143.6

as calling the

1144.559

the supers so uh definitely your mileage

1148.08

may vary on these i don't think like i

1149.679

said

1150

i don't think that's going to run you're

1151.039

going to run into that as much in the

1153.2

certification tests as you are in the

1155.28

real world

1156.559

and i think now it's time for us to uh

1159.12

un

1159.76

knot our brain a little bit and we will

1162.16

come back next time

1163.12

and work on some other class related

1165.76

stuff

1167.039

but until then go out there and have

1168.72

yourself a great day a great week

1171.12

and we'll talk to you next time

1189.039

you