declare

Classes and Objects with Python - Part 1 (Python Tutorial #9)



Sharing buttons:

hey guys in this video I'm gonna give

you a quick introduction on how to use

classes and objects in Python I'm going

to assume that you're already familiar

with these concepts so if you're new to

you know the concepts of classes and

objects

I'd recommend my introduction to classes

and objects video first okay so here's

the class we want to create it's gonna

be called robot and it will have three

attributes or three instance variables

name color and weight those are the name

of the robot the color of the robot and

weight of the robot and then it's gonna

have one function into yourself where

when you run this function it'll just

print out my name is the name whatever

the name is and from this class we want

to create these objects the first object

will look like this it's gonna have the

name Tom the color is gonna be read in a

string and the weight will be 30 to show

that it's 30 pounds it's gonna be an

integer and we're gonna put this object

in a variable called r1 and the second

object we're gonna create will look like

this this robot will have the name Jerry

the color will be blue and weight will

be 40 and then we're gonna put this

object in a variable called r2 so let's

now see how we can create this class

called robot and these two objects out

of that class in Python the first thing

we need to do here is of course we need

to create a class for that you can just

write class robot :

and notice here that our here is

capitalized and then for spaces after

that and we're gonna implement the

function we saw earlier in to do itself

for that you can just write def

introduce underscore self parentheses :

and four more spaces after that and then

print double quotes my name is plus self

dot name and this keyword here self is

sort of like this in Java and it will

refer to whatever object we're running

this function on so for example

if we run this function engine yourself

on an object whose name is Tom this is

gonna print out my name is Tom because

self will refer to that object now this

function you know if you see it looks

like a regular Python function we're

defining a function called introduce

self and then below that we have some

implementation but actually when this

function is within a class when this

function is a method of a class you need

to add an additional argument and that's

going to be call itself so this self is

exactly the same as this self right here

so in Python basically you need to add

this additional argument called self to

every method you want to add to this

class and once you run this block of

code this class is gonna be created and

you might say what about the attributes

like the name weight and color we're

gonna worry about that later so let's

run this block of code for now now that

we have defined this class we'll be able

to create an object out of this class to

do that you can just write our 1 equals

robot parentheses again note that the

word robot is capitalized here and this

simply says create a new object with the

class robot and here you're using the

default Python constructor for this

class robot and to set the attributes

you can just write or one dot name

equals Tom I want the color equals red

and r1 dot weight equals 3 and when you

run this so a new object has been

created it's assigned to r1 and then its

attributes have been set and then if you

want to run this function into yourself

on this object you can just right or one

dot introduce self parentheses and you

don't need to pass in any arguments here

and when you run this cell what's gonna

happen is it's gonna go into this part

of the code and then self will refer to

r1 and so this part of the code will

look like print my name is r1 dot name

and r1 dot name is of course Tom here so

it should print

my name is Tom let's see if it works and

I did we see my name is Tom right here

and just like that you can create

another object - let's go back into this

cell let's write our two eCos robots

this will create a new robot object and

then let's set its attributes to so our

two names echoes Jerry our two the color

equals blue and our 2.8 equals 30 let's

actually set the weight of our two to 40

like we saw earlier and once you run

this cell you'll have an object in r1

and another object in r2 with different

sets of attributes of course and then at

this point you should be able to run or

what introduce self and our to the

introduced self so the first line should

print my name is Tom just like we saw

earlier and then the second line should

print my name is Jerry instead so let's

see if we can get that and that's

exactly what we see okay going back a

little bit when you see this block of

code that we use to create these two

objects you might say well that's not

great because here we're writing our

want name equals Tom but if we may spell

the word name for example if we wrote

our one dot name e equals Tom it's gonna

stop working because if you look at this

function in particular in this class

we're writing here print my name is self

dot name and this piece of code

basically depends on us writing the

attributes name name correctly for it to

work what I mean by that is for example

you know if you misspelled the word name

by writing R 1 dot name equals Tom the

attribute called name does not exist in

this object anymore so we can actually

see what's gonna happen if we try that

so let's run this cell again and this is

not gonna give us any error because when

you write R 1 dot name equals Tom the

attribute called name E is just defined

and then the value Tom is

set for that but when you run this block

of code or Wanda introduce self let's

comment out the second line and let's

focus on the first line when you run

this part you'll say attribute error and

it says Robert as an object has no

attribute name and that's because it has

the attribute name e but it doesn't have

the attribute name so this is probably

not the best way to deal with different

attributes we have let's see how we

might be able to fix that and the way

you can fix it is basically by using a

constructor and in Python you can create

a custom constructor with this keyword

underscore underscore in it and ask

underscore underscore so there's the

word in it and it's surrounded by pairs

two pairs of underscores and then

parentheses and let's have this function

take three arguments give a name given

color and given weight so these are the

name color and weight we want to set for

this particular object that we are

trying to create and then we can write

here self dot name equals give a name

self dot color equals given color and

self dot weight equals given weight and

this block of code of course says set

this object that we are just creating

set that object's name to give a name

and set that object's color to given

color and set the object's weight to

given weight and earlier I said you need

to add this self keyword this additional

argument to every function we define in

this class and the constructor is no

exception so you actually need to write

self as the first argument and then the

arguments that you want to add in and

actually just like many other languages

a common practice here is to name these

arguments exactly the same as the

attributes that we want to set you don't

have to do it but that's just a style

thing so let's just do that

let's change give a name to name

and then given color to color and given

weight to wait and once you run this

block of code this class is going to be

refreshed with a new constructor once we

have a custom constructor in our class

the default constructor that we used

will stop working so let's just comment

out this block of code and then let's

rewrite it using our new constructor for

that you can just write R 1 equals

robots parentheses Tom comma red comma

30 let's capitalize Tom here and then R

2 equals robot Jerry comma blue comma 40

and these two pieces of code do exactly

pretty much the same thing as what we

saw earlier but as you can see this is

much cleaner because you don't need to

specify the attribute names manually

every time and just to reiterate just to

sort of clarify this line R 1 equals

robot Tom red 30 will create a new robot

object and then it'll set Tom as its

name because Tom will go in here as the

name argument and then LLL go in here

self dot name and self again will refer

to the object that we're creating so

it's sort of like saying R 1 dot name

equals name or one dot color equals

color just like we did earlier here and

of course it's the same thing with our

two okay and to make sure we don't have

anything left from the previous code we

were using let's go to kernel and click

restart and clear output this is gonna

clear out every variable we define

earlier and then let's load this class

again and let's run this cell and at

this point we should have r1 and r2

objects defined so we should be able to

run our one dot engine yourself and our

tada introduce self and that should

print exactly what we saw earlier my

name is Tom and my name is Jerry so

let's see if that works

and it does all right you'll be able to

find links to the sample code I used in

this video in the description below and

if anything was unclear in this video

please let me know in a comment below as

well because you know I might be able to

adjust that in the next video and in my

next Python tutorial video I'm planning

to cover how multiple classes and

objects can interact with each other in

Python so look out for that as well and

I'll see you in the next video