Detailed Notes
This episode starts a series of tutorials on classes in Python.
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're considering our series on python certification and we're going to start into now looking at classes within python and so we're going to dive into it today get some basic definitions whoops and um then we're gonna go forward into uh we do inheritance and a lot of other good stuff so though there'll be several class related uh python class and object related discussions uh but this is the uh and this is the first one we're just going to get started so right now we've got a very simple class when you want to create a class in python is you just use the class keyword so i've got a class i'm going to call it simple and now what a class can do and without getting too deep into it and there'll be some show notes some links if you want to go a little deeper in classes but classes have attributes and they have methods or properties and methods so what i've created here is i'm going to do this this will be a class properties and then i'll come down here i do class methods and let me do this just and i'm also gonna do uh define display value print different display plus value okay we'll keep that very simple no pun intended all right so let's see what it complains about here oh okay it wanted an extra line in there for formatting which is fine um to sort of basically wants this extra this is a formatting thing around the class itself anyways back to that so a class has properties which are things which are values that a an instance stores and they have methods which are functions essentially that you can call on that class and its properties in this case we've got a very simple prop we've got one property that we're just going to call it my name and we're going to define it and we're going to assign it a value here by default and then we've got one method which is display and it's going to display it's going to print out my name now notice here we have our sort of normal um definition we have that basically built in so we have def space and then the name but any class level method is gonna have it's gonna start with self and so i'm going to actually put a second one here to show how this works so if i do display 2 count my value and here is the value and we'll just force it to string whatever it happens to be so both of these are methods display and display two are both methods of the simple class and in the one case we don't send it we're not gonna send it anything we'll see how that looks in a second and the other we're gonna send it a value and so to create an instance well let's first do this um let's move this up so if we just take simple the class and try to call display which does not have a self we are going to systems class example uh three class example and we're going to see here that it's missing a positional argument itself so it requires something however let's do [Music] let's create a variable of type class which is as easy as this so we just do simple and we're going to do simple get the class name notice that we've got the case sensitivity there and then if we use a class name with no parameters by default that's going to use the default constructor we'll talk about those a little bit later and let's go ahead and then and then on that variable of class you know type class simple now we're going to call it and notice we're still not going to send a parameter and let's see what that does and now we see that it works it says i am simple because by default what it's going to do or the way it's constructed is self means i need to call this on an instance i need to create a variable an instance of the class first and then do it so if i create one more if i just do a static here i'm going to say print let's see what it says and so now if i do simple.display it's probably going to complain and it complains here you see that a simple.display it requires a positional argument of self oh probably because let's do display three just to be sure that we're calling the right one and uh let's do we'll deal with display two in a minute so display three so now we can see i can't do display without any because it actually ends up overwriting so if i do this what happens is further down i'm basically redeclaring this now with a new signature but if i call this display three then i can actually call that on the class and this may even show so i can oh man it doesn't help me which is static and which is not so this would be something i can call at the class but if i try to do france dot name or i'm sorry my name then it's going to give me an error because self is not defined it's not defined for the class level it's only at an instance so i have to have an instance in order to have properties so i can do some stuff up at store functions up on a class but and they're called static because those are at the class level those are any class any the class will always have those available however it cannot use the properties within the class because those require an instance so let's go back and look at this so uh i don't want to cover this is that notice that because this is my name self also if you've done if you come out of the world of like a um java or c sharp you may think of that also as uh this i think visual basic maybe uses self but basically it says that i want to go to whatever this instance is i want to display the property value for this instance and now we can come here and if we do display two i'm gonna show that one real quick now here because this self is already part of it because i'm doing simple.display2 then i just give it a value so i'm going to give it a value of my value and then we're going to see that this one comes through i am simple and here's a value and you can see that it's my value so i use self to get the the instance level but i can use the if i don't use self it's going to take the local value so if i add which can sometimes get confusing so if i do value equals let's see first value then what we'll see here uh let's see and let's just do print self.value well value okay so what we're going to see here is it does what we originally saw so it does take value it uses this value here but i can distinguish i want to use the property level value here and i get where it says first value so that gives me uh properties and the ability to access that access them now i can you may see so far okay cool what good does any of this do for me because well let's say you know let's say i put all this stuff together why do i have a class instead of just leave these as functions and variables well what i can do here is i can do simple 2 equals simple and now this is where i start working with it so now i can actually i can access these properties so i can do simple two dot my name equals let's see second instance and now if i do simple two dot display then i'm gonna see something different uh let's just do this uh let's get a couple spaces in there just cut so now the second instance we've gone in and we created it we have another instance so we have simple and simple one now this time we're gonna go in and we're gonna set the name on simple two to something different and then do a display and we'll see here boom second instance so we could also do um sample 2 dot value equals another value and then if we do this uh let's just leave it my value because i'm passing the same value in then we're gonna see here so second instance and here's my value and we can see another value there for the value because we've set that out we've assigned that so we have the way i have the ability to work with these things uh as an instance you know individually so we can work with one for a while we can work differently with another we have a series so we could use these to store things so we could have like uh let's say we had an address class that would have a city state zip and then we can do stuff with it and we could actually have multiple addresses and then we can do things like let's say i want to do just print and here i'm going to print which is basically the same as well we'll just keep it as display so now i could do let's split this guy up real quick and i'm gonna do simple three and then simple three dot uh oh i have to spell it right at my name equals third let's call it i am third and then i can do an array so let's just do uh instances equals i call it so simple simple two kind of simple three and then i can do four item in instances and i'm gonna be able to call that so for each one i can do item.display and then we're going to see here at the end simple second and third so i can wheel and deal with these i can do stuff here where i can actually call that display on each of these now if i wanted to do something that doesn't have a display let's do just to show that so if i do class uh let's see simple call it less simple oops sorry i don't need that and then i'm just going to call let's see i'm going to keep the same values let's do it this way let's actually give it a display three even and i display three we'll give it a display too so let's say that so let's say that we have here so less simple is just simple a simple value and so now i can do all of the same stuff and i can create simple four less simple and if i try to do it here let's see what happens go so note that we're calling display but simple less simple does not have that it does not have a display that has been defined for it so here boom less simple object has no attribute display now we're going to find out how we can do some things like that and have classes relate to each other but that's that will be uh next time around i will put going to cover that in another one i do want to cover the init um i don't call we'll put it down here so if we do define and itself [Music] this is a constructor which means that and it's a no argument constructor so here when i create this i'm sorry when i create when i do this it fires off a constructor so what i'm going to do here is i'm just going to say print constructor called and then let me change these two things here so i'm going to change here so i'm going to say self dot my name so let's say i just by default i'm going to set these things to some default some uh simple values of this is just you know i'm clearing this thing out so i don't have any values on it so now if i do this and run it oh i want to get rid of i don't want to call you anymore so now we can see here that the constructor is called and then when we come in here and call display if we go back and look at display display just does my name but there's nothing there and then the display and it calls display 2 with my value well my name is blank so see it doesn't have that and here's the value and so that still comes through but if i do the value after that there's a blank so i could do let's just call let's make it a little easier if i do that default name and default value then i'm going to see those default name default name and so i don't have to declare these guys here or i can just say you know something like this i can say this is what i default to and now i can do um well i can also do that one valve two and in this case my name equals l1 value equals val2 and so if i want to initialize so now this would be uninitialized but then i can turn around and do and let's do these at the end so let's see let's do let's just do it this way so this is going to be uh default well let's do uninet equals that and then if i display it but now if i do on init but now if i do initialized [Music] now i can give it simple um init name and a net value and then if i do init display uh let's see oh and here's the promise that i don't have both so it doesn't like that so if i do this empty and empty let's see what that does and what you're probably going to see is this going to get overwritten we see that from time to time uh let's see yeah and so here see here it takes one positional argument but three we're given so we have to do is this goes back to what we saw before is that i need to allow it to be uh let's just do default and default and so now because i've set those up i can actually call both of these and we can see here that here's where it gets the default and here's where it gets to i'm sorry and here is where it gets a net name so when we create a create constructors we're going to basically build some default values out and in this case that allows us to call it with no values or with name values much as we saw when we went into the some discussion about functions so that's the basics of a class in an instance and we're going to get much more into this moving forward but i think that probably is a good stopping point for today so i will let you get back to it as always there'll be uh source code repository is out there we have links in the show notes so that you can get to that you can see these you can play around them and do whatever you want with them we also have links to some additional information if you want to learn some more about classes and python classes in particular if you have any questions as always shoot us an email at info developer.com and that'll wrap it up 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're considering our series on python
certification
and we're going to start into now
looking at classes
within python and so we're going to
dive into it today get some basic
definitions whoops and
um then we're gonna go forward into uh
we do inheritance and a lot of other
good stuff so though
there'll be several class related uh
python class
and object related discussions uh but
this is the
uh and this is the first one we're just
going to get started so
right now we've got a very simple class
when you want to create a class
in python is you just use the class
keyword
so i've got a class i'm going to call it
simple
and now what a class can do and without
getting too deep into it
and there'll be some show notes some
links if you want to go a little deeper
in classes but classes have attributes
and they have methods
or properties and methods so what i've
created here
is i'm going to do this this will be a
class properties
and then i'll come down here i do class
methods
and let me do this just and i'm also
gonna do
uh define display
value print
different display
plus value okay we'll keep that very
simple
no pun intended all right so
let's see what it complains about here
oh okay it wanted an extra line in there
for formatting which is fine
um to sort of basically wants this extra
this is a formatting thing around the
class itself anyways
back to that so a class has properties
which
are things which are values that a an
instance stores and they have methods
which are
functions essentially that you can call
on
that class and its properties in this
case we've got a very simple prop we've
got one property that we're just going
to call it my name
and we're going to define it and we're
going to assign it a value here by
default
and then we've got one method which is
display and it's going to display
it's going to print out my name now
notice here
we have our sort of normal um
definition we have that basically built
in so we have def
space and then the name but any class
level
method is gonna have it's gonna start
with self and so
i'm going to actually put a second one
here to show how this works so if i do
display
2 count my value
and here is the value
and we'll just force it to string
whatever it happens to be
so both of these are methods display and
display two are both methods
of the simple class and
in the one case we don't send it we're
not gonna send it anything we'll see how
that looks in a second
and the other we're gonna send it a
value and so to create
an instance well let's first do this
um let's move this up
so if we just take simple the class and
try to call display
which does not have a self we are going
to systems class example
uh three
class example and we're going to see
here that it's missing
a positional argument itself
so it requires something however
let's do
[Music]
let's create a variable of type class
which is as easy as this so we just do
simple and we're going to do simple
get the class name notice that we've got
the case sensitivity there
and then if we use a class name with no
parameters
by default that's going to use the
default constructor we'll talk about
those a little bit later
and let's go ahead and then and then
on that variable of class you know type
class
simple now we're going to call it and
notice we're still not going to send a
parameter and let's see what that does
and now we see that it works it says i
am simple because by default
what it's going to do or the way it's
constructed
is self means i need to call this on
an instance i need to create a variable
an instance of the class
first and then do it so if i create one
more if i just do a static
here i'm going to say print
let's see what it says
and so now if i do simple.display
it's probably going to complain and it
complains here
you see that a simple.display it
requires a positional argument of self
oh
probably because let's do
display three
just to be sure that we're calling the
right one
and uh let's do we'll deal with display
two in a minute
so display three so now
we can see i can't do display without
any because
it actually ends up overwriting so if i
do this
what happens is further down i'm
basically redeclaring this now
with a new signature but if i call this
display three
then i can actually call that on the
class and this may even show
so i can oh man it doesn't help me which
is static and which is not
so this would be something i can call at
the class
but if i try to do
france dot name
or i'm sorry my name
then it's going to give me an error
because
self is not defined it's not defined for
the class level it's only at an instance
so i have to have an instance in order
to have properties
so i can do some stuff up at store
functions up on a class but and they're
called static
because those are at the class level
those are
any class any the class will always have
those available
however it cannot use the properties
within the class
because those require an instance
so let's go back and look at this so uh
i don't want to cover this
is that notice that because this is my
name
self also if you've done if you come out
of the world of like a um
java or c sharp you may think of that
also as
uh this i think visual basic maybe uses
self
but basically it says that i want to go
to whatever this instance is
i want to display the property value
for this instance and now we can come
here
and if we do display two i'm gonna show
that one real quick
now here because this self is already
part of it because i'm doing
simple.display2
then i just give it a value so i'm going
to give it a value of
my value
and then we're going to see that this
one comes through i am simple and here's
a value
and you can see that it's my value so
i use self to get the the instance level
but i can use the if i don't use self
it's going to take the
local value so if i add which can
sometimes get confusing so if i do value
equals let's see
first value
then what we'll see here uh let's see
and let's just do print
self.value well
value okay
so what we're going to see here is it
does what we originally saw
so it does take value it uses this value
here but i can distinguish i want to use
the property level value
here and i get where it says first value
so that gives me uh properties
and the ability to access that access
them
now i can you may see so far okay
cool what good does any of this do for
me because
well let's say you know let's say i put
all this stuff together
why do i have a class instead of just
leave these as functions and
variables well what i can do here is i
can do
simple 2 equals simple
and now this is where i start working
with it so now i can actually i can
access these properties so i can do
simple
two dot my name
equals let's see
second instance
and now if i do simple two dot display
then i'm gonna see something different
uh let's just do
this uh
let's get a couple spaces in there just
cut
so now the second instance we've gone in
and we created it
we have another instance so we have
simple and simple one
now this time we're gonna go in and
we're gonna set the name on simple two
to something different
and then do a display and we'll see here
boom
second instance so we could also do
um sample 2
dot value equals
another value and then
if we do this
uh let's just leave it my value because
i'm passing the same value in
then we're gonna see here so second
instance and here's my value and we can
see
another value there for the value
because we've set that out we've
assigned that
so we have the way i have the ability to
work with these things uh
as an instance you know individually so
we can work with one for a while we can
work differently with another
we have a series so we could use these
to store things
so we could have like uh let's say we
had an address class that would have a
city state zip
and then we can do stuff with it and we
could actually have multiple addresses
and then we can do things like let's say
i want to do just print
and here i'm going to print
which is basically the same as well
we'll just keep it as display
so now i could do let's
split this guy up real quick and i'm
gonna do
simple three
and then simple three dot
uh oh i have to spell it right
at my name equals
third let's call it i am third
and then i can do an array so let's just
do
uh instances
equals i call it so
simple simple two kind of simple three
and then i can do four item in
instances
and i'm gonna be able to call that so
for each one i can do item.display
and then we're going to see here at the
end simple second and third so i can
wheel and deal with these i can do stuff
here where i can actually call that
display
on each of these now if i wanted to do
something that doesn't have a display
let's do just to show that so if i do
class uh
let's see simple call it less simple
oops sorry i don't need that and then
i'm just going to call let's see i'm
going to keep the same values
let's do it this way let's actually give
it a display three even
and i display three we'll give it a
display too
so let's say that so let's say that we
have here
so less simple is just
simple
a simple value
and so now i can do all of the same
stuff and i can create
simple four
less simple
and if i try to do it here
let's see what happens go so note that
we're calling display but
simple less simple does not have
that it does not have a display
that has been defined for it so here
boom
less simple object has no attribute
display
now we're going to find out how we can
do some things like that and have
classes relate to each other but that's
that will be uh next time around i will
put going to cover that in another one
i do want to cover
the init
um i don't call we'll put it down here
so if we do define
and itself
[Music]
this is a constructor which means that
and it's a no argument constructor so
here when i create
this i'm sorry when i create when i do
this
it fires off a constructor so what i'm
going to do here
is i'm just going to say print
constructor
called and then let me change these two
things here so i'm going to change here
so i'm going to say self
dot my name so let's say i just
by default i'm going to set these things
to some default some
uh simple values of this is just
you know i'm clearing this thing out so
i don't have any values on it
so now if i do this and run it oh
i want to get rid of i don't want to
call you anymore
so now we can see here that the
constructor is called
and then when we come in here and call
display if we go back and look at
display
display just does my name but
there's nothing there and then the
display and it calls display 2 with my
value well my name
is blank so see it doesn't have that
and here's the value and so that still
comes through but if i do the value
after that there's a blank
so i could do
let's just call let's make it a little
easier if i do that default name
and default value
then i'm going to see those default name
default name
and so i don't have to declare these
guys here
or i can just say you know something
like this i can say this
is what i default to and now
i can do um well i can also do that one
valve two
and in this case my name equals
l1 value equals val2
and so if i want to initialize so now
this would be uninitialized
but then i can turn around and do
and let's do these at the end so let's
see let's do
let's just do it this way
so this is going to be uh default
well let's do uninet
equals that and then if i display it
but now if i do on init
but now if i do initialized
[Music]
now i can give it simple um
init name and a net value
and then if i do init display
uh let's see oh and here's the promise
that i don't have both
so it doesn't like that so
if i do this
empty and empty let's see what that does
and what you're probably going to see is
this going to get overwritten we see
that from time to time
uh let's see yeah and so here see here
it takes one positional argument but
three we're given so we have to do
is this goes back to what we saw
before
is that i need to allow it to be
uh let's just do default
and default and so now
because i've set those up i can actually
call both of these
and we can see here that here's where it
gets the default and here's where it
gets to
i'm sorry and here is where it gets a
net name
so when we create a create constructors
we're going to basically build some
default values
out and in this case that allows us to
call it with no values or
with name values much as we saw when we
went into the
some discussion about functions so
that's the basics
of a class in an instance and we're
going to get much more into this
moving forward but i think that probably
is a good stopping point for
today so i will let you get back to it
as always there'll be uh source code
repository is out there we have links in
the show notes so that you can get to
that
you can see these you can play around
them and do whatever you want with them
we also have links to some additional
information if you want to learn some
more about classes and python classes
in particular if you have any questions
as always shoot us an email at info
developer.com and that'll wrap it up
so as always go out there have yourself
a great day a great week
and we will talk to you next time
you