📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Class Helper Functions

2021-08-26 •Youtube

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
0.46

[Music]

26.32

well hello and welcome back

27.92

we are continuing looking at examples

31.039

for python certification

33.12

and this episode we're going to deal

34.559

with we're going to swing back around a

36.16

little bit to classes

38.32

and this time we're going to look at

39.68

some of the uh we'll call them the

41.92

the type related functions for classes

45.36

now to do this we've got two classes

48.64

that we've declared we've seen these

49.84

before

51.199

how we've looked at the inheritance

53.28

we've got simple

54.719

and then we have simple which inherits

56.719

from simple so very simple

59.84

not to get too caught up with that word

62.32

but we have

63.28

a straightforward hierarchy you have

66.24

simple

66.72

and then you have less simple which

69.119

inherits from

70.08

simple so it looks something like

73.68

uh if you have like a it should be

75.439

something like this

76.88

you know basically that becomes less

79.2

simple

82.24

so just to remember that let's put that

85.119

there now what we're going to do

87.119

is we're going to create one of each

88.799

just because

91.04

and the first thing we want to look at

92.56

and actually let's just do

94.159

a um this will just be

97.28

value equals uh abc

102.799

and we're gonna do value two equals one

105.52

two three

106.24

now let's do that one two three as well

109.04

and then actually let's do value

110.799

three equals uh

114

let's see one comma two comma three

117.68

and now the first one we're gonna look

119.36

at is type

122.799

so what i'm gonna do is take each of

124.32

those

127.36

and i'm going to do type

131.12

of simple

136.56

well i'll do it again and again again

140.16

and so it's going to be l simple

147.12

value

152.56

value 2 and value

156.48

three so let's look at each of these

164.16

just do that clear it up let's type

167.76

functions there we go let's see

172.239

oh because we've got a random in there

174.319

somewhere

176.16

for that id let's just get rid of that

182.84

okay so we can see here

186.08

uh in order so simple is a class that's

190

defined in main

191.28

that's simple less simple

194.64

is going to be whatever the which is the

197.12

class that it is declared of

198.8

so that is the type and we can see that

202

we can send a string

203.76

uh here uh value so it tells us that's

206.319

an str

208.4

tells us this is an int this

212.159

is a tuple and actually let's go ahead

214.319

and do

215.68

just to wrap these up give us a couple

219.04

different things let's do

220

one point two three

223.519

it's gonna be value four two three and

227.599

four and let's see what it does for that

230.64

decimal

231.44

and we'll see here that that's a float

234.08

and so type

234.879

tells us the type uh

238.159

what if it's a native type it'll give us

241.28

like string integer tuple whatever if

243.28

it's a class it's going to tell us what

244.64

the class type is

245.68

now it does tell us where the module is

249.12

that it is uh that's been declared and

252.159

in this case it's main we could could

255.04

import it from elsewhere

256.32

i'm not going to mess with that we'll

257.359

actually cover modules in a different

259.359

episode and

260.239

probably see it then so let's look at is

263.759

instance

265.84

now this one becomes um let's do it this

269.28

way

270.88

is instance examples

275.68

so let's start with print

280.639

uh let's see simple oh sorry

285.04

is instance

288.32

and so is instant let's see is simple

291.68

going to be an instance of simple

295.759

and then let's say what happens is

300.16

l simple and it's not so simple

304.56

and then let's do those two same ones

309.36

as less simple

312.4

let's see what it tells us

315.52

oop there we go and so we get true true

319.199

false true

320.16

so true true let's see so i'll say like

323.52

true

326.32

true that's false

330

and that's true so simple is an instance

333.6

of simple

334.479

because we can tell right up here

338.32

that it is but it also less simple

342.639

is going to be an instance of simple

344.08

because it inherits

345.84

remember less simple comes from samples

347.52

because it's a child

349.44

now simple is not

353.36

an instance of less simple it if it's

356.16

further down the hierarchy then it's not

357.759

going to

358.16

count so you can't cover it there and

360.96

then or you know

362

it's not it's not an instance of that it

365.12

only works down the hierarchy not back

366.88

up the hierarchy

368.16

and then of course less simple is also

370.08

an instance of

371.84

less sub or l simple as an instance of

373.919

less simple because

375.68

that's directly what we're creating it

377.52

so notice that that l

379.199

simple is at the same time

383.039

an instance of simple and also an

385.199

instance of less simple

387.039

so those two are pretty straightforward

388.639

let's do

390.319

let's try this one so now let's do

395.36

is instance ah let's see

398.96

let's do value let's see

402.08

is it a string

405.6

and then let's see is it an ant

414.16

and let's see

418.479

let's see what comes up here

421.68

so value is uh it's a string one two

425.039

three

425.44

now let's see what it tells us it's an

427.84

instance of

428.8

and there you go the natives examples it

431.84

is an instance of a strain but

433.759

even though one two three is an

436.96

int you know we can convert it to such

439.68

it's not an instance but if we were to

441.28

go

441.599

do this

445.84

then it's true so then we've just

448.24

converted it and said nope that's going

449.599

to be our instance

454.88

so let's look at der let's see

458

so let's print

465.52

and let's do simple uh

469.919

let's try that so let's do uh the

472.319

instance simple

473.28

and see what happens

479.12

this is going to be tour examples

484.4

let's clear this

487.84

and run it and so here when we run

491.44

simple

494.4

this is everything that is a part of

497.759

what we see is everything that's a part

499.44

of this simple class so we're going to

500.879

see

501.199

dump we're going to see display my name

503.919

value

504.479

id so let's see so it has

508.16

and these are all things that we can

510.96

call

512.159

on simple so we can get an id

515.68

we get a class we can delete an

518

attribute

518.88

we get a dictionary dir doc quit forma

521.44

blah blah blah we get all these and then

523.039

let's see somewhere down here oh here we

524.88

go

525.519

and then we get uh i think we call it my

528.88

name and value

529.76

yeah so we see my name and value there's

531.76

my properties

533.12

and i did display and dump there's my

535.92

the

536.32

um the methods i specifically created

539.44

plus

540.32

a knit it's probably going to be in

541.519

there somewhere there we go

545.12

but i could call any of these now some

547.76

of these we've seen

549.12

to some extent but we're going to

551.279

continue playing around with these a

552.56

little bit so let me look at like

554.399

so what happens

557.92

if i do

562.399

simple dot underscore str

567.6

let's see what that does it gets the end

570.8

and it just says

571.68

it's a simple object which is

573.839

interesting but what i can do

576.8

is i can actually define that

579.42

[Music]

580.72

at this level and i can say

585.76

uh so oop self and then

589.44

he's just gonna now he's gonna return

591.04

instead of that which you would

593.04

recognize if you've done this

594.56

uh a lot of time like in java or c-sharp

596.959

stuff like that there's a lot of times

598.08

that we create

599.68

a essentially a two-string kind of

601.519

method so that we can convert it to a

602.959

string and so

604.32

i would say here if i do self dot my

607.36

name

610.88

and then i'm just gonna do it like this

618.68

self.value

621.6

and now if i do that there we go default

625.12

and default

626

so now i've overwritten what you can do

629.36

we've talked about that to some extent

630.72

you can override things

632.88

so i've gone in and overridden the

635.279

string method

636.48

and i can do this with others so i can

637.839

actually come in and do things like

639.2

comparators

640

so i can do greater thans and greater

641.76

than equals

643.839

uh let's look at

647.36

let's look at our class so

652.399

now we could since this is an array

657.36

what i can do is i can do

661.68

print

665.19

[Music]

666.48

well this will probably blow up because

668.24

a lot of those don't exist but i could

669.68

actually map them

670.64

if i wanted to i could pray map each of

672.079

those but i'm not going to because those

673.12

are not

674.079

i'm not going to be able because i'm not

675.12

going to have the same uh

677.279

variables parameters for each of those

679.839

but let's do

680.56

i do want to do that let's do let's

683.44

print a couple of these out so we can

684.64

see what they look like so

685.92

remember this is at the simple level

688.959

uh let's do class

692.56

and let's do size of oh that'd be

694.959

another good one

695.64

[Music]

697.279

now let's just see what it comes up with

699.2

on these

711.92

so let's see so class

715.839

see string clad oh yeah

721.839

simple blah blah blah oh here we go

724.959

so here uh strimple does that

728.079

clap whoops but class is actually going

730.72

to generate

732.639

an instance of that and then

735.92

size of is just going to give us it's

738.72

basically this is its size in memory so

740.56

there's a bunch of little things we can

741.68

do

742.56

playing around with this uh the

744.399

important thing is that we can come in

746.079

we can see if

747.92

things exist

752.16

and sort of dynamically create things

754.399

that's our

755.44

dir so let's do a has attribute

759.279

and so let's do that

772.32

so now let's do

775.55

[Music]

779.36

let's see if simple has an

782.72

attribute called

785.76

value called my name

790.48

actually let's do l simple let's do that

793.12

one first because we're going to see

794.32

this

795.44

so l simple first

798.88

let's see if it has a my name now we

800.959

also had it in el simple we had an

803.2

address

805.68

so what if i do address

814.24

and let's see what it comes back with

816.54

[Music]

818

and so we'll see that it does have an

820.56

address it does have

822

a my name so i could dynamically figure

826.079

out what i'm doing and say hey if it has

827.6

it then

828

i'm going to assign it to something or

831.68

i could say if l simple has let's say

834.48

display

837.68

well let's see let's do that let's now

839.519

look at those are attributes

840.959

let's see what happens when we do

844.639

methods so let's do a method up at this

846.639

level so let's do uh

848.079

just don't get rid so let's do my

849.92

address

855.36

and then let's see if he has a dump or

857.68

if he is dope

861.44

and yes they do so now so i could say

865.12

if he has that then

868.839

print simple.dump

875.519

and boom there i get my my dom i've

878.32

dumped that out

879.6

so i can actually determine what's out

881.279

there now an interesting thing we can do

883.44

is i can also do a get and a set so

885.519

let's say so i can do

899.04

so now let me do git attribute dump

902.639

and then let me get attribute um

906

what was that address

910.079

let's go look at those two and so we'll

912.8

see here

918.399

when i do a git dump then

922.16

he's going to get he tells me that

923.92

there's this

925.199

method less simple.dom that i've gotten

928.56

and if i do get attribute address then

931.519

it's just going to print out

933.36

you know that address value now i could

935.759

also do um

940.48

so i can do uh let's see

944

oops so i can set an attribute so i

948.32

could actually come in let me do

949.759

address i'm gonna get that twice so i

952.639

can also go in and just say i'm going to

954.16

set attribute

956.959

um address and i'm going to call it

962.32

new value so let's go look at that real

965.68

quick

966.88

and we'll see that now new value is what

969.04

the value is so i could do that there's

970.639

either easier ways to do it but

972.639

you can do it that way now let's see

973.839

what happens

976.56

if i try to set an address a value that

978.88

doesn't exist

980.72

so let's say uh city

984.8

goes to new york and then i try to get

988.16

city

992.8

boom i just

996.24

extended my class by doing a set

999.36

attribute

1000.32

so if i get one that exists

1003.839

then i'm good um then i you know then

1006.72

i'm

1007.12

i'm just working with that but

1011.36

i can also set it so if you went back to

1014.48

lambdas

1017.759

where oh let's see

1025.199

so if i create

1031.36

a lambda and now i do

1034.959

um i'm just going to call it new funk

1040

and now i'm going to

1045.36

set an address uh set

1050.72

attribute l simple

1054.72

add one and then give it new funk

1062

and then print

1067.039

uh string l simple dot

1072

new funk

1076.32

one i'm sorry it's not called new funk

1080.32

it's called add1

1083.28

let's see what happens

1088.799

bam we just added a function

1092.24

so we just created a function on the fly

1094.72

and then added it to our class

1096.24

we have which is one of the nice

1098.48

powerful things

1099.919

that we have in python is we can

1101.679

actually create classes on the fly

1104

and dynamically make changes to them as

1106.64

we just did

1110

so that gives us our has get and sets

1114.24

allows us to do some pretty cool stuff

1118.16

so there's some great ways that we can

1119.52

make uh you know get complicated with

1121.52

their code

1122.799

but for now i think that makes most

1124.32

sense for us to stop i don't want to get

1126

too much deeper in it

1127.36

and that also covered the functions i

1128.88

want to cover today so classes in python

1131.36

pretty cool uh we will get into some

1133.44

more talking about modules and things

1135.039

like that later on

1136.64

but i think you probably have seen now

1139.2

we're getting into some of the more

1140.96

uh one the more powerful pieces of

1142.799

python maybe not used as much

1145.039

but that also means you know play around

1147.52

with them pay attention to them because

1149.36

you're more likely to see those in a

1150.64

certification because they want to make

1152.72

sure that you've

1153.919

been exposed to all these different

1155.28

ideas that being said

1157.36

we'll wrap it up so go out there have

1159.12

yourself a great day a great week

1161.28

and we will talk to you next time

1179.28

you