📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Class Inheritance

2021-08-12 •Youtube

Detailed Notes

This episode continues a series of tutorials on classes in Python. We look at inheritance and private members.

Useful Links: https://docs.python.org/3/tutorial/classes.html

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
working on uh i'm adding a note here
talk to you uh we are working on our
python certification
walking through all the things that we
need to know basically
or that are part of that test or part of
the syllabus
and this episode we're going to continue
talking about classes
last time around we went through some
very basic examples
we're going to get a little more
involved in it this time
focus mostly on inheritance and some
things that come
out of that um they
they do apply to classes in general but
it makes more sense in the inheritance
context
so just as a note uh sometimes we've
changed our you know we've sort of have
something
file name based on topic
and we have classes but this time we're
going to actually be in
inherit we're going to focus really on
the inheritance side of that so
let's dive in uh we don't need this guy
right now so we're going to go back to
sort of what we had uh we're going to
start with our simple class
which we if you remember we had a couple
properties
and we had basically the initial the
constructor
and then we just had this display that
it had
and so let's do i'm going to add another
one that's going to be a little bit more
uh just because we're going to have a
couple functions to work with so let's
just create a dump
and this one's going to be let's just do
return whoops if i can do this right
return
self. my name plus
um we'll do
man when i do come we'll do this
strain value
so got a couple of methods in a very
simple class the first thing we're going
to do is we're going to work
with inheritance uh and just to show
here
uh let me just sort of dump this do this
so print
simple dot dump
and if we use these
three in here there we go
uh constructor call to start oh value's
not defined
i'm sorry because that has to be
self.value my mistake
okay so now we can see here's our dump
and
it's just a default constructor so
there's not a whole lot of interesting
stuff in there
now let's say that we want to create
less simple
and this is going to inherit from
simple so we're going to add on top of
the simple class
and what we're going to do here is we're
going to add a new property
which is going to be let's see if we
have a name of value and
a let's just call it an address
and then we're going to add uh
let's see we're going to do the same
display
well actually let's do the same dump
it's it's going to have a dump except
for now
it's going to do super
and it's gonna be let's see
well let's do this
and then return uh self dead address
we're going to fault this to entered
uh let's not i will leave it here for
now let's just do it like this so
um
that will let's just do self-doubt
address okay let's just leave that
because i think that will all compile
yep okay so starting with the simple
stuff here
no pun intended so we're inheriting from
simple so what we're going to find out
here
when we create a
we'll call him l simple
and it is less simple so we inherit from
el simple we can actually do
the same things because
oops we inherited from it
so we already get the display and the
dump and we'll see that here
is that um so here when we called
dump and i do
super now what super does is actually
going to get
the uh it returns a object
that's what we really want to do is
let's see how we do this one
and then he does the uh or prints the
address so you can see that even though
here let's play with this a little bit
more before i mess with this too much
so here we can call display but display
doesn't exist unless simple we're
actually calling display up here
which is just my name and notice that my
name's not here now if we wanted to do
oh let's just take this
so we can there's two things we can do
we can either overwrite dump
and just do this so we can just say
let's do that plus
let's do that
and so we can do it here and we can see
that
it's going to write it gives us what we
need but
um we may need to call something further
up
and this one actually just overwrites
that one so we can either notice that
it's
doing the address it's not doing this
other piece
if we don't have it at all
then it's still going to run it's just
going to call the one up here
it's going to call the parents the
parent
method
so one of the things we can do is let's
do display because that's a little
easier to deal with
so display prints a name now what we can
do
is we can add to it so we can say if we
call super this means it's going to call
the parent
display and then let's just say
i'm going to do print
self.address
so now if we do the display
let's just do that at the end
so now what it's going to do is it's
going to go up because we called super
and that's the first thing we call when
we do this is that we say all right go
up and deal with this
do what this tells me to do and then i'm
going to do some other stuff
and if we do that we see that it calls
so it should be my name which is default
oops let's see what did i call so l
simple display
calls this display which calls super
which should print my name
not entered and then address oh and then
press
addressed so let's just call
let's do this um
base it's gonna be hard to follow maybe
and so he's gonna do bass display and
then let's do
channel display let's do and we called
simple.display
so it's just calling the channel display
so it's not actually calling
this i'm sorry
this actually has to be super dot
because that's just super gives me the
class it doesn't actually call
i forgot this is different from uh like
java and c-sharp a lot of cases it'll
just call up this
but i here have to actually um
so super gives me a class instance
that's whoops and so now
i can if i can get around to what i had
now we can see that it costs a super
so i can actually call up and add on top
of stuff
as needed
uh let's see so now another thing that
we want to do oh well let me
look at this so now also what i can do
is i can do
l simple and i can
even though it doesn't necessarily have
that i can still set my name
equal to lsname
and
ls value and so now i can see
where did that go so i go here i can see
that here's my ls name
ls name and value i can see that where i
called it after i do that little dump
so i can set those values i can also set
the address
equals
and i can get that and so i can actually
so now let's actually extend
dump
and so i can do
so i'm just going to do oh that's going
to be dump
or dimp
so i can take that
and i'm gonna do
plus at
solve that address so now i can
make a more complicated dump here
here we go here's my simple dump
so now i can actually extend off of that
now i may not want to allow
child values to change so i could have
like a id
equals i will start with zero
and then in the initializer we're just
gonna do self dot
id equals
random 10 i'll just do that
probably doesn't like that because i
need to import my random.random okay
i'll do that
and now i can here let's say
in this dump
i'm going to do a bracketed
just to show that now here
i can do self dot id
and i want to be a string
so if i do this i'm going to see my ids
pop
up whoop
oh sorry
let's do 100 times that and this let's
make that
uh
all right let's just leave it like that
forget it i'm not gonna worry about that
i'm not open up plus times so now let's
see here's my id
um yeah let's actually let's go ahead
and make that an end
here so here's my id and so you can see
every time i'm getting a different one
so here's i generated an id
and notice even though i don't have that
init anywhere it's picking that up
here in my less simple so it creates uh
there was for my simple
here's my id for my less simple
but if i wanted to display it less
simple let's say
i wanted to do um
let's just call it my id
oh that's gonna be a self and then let's
just do
print string
self dot id
oh i need to actually call that so now
if i do all simple
dot what i call my id
we're gonna see here we're gonna get an
error because
when it calls that underscore underscore
id less simple doesn't have it
so this is a construct that we have to
make stuff private
is that we can make we add a double
underscore
and that's going to make something
private which means it's not going to be
directly accessible
by the the child
and so if i actually were to take let's
see so dump and display
so let's do that with display let's show
you what happens there
so now if i do that
here we go simple object has no
attribute display
because it is not i have to actually
call the private version of it
see by here simple object has no
attribute display
so even there i cannot access it
because it doesn't even see it the only
way i could do it would be
in here then i can i can have a
reference to it
and that allows you to do some sort of
protected stuff now i could do like in
my
id if i put it here
so now it has access to that private
value
um oops did i do that oh i didn't fix my
display
apologies
and so now it works because the id is
going to get it but it's calling it up
here
so within that class it can but i cannot
access that
private those private properties within
my
child classes
and i think i'm going to hold off there
the next thing i'm going to get into is
there's actually
multiple inheritance and i guess before
going
i do want to say that if you have a you
can do a module dot
so if you have a class within a module
um sometimes we'll run it we'll talk
more about modules later
but i just wanted to touch on that in
case you're looking at some examples and
you see
this kind of a syntax is that
this means that the class is just
available if i want to do one within a
specific
module then i'm going to have to i'll
give it a module name to define it
sort of like you know up here if i want
to do a specific type of random
and so we've we've walked through very
basic
inheritance and how things can and then
we can also extend
so i could add a whole new something
here well which i did
um with the my id
so i guess just to show how this work
let me do that let's do my address
just in partings if i do
address and so
that's for less simple now notice that
that means that i cannot call
simple dot my address
it's gonna blow up because sim less
simple
oh sorry what did i do i gotta get rid
of that one first
and so i see here simple does not have
my address
so it's a one-way relationship this
this guy inherits from this this does
not inherit from there
and if i wanted to try this just to show
if i want to try to make some circular
reference
then what's going to happen is less
simple has not been defined yet
so i cannot do a circular reference of
of inheritance
and that'll wrap it up for this time i
want to dig into some we're going gonna
dig more into
inheritance next time around uh but i
don't want to get too long on this one
so
as always go out there have yourself a
great day a great week
and we will talk to you next time
you
Transcript Segments
0.46

[Music]

26.48

well

27.039

hello and welcome back we are

30.16

working on uh i'm adding a note here

34

talk to you uh we are working on our

36.559

python certification

37.84

walking through all the things that we

40.559

need to know basically

41.84

or that are part of that test or part of

44.16

the syllabus

45.52

and this episode we're going to continue

47.6

talking about classes

49.44

last time around we went through some

51.12

very basic examples

52.64

we're going to get a little more

54.239

involved in it this time

56.239

focus mostly on inheritance and some

58.48

things that come

59.44

out of that um they

62.559

they do apply to classes in general but

64.64

it makes more sense in the inheritance

66.479

context

67.52

so just as a note uh sometimes we've

69.76

changed our you know we've sort of have

70.96

something

72.08

file name based on topic

75.119

and we have classes but this time we're

77.2

going to actually be in

78.159

inherit we're going to focus really on

80.479

the inheritance side of that so

83.04

let's dive in uh we don't need this guy

85.68

right now so we're going to go back to

88.799

sort of what we had uh we're going to

90.72

start with our simple class

92.159

which we if you remember we had a couple

94

properties

95.36

and we had basically the initial the

97.439

constructor

98.88

and then we just had this display that

101.28

it had

103.28

and so let's do i'm going to add another

107.119

one that's going to be a little bit more

110.64

uh just because we're going to have a

111.6

couple functions to work with so let's

112.96

just create a dump

114.24

and this one's going to be let's just do

118.079

return whoops if i can do this right

122.159

return

125.92

self. my name plus

129.679

um we'll do

133.599

man when i do come we'll do this

137.2

strain value

140.879

so got a couple of methods in a very

142.959

simple class the first thing we're going

144.16

to do is we're going to work

145.36

with inheritance uh and just to show

148.959

here

150.72

uh let me just sort of dump this do this

153.12

so print

155.44

simple dot dump

160.4

and if we use these

163.92

three in here there we go

167.12

uh constructor call to start oh value's

170.319

not defined

171.599

i'm sorry because that has to be

174.08

self.value my mistake

176.72

okay so now we can see here's our dump

179.2

and

179.84

it's just a default constructor so

181.44

there's not a whole lot of interesting

182.879

stuff in there

184.64

now let's say that we want to create

186.879

less simple

188.64

and this is going to inherit from

191.92

simple so we're going to add on top of

194.64

the simple class

196.56

and what we're going to do here is we're

198.64

going to add a new property

200.159

which is going to be let's see if we

201.44

have a name of value and

203.76

a let's just call it an address

210.48

and then we're going to add uh

213.68

let's see we're going to do the same

215.28

display

216.72

well actually let's do the same dump

220.64

it's it's going to have a dump except

222.4

for now

223.84

it's going to do super

232.08

and it's gonna be let's see

236.879

well let's do this

243.36

and then return uh self dead address

249.12

we're going to fault this to entered

254.72

uh let's not i will leave it here for

257.68

now let's just do it like this so

261.199

um

264.24

that will let's just do self-doubt

266

address okay let's just leave that

268.08

because i think that will all compile

270.639

yep okay so starting with the simple

274.32

stuff here

275.759

no pun intended so we're inheriting from

278.16

simple so what we're going to find out

279.919

here

280.56

when we create a

283.919

we'll call him l simple

287.919

and it is less simple so we inherit from

290.96

el simple we can actually do

294.88

the same things because

299.36

oops we inherited from it

302.88

so we already get the display and the

305.36

dump and we'll see that here

311.919

is that um so here when we called

316.32

dump and i do

320.16

super now what super does is actually

322.16

going to get

323.199

the uh it returns a object

326.479

that's what we really want to do is

330.24

let's see how we do this one

334.08

and then he does the uh or prints the

336

address so you can see that even though

338.32

here let's play with this a little bit

340.08

more before i mess with this too much

344.4

so here we can call display but display

346.32

doesn't exist unless simple we're

347.68

actually calling display up here

349.44

which is just my name and notice that my

352.16

name's not here now if we wanted to do

356.56

oh let's just take this

361.759

so we can there's two things we can do

363.84

we can either overwrite dump

367.28

and just do this so we can just say

376.24

let's do that plus

383.28

let's do that

386.639

and so we can do it here and we can see

389.199

that

390.96

it's going to write it gives us what we

393.199

need but

395.36

um we may need to call something further

398.16

up

398.4

and this one actually just overwrites

399.759

that one so we can either notice that

401.68

it's

402.16

doing the address it's not doing this

405.12

other piece

406

if we don't have it at all

409.28

then it's still going to run it's just

410.639

going to call the one up here

412.4

it's going to call the parents the

414.8

parent

415.52

method

419.199

so one of the things we can do is let's

422.4

do display because that's a little

424.8

easier to deal with

428.4

so display prints a name now what we can

431.199

do

431.84

is we can add to it so we can say if we

433.759

call super this means it's going to call

435.68

the parent

436.56

display and then let's just say

440.319

i'm going to do print

443.8

self.address

446.88

so now if we do the display

452.08

let's just do that at the end

456.08

so now what it's going to do is it's

457.599

going to go up because we called super

459.44

and that's the first thing we call when

460.639

we do this is that we say all right go

462.479

up and deal with this

464.16

do what this tells me to do and then i'm

466.56

going to do some other stuff

469.28

and if we do that we see that it calls

475.36

so it should be my name which is default

480.56

oops let's see what did i call so l

482.639

simple display

483.599

calls this display which calls super

486.72

which should print my name

488.879

not entered and then address oh and then

492.08

press

492.4

addressed so let's just call

496.4

let's do this um

502.639

base it's gonna be hard to follow maybe

510.08

and so he's gonna do bass display and

512

then let's do

520.159

channel display let's do and we called

525.24

simple.display

530.88

so it's just calling the channel display

532.8

so it's not actually calling

534

this i'm sorry

537.6

this actually has to be super dot

540.72

because that's just super gives me the

542.24

class it doesn't actually call

544.16

i forgot this is different from uh like

547.279

java and c-sharp a lot of cases it'll

548.959

just call up this

550.56

but i here have to actually um

553.76

so super gives me a class instance

555.44

that's whoops and so now

557.12

i can if i can get around to what i had

560.959

now we can see that it costs a super

564.48

so i can actually call up and add on top

567.04

of stuff

567.6

as needed

570.959

uh let's see so now another thing that

572.64

we want to do oh well let me

574.48

look at this so now also what i can do

576.399

is i can do

577.6

l simple and i can

580.64

even though it doesn't necessarily have

582.399

that i can still set my name

584.24

equal to lsname

588.56

and

599.279

ls value and so now i can see

602.88

where did that go so i go here i can see

606.64

that here's my ls name

608.64

ls name and value i can see that where i

610.48

called it after i do that little dump

614.399

so i can set those values i can also set

616.72

the address

622.64

equals

629.12

and i can get that and so i can actually

631.92

so now let's actually extend

634

dump

638.32

and so i can do

645.44

so i'm just going to do oh that's going

648.8

to be dump

653.92

or dimp

657.2

so i can take that

661.2

and i'm gonna do

665.12

plus at

669.839

solve that address so now i can

673.44

make a more complicated dump here

677.36

here we go here's my simple dump

680.48

so now i can actually extend off of that

683.92

now i may not want to allow

687.12

child values to change so i could have

690.56

like a id

693.519

equals i will start with zero

698

and then in the initializer we're just

700.48

gonna do self dot

703.6

id equals

708.64

random 10 i'll just do that

712.32

probably doesn't like that because i

715.44

need to import my random.random okay

717.6

i'll do that

722

and now i can here let's say

725.6

in this dump

728.959

i'm going to do a bracketed

737.2

just to show that now here

740.72

i can do self dot id

744.32

and i want to be a string

755.04

so if i do this i'm going to see my ids

758.32

pop

758.639

up whoop

761.68

oh sorry

765.279

let's do 100 times that and this let's

768.24

make that

770.839

uh

777.04

all right let's just leave it like that

778.72

forget it i'm not gonna worry about that

780.399

i'm not open up plus times so now let's

786.399

see here's my id

788.079

um yeah let's actually let's go ahead

791.6

and make that an end

793.839

here so here's my id and so you can see

795.519

every time i'm getting a different one

797.2

so here's i generated an id

800.399

and notice even though i don't have that

803.519

init anywhere it's picking that up

805.44

here in my less simple so it creates uh

808.56

there was for my simple

809.76

here's my id for my less simple

812.8

but if i wanted to display it less

815.12

simple let's say

816.639

i wanted to do um

820

let's just call it my id

824.079

oh that's gonna be a self and then let's

827.839

just do

828.959

print string

834.639

self dot id

840.399

oh i need to actually call that so now

842.88

if i do all simple

844.24

dot what i call my id

850.16

we're gonna see here we're gonna get an

853.44

error because

854.88

when it calls that underscore underscore

856.32

id less simple doesn't have it

859.68

so this is a construct that we have to

861.6

make stuff private

863.44

is that we can make we add a double

866

underscore

867.12

and that's going to make something

868.399

private which means it's not going to be

870

directly accessible

872.639

by the the child

876.24

and so if i actually were to take let's

879.04

see so dump and display

883.519

so let's do that with display let's show

885.76

you what happens there

888

so now if i do that

892.48

here we go simple object has no

895.76

attribute display

899.6

because it is not i have to actually

902

call the private version of it

906.24

see by here simple object has no

908.16

attribute display

910.079

so even there i cannot access it

913.199

because it doesn't even see it the only

915.68

way i could do it would be

917.04

in here then i can i can have a

919.44

reference to it

921.68

and that allows you to do some sort of

924.16

protected stuff now i could do like in

926

my

926.32

id if i put it here

932.399

so now it has access to that private

934.56

value

936.079

um oops did i do that oh i didn't fix my

939.279

display

940

apologies

943.68

and so now it works because the id is

946.56

going to get it but it's calling it up

948.16

here

949.68

so within that class it can but i cannot

952.32

access that

953.199

private those private properties within

956.079

my

956.8

child classes

960.16

and i think i'm going to hold off there

962.32

the next thing i'm going to get into is

963.44

there's actually

964.32

multiple inheritance and i guess before

967.44

going

968.079

i do want to say that if you have a you

970.24

can do a module dot

971.92

so if you have a class within a module

974.959

um sometimes we'll run it we'll talk

976.8

more about modules later

978.32

but i just wanted to touch on that in

979.759

case you're looking at some examples and

981.68

you see

982.48

this kind of a syntax is that

985.6

this means that the class is just

987.279

available if i want to do one within a

989.759

specific

990.48

module then i'm going to have to i'll

992.88

give it a module name to define it

994.8

sort of like you know up here if i want

996.959

to do a specific type of random

1000.48

and so we've we've walked through very

1003.04

basic

1003.92

inheritance and how things can and then

1005.839

we can also extend

1007.759

so i could add a whole new something

1009.759

here well which i did

1011.6

um with the my id

1017.68

so i guess just to show how this work

1019.36

let me do that let's do my address

1022.399

just in partings if i do

1027.52

address and so

1030.88

that's for less simple now notice that

1032.72

that means that i cannot call

1035.12

simple dot my address

1040.079

it's gonna blow up because sim less

1042.24

simple

1043.439

oh sorry what did i do i gotta get rid

1046.16

of that one first

1048.799

and so i see here simple does not have

1051.919

my address

1052.799

so it's a one-way relationship this

1055.919

this guy inherits from this this does

1058.4

not inherit from there

1059.76

and if i wanted to try this just to show

1061.919

if i want to try to make some circular

1063.84

reference

1065.12

then what's going to happen is less

1067.28

simple has not been defined yet

1069.679

so i cannot do a circular reference of

1072.48

of inheritance

1076.24

and that'll wrap it up for this time i

1077.76

want to dig into some we're going gonna

1079.12

dig more into

1080.16

inheritance next time around uh but i

1082.559

don't want to get too long on this one

1084.08

so

1085.2

as always go out there have yourself a

1087.039

great day a great week

1088.72

and we will talk to you next time

1106.96

you