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
[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