Detailed Notes
We look at useful functions for working with classes in Python for this episode focused on getting ready for certification.
Useful Links: https://www.programmersought.com/article/8963908455/
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 looking at examples for python certification and this episode we're going to deal with we're going to swing back around a little bit to classes and this time we're going to look at some of the uh we'll call them the the type related functions for classes now to do this we've got two classes that we've declared we've seen these before how we've looked at the inheritance we've got simple and then we have simple which inherits from simple so very simple not to get too caught up with that word but we have a straightforward hierarchy you have simple and then you have less simple which inherits from simple so it looks something like uh if you have like a it should be something like this you know basically that becomes less simple so just to remember that let's put that there now what we're going to do is we're going to create one of each just because and the first thing we want to look at and actually let's just do a um this will just be value equals uh abc and we're gonna do value two equals one two three now let's do that one two three as well and then actually let's do value three equals uh let's see one comma two comma three and now the first one we're gonna look at is type so what i'm gonna do is take each of those and i'm going to do type of simple well i'll do it again and again again and so it's going to be l simple value value 2 and value three so let's look at each of these just do that clear it up let's type functions there we go let's see oh because we've got a random in there somewhere for that id let's just get rid of that okay so we can see here uh in order so simple is a class that's defined in main that's simple less simple is going to be whatever the which is the class that it is declared of so that is the type and we can see that we can send a string uh here uh value so it tells us that's an str tells us this is an int this is a tuple and actually let's go ahead and do just to wrap these up give us a couple different things let's do one point two three it's gonna be value four two three and four and let's see what it does for that decimal and we'll see here that that's a float and so type tells us the type uh what if it's a native type it'll give us like string integer tuple whatever if it's a class it's going to tell us what the class type is now it does tell us where the module is that it is uh that's been declared and in this case it's main we could could import it from elsewhere i'm not going to mess with that we'll actually cover modules in a different episode and probably see it then so let's look at is instance now this one becomes um let's do it this way is instance examples so let's start with print uh let's see simple oh sorry is instance and so is instant let's see is simple going to be an instance of simple and then let's say what happens is l simple and it's not so simple and then let's do those two same ones as less simple let's see what it tells us oop there we go and so we get true true false true so true true let's see so i'll say like true true that's false and that's true so simple is an instance of simple because we can tell right up here that it is but it also less simple is going to be an instance of simple because it inherits remember less simple comes from samples because it's a child now simple is not an instance of less simple it if it's further down the hierarchy then it's not going to count so you can't cover it there and then or you know it's not it's not an instance of that it only works down the hierarchy not back up the hierarchy and then of course less simple is also an instance of less sub or l simple as an instance of less simple because that's directly what we're creating it so notice that that l simple is at the same time an instance of simple and also an instance of less simple so those two are pretty straightforward let's do let's try this one so now let's do is instance ah let's see let's do value let's see is it a string and then let's see is it an ant and let's see let's see what comes up here so value is uh it's a string one two three now let's see what it tells us it's an instance of and there you go the natives examples it is an instance of a strain but even though one two three is an int you know we can convert it to such it's not an instance but if we were to go do this then it's true so then we've just converted it and said nope that's going to be our instance so let's look at der let's see so let's print and let's do simple uh let's try that so let's do uh the instance simple and see what happens this is going to be tour examples let's clear this and run it and so here when we run simple this is everything that is a part of what we see is everything that's a part of this simple class so we're going to see dump we're going to see display my name value id so let's see so it has and these are all things that we can call on simple so we can get an id we get a class we can delete an attribute we get a dictionary dir doc quit forma blah blah blah we get all these and then let's see somewhere down here oh here we go and then we get uh i think we call it my name and value yeah so we see my name and value there's my properties and i did display and dump there's my the um the methods i specifically created plus a knit it's probably going to be in there somewhere there we go but i could call any of these now some of these we've seen to some extent but we're going to continue playing around with these a little bit so let me look at like so what happens if i do simple dot underscore str let's see what that does it gets the end and it just says it's a simple object which is interesting but what i can do is i can actually define that [Music] at this level and i can say uh so oop self and then he's just gonna now he's gonna return instead of that which you would recognize if you've done this uh a lot of time like in java or c-sharp stuff like that there's a lot of times that we create a essentially a two-string kind of method so that we can convert it to a string and so i would say here if i do self dot my name and then i'm just gonna do it like this self.value and now if i do that there we go default and default so now i've overwritten what you can do we've talked about that to some extent you can override things so i've gone in and overridden the string method and i can do this with others so i can actually come in and do things like comparators so i can do greater thans and greater than equals uh let's look at let's look at our class so now we could since this is an array what i can do is i can do print [Music] well this will probably blow up because a lot of those don't exist but i could actually map them if i wanted to i could pray map each of those but i'm not going to because those are not i'm not going to be able because i'm not going to have the same uh variables parameters for each of those but let's do i do want to do that let's do let's print a couple of these out so we can see what they look like so remember this is at the simple level uh let's do class and let's do size of oh that'd be another good one [Music] now let's just see what it comes up with on these so let's see so class see string clad oh yeah simple blah blah blah oh here we go so here uh strimple does that clap whoops but class is actually going to generate an instance of that and then size of is just going to give us it's basically this is its size in memory so there's a bunch of little things we can do playing around with this uh the important thing is that we can come in we can see if things exist and sort of dynamically create things that's our dir so let's do a has attribute and so let's do that so now let's do [Music] let's see if simple has an attribute called value called my name actually let's do l simple let's do that one first because we're going to see this so l simple first let's see if it has a my name now we also had it in el simple we had an address so what if i do address and let's see what it comes back with [Music] and so we'll see that it does have an address it does have a my name so i could dynamically figure out what i'm doing and say hey if it has it then i'm going to assign it to something or i could say if l simple has let's say display well let's see let's do that let's now look at those are attributes let's see what happens when we do methods so let's do a method up at this level so let's do uh just don't get rid so let's do my address and then let's see if he has a dump or if he is dope and yes they do so now so i could say if he has that then print simple.dump and boom there i get my my dom i've dumped that out so i can actually determine what's out there now an interesting thing we can do is i can also do a get and a set so let's say so i can do so now let me do git attribute dump and then let me get attribute um what was that address let's go look at those two and so we'll see here when i do a git dump then he's going to get he tells me that there's this method less simple.dom that i've gotten and if i do get attribute address then it's just going to print out you know that address value now i could also do um so i can do uh let's see oops so i can set an attribute so i could actually come in let me do address i'm gonna get that twice so i can also go in and just say i'm going to set attribute um address and i'm going to call it new value so let's go look at that real quick and we'll see that now new value is what the value is so i could do that there's either easier ways to do it but you can do it that way now let's see what happens if i try to set an address a value that doesn't exist so let's say uh city goes to new york and then i try to get city boom i just extended my class by doing a set attribute so if i get one that exists then i'm good um then i you know then i'm i'm just working with that but i can also set it so if you went back to lambdas where oh let's see so if i create a lambda and now i do um i'm just going to call it new funk and now i'm going to set an address uh set attribute l simple add one and then give it new funk and then print uh string l simple dot new funk one i'm sorry it's not called new funk it's called add1 let's see what happens bam we just added a function so we just created a function on the fly and then added it to our class we have which is one of the nice powerful things that we have in python is we can actually create classes on the fly and dynamically make changes to them as we just did so that gives us our has get and sets allows us to do some pretty cool stuff so there's some great ways that we can make uh you know get complicated with their code but for now i think that makes most sense for us to stop i don't want to get too much deeper in it and that also covered the functions i want to cover today so classes in python pretty cool uh we will get into some more talking about modules and things like that later on but i think you probably have seen now we're getting into some of the more uh one the more powerful pieces of python maybe not used as much but that also means you know play around with them pay attention to them because you're more likely to see those in a certification because they want to make sure that you've been exposed to all these different ideas that being said we'll wrap it up so 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 continuing looking at examples
for python certification
and this episode we're going to deal
with we're going to swing back around a
little bit to classes
and this time we're going to look at
some of the uh we'll call them the
the type related functions for classes
now to do this we've got two classes
that we've declared we've seen these
before
how we've looked at the inheritance
we've got simple
and then we have simple which inherits
from simple so very simple
not to get too caught up with that word
but we have
a straightforward hierarchy you have
simple
and then you have less simple which
inherits from
simple so it looks something like
uh if you have like a it should be
something like this
you know basically that becomes less
simple
so just to remember that let's put that
there now what we're going to do
is we're going to create one of each
just because
and the first thing we want to look at
and actually let's just do
a um this will just be
value equals uh abc
and we're gonna do value two equals one
two three
now let's do that one two three as well
and then actually let's do value
three equals uh
let's see one comma two comma three
and now the first one we're gonna look
at is type
so what i'm gonna do is take each of
those
and i'm going to do type
of simple
well i'll do it again and again again
and so it's going to be l simple
value
value 2 and value
three so let's look at each of these
just do that clear it up let's type
functions there we go let's see
oh because we've got a random in there
somewhere
for that id let's just get rid of that
okay so we can see here
uh in order so simple is a class that's
defined in main
that's simple less simple
is going to be whatever the which is the
class that it is declared of
so that is the type and we can see that
we can send a string
uh here uh value so it tells us that's
an str
tells us this is an int this
is a tuple and actually let's go ahead
and do
just to wrap these up give us a couple
different things let's do
one point two three
it's gonna be value four two three and
four and let's see what it does for that
decimal
and we'll see here that that's a float
and so type
tells us the type uh
what if it's a native type it'll give us
like string integer tuple whatever if
it's a class it's going to tell us what
the class type is
now it does tell us where the module is
that it is uh that's been declared and
in this case it's main we could could
import it from elsewhere
i'm not going to mess with that we'll
actually cover modules in a different
episode and
probably see it then so let's look at is
instance
now this one becomes um let's do it this
way
is instance examples
so let's start with print
uh let's see simple oh sorry
is instance
and so is instant let's see is simple
going to be an instance of simple
and then let's say what happens is
l simple and it's not so simple
and then let's do those two same ones
as less simple
let's see what it tells us
oop there we go and so we get true true
false true
so true true let's see so i'll say like
true
true that's false
and that's true so simple is an instance
of simple
because we can tell right up here
that it is but it also less simple
is going to be an instance of simple
because it inherits
remember less simple comes from samples
because it's a child
now simple is not
an instance of less simple it if it's
further down the hierarchy then it's not
going to
count so you can't cover it there and
then or you know
it's not it's not an instance of that it
only works down the hierarchy not back
up the hierarchy
and then of course less simple is also
an instance of
less sub or l simple as an instance of
less simple because
that's directly what we're creating it
so notice that that l
simple is at the same time
an instance of simple and also an
instance of less simple
so those two are pretty straightforward
let's do
let's try this one so now let's do
is instance ah let's see
let's do value let's see
is it a string
and then let's see is it an ant
and let's see
let's see what comes up here
so value is uh it's a string one two
three
now let's see what it tells us it's an
instance of
and there you go the natives examples it
is an instance of a strain but
even though one two three is an
int you know we can convert it to such
it's not an instance but if we were to
go
do this
then it's true so then we've just
converted it and said nope that's going
to be our instance
so let's look at der let's see
so let's print
and let's do simple uh
let's try that so let's do uh the
instance simple
and see what happens
this is going to be tour examples
let's clear this
and run it and so here when we run
simple
this is everything that is a part of
what we see is everything that's a part
of this simple class so we're going to
see
dump we're going to see display my name
value
id so let's see so it has
and these are all things that we can
call
on simple so we can get an id
we get a class we can delete an
attribute
we get a dictionary dir doc quit forma
blah blah blah we get all these and then
let's see somewhere down here oh here we
go
and then we get uh i think we call it my
name and value
yeah so we see my name and value there's
my properties
and i did display and dump there's my
the
um the methods i specifically created
plus
a knit it's probably going to be in
there somewhere there we go
but i could call any of these now some
of these we've seen
to some extent but we're going to
continue playing around with these a
little bit so let me look at like
so what happens
if i do
simple dot underscore str
let's see what that does it gets the end
and it just says
it's a simple object which is
interesting but what i can do
is i can actually define that
[Music]
at this level and i can say
uh so oop self and then
he's just gonna now he's gonna return
instead of that which you would
recognize if you've done this
uh a lot of time like in java or c-sharp
stuff like that there's a lot of times
that we create
a essentially a two-string kind of
method so that we can convert it to a
string and so
i would say here if i do self dot my
name
and then i'm just gonna do it like this
self.value
and now if i do that there we go default
and default
so now i've overwritten what you can do
we've talked about that to some extent
you can override things
so i've gone in and overridden the
string method
and i can do this with others so i can
actually come in and do things like
comparators
so i can do greater thans and greater
than equals
uh let's look at
let's look at our class so
now we could since this is an array
what i can do is i can do
[Music]
well this will probably blow up because
a lot of those don't exist but i could
actually map them
if i wanted to i could pray map each of
those but i'm not going to because those
are not
i'm not going to be able because i'm not
going to have the same uh
variables parameters for each of those
but let's do
i do want to do that let's do let's
print a couple of these out so we can
see what they look like so
remember this is at the simple level
uh let's do class
and let's do size of oh that'd be
another good one
[Music]
now let's just see what it comes up with
on these
so let's see so class
see string clad oh yeah
simple blah blah blah oh here we go
so here uh strimple does that
clap whoops but class is actually going
to generate
an instance of that and then
size of is just going to give us it's
basically this is its size in memory so
there's a bunch of little things we can
do
playing around with this uh the
important thing is that we can come in
we can see if
things exist
and sort of dynamically create things
that's our
dir so let's do a has attribute
and so let's do that
so now let's do
[Music]
let's see if simple has an
attribute called
value called my name
actually let's do l simple let's do that
one first because we're going to see
this
so l simple first
let's see if it has a my name now we
also had it in el simple we had an
address
so what if i do address
and let's see what it comes back with
[Music]
and so we'll see that it does have an
address it does have
a my name so i could dynamically figure
out what i'm doing and say hey if it has
it then
i'm going to assign it to something or
i could say if l simple has let's say
display
well let's see let's do that let's now
look at those are attributes
let's see what happens when we do
methods so let's do a method up at this
level so let's do uh
just don't get rid so let's do my
address
and then let's see if he has a dump or
if he is dope
and yes they do so now so i could say
if he has that then
print simple.dump
and boom there i get my my dom i've
dumped that out
so i can actually determine what's out
there now an interesting thing we can do
is i can also do a get and a set so
let's say so i can do
so now let me do git attribute dump
and then let me get attribute um
what was that address
let's go look at those two and so we'll
see here
when i do a git dump then
he's going to get he tells me that
there's this
method less simple.dom that i've gotten
and if i do get attribute address then
it's just going to print out
you know that address value now i could
also do um
so i can do uh let's see
oops so i can set an attribute so i
could actually come in let me do
address i'm gonna get that twice so i
can also go in and just say i'm going to
set attribute
um address and i'm going to call it
new value so let's go look at that real
quick
and we'll see that now new value is what
the value is so i could do that there's
either easier ways to do it but
you can do it that way now let's see
what happens
if i try to set an address a value that
doesn't exist
so let's say uh city
goes to new york and then i try to get
city
boom i just
extended my class by doing a set
attribute
so if i get one that exists
then i'm good um then i you know then
i'm
i'm just working with that but
i can also set it so if you went back to
lambdas
where oh let's see
so if i create
a lambda and now i do
um i'm just going to call it new funk
and now i'm going to
set an address uh set
attribute l simple
add one and then give it new funk
and then print
uh string l simple dot
new funk
one i'm sorry it's not called new funk
it's called add1
let's see what happens
bam we just added a function
so we just created a function on the fly
and then added it to our class
we have which is one of the nice
powerful things
that we have in python is we can
actually create classes on the fly
and dynamically make changes to them as
we just did
so that gives us our has get and sets
allows us to do some pretty cool stuff
so there's some great ways that we can
make uh you know get complicated with
their code
but for now i think that makes most
sense for us to stop i don't want to get
too much deeper in it
and that also covered the functions i
want to cover today so classes in python
pretty cool uh we will get into some
more talking about modules and things
like that later on
but i think you probably have seen now
we're getting into some of the more
uh one the more powerful pieces of
python maybe not used as much
but that also means you know play around
with them pay attention to them because
you're more likely to see those in a
certification because they want to make
sure that you've
been exposed to all these different
ideas that being said
we'll wrap it up so go out there have
yourself a great day a great week
and we will talk to you next time
you