📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Classes

2021-08-10 •Youtube

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
0.46

[Music]

26.32

well hello and welcome back

28.24

we're considering our series on python

31.039

certification

32.559

and we're going to start into now

34.719

looking at classes

36.239

within python and so we're going to

39.28

dive into it today get some basic

40.879

definitions whoops and

42.719

um then we're gonna go forward into uh

44.879

we do inheritance and a lot of other

46.719

good stuff so though

48

there'll be several class related uh

50.96

python class

52

and object related discussions uh but

55.199

this is the

55.84

uh and this is the first one we're just

57.039

going to get started so

60.239

right now we've got a very simple class

62.719

when you want to create a class

64.32

in python is you just use the class

66.88

keyword

67.76

so i've got a class i'm going to call it

69.84

simple

71.6

and now what a class can do and without

74.88

getting too deep into it

77.6

and there'll be some show notes some

79.439

links if you want to go a little deeper

80.64

in classes but classes have attributes

82.64

and they have methods

84.24

or properties and methods so what i've

87.439

created here

88.64

is i'm going to do this this will be a

90.32

class properties

93.28

and then i'll come down here i do class

98.84

methods

100.72

and let me do this just and i'm also

103.52

gonna do

104.24

uh define display

110.96

value print

118.32

different display

122

plus value okay we'll keep that very

124.399

simple

126.32

no pun intended all right so

129.52

let's see what it complains about here

137.92

oh okay it wanted an extra line in there

140.239

for formatting which is fine

142.239

um to sort of basically wants this extra

145.12

this is a formatting thing around the

146.4

class itself anyways

147.76

back to that so a class has properties

151.519

which

151.84

are things which are values that a an

154.879

instance stores and they have methods

158.239

which are

159.36

functions essentially that you can call

161.44

on

162.56

that class and its properties in this

165.28

case we've got a very simple prop we've

166.879

got one property that we're just going

167.92

to call it my name

170.239

and we're going to define it and we're

171.36

going to assign it a value here by

173.04

default

174.72

and then we've got one method which is

176.08

display and it's going to display

179.28

it's going to print out my name now

181.04

notice here

182.64

we have our sort of normal um

186.08

definition we have that basically built

188.08

in so we have def

189.44

space and then the name but any class

192.879

level

194.48

method is gonna have it's gonna start

196.08

with self and so

198.48

i'm going to actually put a second one

200.159

here to show how this works so if i do

202

display

203.84

2 count my value

212.239

and here is the value

216.48

and we'll just force it to string

217.84

whatever it happens to be

224.319

so both of these are methods display and

226.239

display two are both methods

228

of the simple class and

231.04

in the one case we don't send it we're

233.04

not gonna send it anything we'll see how

234.4

that looks in a second

235.36

and the other we're gonna send it a

236.4

value and so to create

239.599

an instance well let's first do this

242.72

um let's move this up

250.08

so if we just take simple the class and

252.799

try to call display

255.439

which does not have a self we are going

258.239

to systems class example

263.12

uh three

266.639

class example and we're going to see

269.199

here that it's missing

270.639

a positional argument itself

274.32

so it requires something however

278.72

let's do

283.03

[Music]

284.32

let's create a variable of type class

287.44

which is as easy as this so we just do

289.68

simple and we're going to do simple

291.28

get the class name notice that we've got

293.44

the case sensitivity there

295.36

and then if we use a class name with no

297.6

parameters

298.56

by default that's going to use the

299.759

default constructor we'll talk about

301.919

those a little bit later

304.32

and let's go ahead and then and then

308.56

on that variable of class you know type

311.44

class

312.639

simple now we're going to call it and

314.72

notice we're still not going to send a

316.24

parameter and let's see what that does

319.28

and now we see that it works it says i

321.44

am simple because by default

324.16

what it's going to do or the way it's

325.759

constructed

328.08

is self means i need to call this on

331.12

an instance i need to create a variable

333.12

an instance of the class

334.4

first and then do it so if i create one

338

more if i just do a static

342.08

here i'm going to say print

347.759

let's see what it says

354

and so now if i do simple.display

358.8

it's probably going to complain and it

360.08

complains here

363.36

you see that a simple.display it

365.199

requires a positional argument of self

367.28

oh

368

probably because let's do

371.36

display three

374.56

just to be sure that we're calling the

376.96

right one

381.36

and uh let's do we'll deal with display

384.319

two in a minute

386.8

so display three so now

391.919

we can see i can't do display without

394

any because

395.44

it actually ends up overwriting so if i

397.199

do this

398.56

what happens is further down i'm

401.199

basically redeclaring this now

403.36

with a new signature but if i call this

406.96

display three

409.12

then i can actually call that on the

411.36

class and this may even show

414.56

so i can oh man it doesn't help me which

417.12

is static and which is not

419.84

so this would be something i can call at

421.919

the class

423.12

but if i try to do

427.44

france dot name

432

or i'm sorry my name

435.039

then it's going to give me an error

436.72

because

439.36

self is not defined it's not defined for

443.36

the class level it's only at an instance

446.24

so i have to have an instance in order

447.52

to have properties

450.56

so i can do some stuff up at store

453.759

functions up on a class but and they're

456.639

called static

457.599

because those are at the class level

459.199

those are

460.88

any class any the class will always have

464

those available

467.039

however it cannot use the properties

469.44

within the class

470.4

because those require an instance

474.56

so let's go back and look at this so uh

477.44

i don't want to cover this

478.479

is that notice that because this is my

481.44

name

482.319

self also if you've done if you come out

484.319

of the world of like a um

486.16

java or c sharp you may think of that

489.199

also as

489.84

uh this i think visual basic maybe uses

493.44

self

494.8

but basically it says that i want to go

496.56

to whatever this instance is

497.919

i want to display the property value

501.44

for this instance and now we can come

504.84

here

506.479

and if we do display two i'm gonna show

508.72

that one real quick

511.52

now here because this self is already

513.919

part of it because i'm doing

514.919

simple.display2

516.08

then i just give it a value so i'm going

517.519

to give it a value of

521.919

my value

525.12

and then we're going to see that this

526.32

one comes through i am simple and here's

529.04

a value

530.88

and you can see that it's my value so

534.32

i use self to get the the instance level

537.92

but i can use the if i don't use self

540.72

it's going to take the

541.76

local value so if i add which can

544.399

sometimes get confusing so if i do value

546.839

equals let's see

549.6

first value

552.64

then what we'll see here uh let's see

557.44

and let's just do print

560.88

self.value well

568.48

value okay

571.76

so what we're going to see here is it

574.839

does what we originally saw

577.44

so it does take value it uses this value

580.24

here but i can distinguish i want to use

581.92

the property level value

583.68

here and i get where it says first value

588.88

so that gives me uh properties

592.48

and the ability to access that access

595.36

them

597.12

now i can you may see so far okay

600.16

cool what good does any of this do for

602.64

me because

603.6

well let's say you know let's say i put

606.16

all this stuff together

606.959

why do i have a class instead of just

609.6

leave these as functions and

611.36

variables well what i can do here is i

613.92

can do

614.88

simple 2 equals simple

619.68

and now this is where i start working

621.04

with it so now i can actually i can

622.8

access these properties so i can do

624.56

simple

625.279

two dot my name

629.6

equals let's see

632.88

second instance

637.76

and now if i do simple two dot display

645.44

then i'm gonna see something different

647.279

uh let's just do

648.88

this uh

653.68

let's get a couple spaces in there just

655.92

cut

656.88

so now the second instance we've gone in

658.959

and we created it

660.32

we have another instance so we have

661.76

simple and simple one

663.36

now this time we're gonna go in and

664.48

we're gonna set the name on simple two

666.16

to something different

667.519

and then do a display and we'll see here

670.399

boom

670.88

second instance so we could also do

674.959

um sample 2

679.2

dot value equals

684.24

another value and then

688.72

if we do this

695.76

uh let's just leave it my value because

697.6

i'm passing the same value in

699.839

then we're gonna see here so second

701.68

instance and here's my value and we can

703.839

see

704.16

another value there for the value

707.76

because we've set that out we've

709.279

assigned that

711.279

so we have the way i have the ability to

714.24

work with these things uh

716

as an instance you know individually so

717.68

we can work with one for a while we can

718.959

work differently with another

720.32

we have a series so we could use these

722.16

to store things

724.079

so we could have like uh let's say we

725.44

had an address class that would have a

727.279

city state zip

728.72

and then we can do stuff with it and we

730.32

could actually have multiple addresses

732.399

and then we can do things like let's say

740.72

i want to do just print

745.519

and here i'm going to print

749.279

which is basically the same as well

750.56

we'll just keep it as display

755.279

so now i could do let's

759.04

split this guy up real quick and i'm

762.079

gonna do

762.56

simple three

770.399

and then simple three dot

774.8

uh oh i have to spell it right

779.2

at my name equals

783.36

third let's call it i am third

788.56

and then i can do an array so let's just

791.44

do

792.079

uh instances

795.279

equals i call it so

799.2

simple simple two kind of simple three

803.76

and then i can do four item in

807.04

instances

811.36

and i'm gonna be able to call that so

812.639

for each one i can do item.display

817.92

and then we're going to see here at the

819.76

end simple second and third so i can

822.959

wheel and deal with these i can do stuff

825.279

here where i can actually call that

826.639

display

827.279

on each of these now if i wanted to do

830

something that doesn't have a display

832.399

let's do just to show that so if i do

836.72

class uh

839.92

let's see simple call it less simple

846.72

oops sorry i don't need that and then

848.88

i'm just going to call let's see i'm

850.32

going to keep the same values

854.48

let's do it this way let's actually give

857.12

it a display three even

860.399

and i display three we'll give it a

861.6

display too

863.519

so let's say that so let's say that we

867.44

have here

868.88

so less simple is just

874.24

simple

877.279

a simple value

880.639

and so now i can do all of the same

882.24

stuff and i can create

885.199

simple four

891.44

less simple

894.88

and if i try to do it here

899.44

let's see what happens go so note that

902.399

we're calling display but

903.6

simple less simple does not have

908.32

that it does not have a display

912.959

that has been defined for it so here

915.12

boom

916.079

less simple object has no attribute

917.839

display

920

now we're going to find out how we can

921.68

do some things like that and have

923.04

classes relate to each other but that's

924.88

that will be uh next time around i will

927.36

put going to cover that in another one

929.279

i do want to cover

932.72

the init

936.399

um i don't call we'll put it down here

939.279

so if we do define

942.16

and itself

942.91

[Music]

945.36

this is a constructor which means that

948.48

and it's a no argument constructor so

950.16

here when i create

952.88

this i'm sorry when i create when i do

956.959

this

958.16

it fires off a constructor so what i'm

960.399

going to do here

961.199

is i'm just going to say print

966.839

constructor

969.759

called and then let me change these two

972.959

things here so i'm going to change here

974.639

so i'm going to say self

976.639

dot my name so let's say i just

980.639

by default i'm going to set these things

982.079

to some default some

984.32

uh simple values of this is just

987.92

you know i'm clearing this thing out so

989.44

i don't have any values on it

991.44

so now if i do this and run it oh

995.199

i want to get rid of i don't want to

997.04

call you anymore

1002.959

so now we can see here that the

1005.6

constructor is called

1009.12

and then when we come in here and call

1012.8

display if we go back and look at

1015.6

display

1017.04

display just does my name but

1020.48

there's nothing there and then the

1023.519

display and it calls display 2 with my

1025.839

value well my name

1027.28

is blank so see it doesn't have that

1030.959

and here's the value and so that still

1033.12

comes through but if i do the value

1034.799

after that there's a blank

1038.079

so i could do

1041.52

let's just call let's make it a little

1045.039

easier if i do that default name

1046.64

and default value

1050.96

then i'm going to see those default name

1052.88

default name

1054.32

and so i don't have to declare these

1057.36

guys here

1058.96

or i can just say you know something

1061.039

like this i can say this

1062.559

is what i default to and now

1068.16

i can do um well i can also do that one

1072.88

valve two

1076.559

and in this case my name equals

1080.32

l1 value equals val2

1086.08

and so if i want to initialize so now

1089.039

this would be uninitialized

1091.44

but then i can turn around and do

1094.48

and let's do these at the end so let's

1096.32

see let's do

1098.4

let's just do it this way

1102.24

so this is going to be uh default

1106.08

well let's do uninet

1110.559

equals that and then if i display it

1114.799

but now if i do on init

1118.4

but now if i do initialized

1123.71

[Music]

1125.52

now i can give it simple um

1130.559

init name and a net value

1136.32

and then if i do init display

1139.44

uh let's see oh and here's the promise

1143.12

that i don't have both

1145.84

so it doesn't like that so

1150.48

if i do this

1160.24

empty and empty let's see what that does

1165.36

and what you're probably going to see is

1166.48

this going to get overwritten we see

1168

that from time to time

1170.08

uh let's see yeah and so here see here

1173.84

it takes one positional argument but

1175.36

three we're given so we have to do

1177.28

is this goes back to what we saw

1181.84

before

1185.52

is that i need to allow it to be

1190.72

uh let's just do default

1194.4

and default and so now

1197.44

because i've set those up i can actually

1199.6

call both of these

1203.039

and we can see here that here's where it

1204.72

gets the default and here's where it

1206.159

gets to

1206.96

i'm sorry and here is where it gets a

1208.559

net name

1211.2

so when we create a create constructors

1213.28

we're going to basically build some

1214.559

default values

1215.44

out and in this case that allows us to

1217.6

call it with no values or

1220.4

with name values much as we saw when we

1222.48

went into the

1223.919

some discussion about functions so

1226.64

that's the basics

1227.679

of a class in an instance and we're

1229.76

going to get much more into this

1231.679

moving forward but i think that probably

1233.36

is a good stopping point for

1235.12

today so i will let you get back to it

1238.96

as always there'll be uh source code

1240.559

repository is out there we have links in

1242.48

the show notes so that you can get to

1243.84

that

1244.159

you can see these you can play around

1245.679

them and do whatever you want with them

1248.159

we also have links to some additional

1250.4

information if you want to learn some

1251.52

more about classes and python classes

1253.84

in particular if you have any questions

1256.48

as always shoot us an email at info

1258.84

developer.com and that'll wrap it up

1262

so as always go out there have yourself

1264.159

a great day a great week

1265.919

and we will talk to you next time

1284.48

you