[inaudible]
So here's a blueprint for a car.
And that blueprint that specifies what the color of the car is,
how many wheels it should have, what its mileage is, how much fuel it has,
all of those bits of data combined with all of its functionality
like the ability to drive, the ability to stop and break.
And that blueprint which models a real-life car is known as the class.
And it's from this blueprint,
this class, that we can generate as many objects as we want. Now,
the object is the actual thing that we're going to be using in our code.
The code equivalent of what just happened,
creating a new object from a blueprint,
looks like this in Python. You have the class
which is normally written with the first letter of each word capitalized,
which is known as Pascal case.
This is to differentiate it from all the variable and function names that we
give in Python, where each word is separated by underscores.
So in this case,
the car is the object and it gets created from this car blueprint.
All we have to do to create the object from the class is to give the object a
name, it can be anything you want, set it equal to the name of the class,
and then the parentheses, which in the same way as it activates the function,
it activates the construction of this object from the blueprint.
Now, in order for us to practice this and to see it in action,
we're going to using a library of code that somebody else has created.
And what it allows us to do is to finally start putting graphics onto the
screen. The premise is really simple.
Let's say that somebody somewhere has created a blueprint for a turtle.
Now this turtle has a paintbrush on its back,
and you can tell the turtle to dip the paintbrush in different colors,
pick a different of size paint brushes and most importantly,
you can tell it to draw onto the screen.
This library is called turtle graphics and it's preloaded with every download of
Python so we can get started with it straight away.
So go ahead and open up PyCharm and create a new project.
Now I'm going to call mine, buy the old convention day 16 start
and making sure that I'm on the latest version of Python in my base interpreter,
I'm going to click create. Now, once you've created the new project,
go ahead and create the main.py as we have always done.
In our case,
we're going to be creating an object from a blueprint that somebody else has
already created,
and the blueprint lives in another module called turtle.
So we'll have to import that module.
And we know that from previously,
we can import other modules and use the things that are declared inside the
module by importing it, and then tapping into various things in there.
So for example, if I was to go and create another new file
which I'll call another_module.
Now inside another_module, let's say I create another variable.
And now I can go back to my main.py,
I can import my another_module I just created
and I can tap into that variable
that's inside another module by saying another module dot another variable.
Now let's go ahead and run this code as it is.
And you can see the value of that variable from the other module being used
inside our main.py.
Now I want to do the same thing using turtle, but in this case,
I want to tap into the class called turtle
that's declared inside this turtle module. The wording's a little bit confusing,
but let me show you what I mean.
So I'm going to tap into the turtle module and I'm going to get hold of a
turtle class. So notice how it's got the C here
denoting that this is a blueprint for creating a new turtle
object. Now, remember that to actually construct the object
we need to add the parentheses at the end,
and now we can save all of this into an actual object, which we get to name.
So I'm going to call my turtle timmy
and now I've got a new turtle object. Essentially,
we've done the same thing as we have done up here.
We've imported a module called turtle,
we've tapped into that turtle module here,
and then we've fetched something from that module. In this case,
it's a variable. And in this case,
it's a class which is denoted by the capital T, the Pascal case here.
And then we've used the parentheses to initialize
or basically construct an object from that blueprint
and we've saved it into an object called Timmy.
Now I can simplify this code to make it look more like what we had in the slides
by saying, instead of import turtle, I can say from,
from the turtle module import the turtle class.
So now, instead of writing all of this,
I can simply just write Timmy
equals a new object created from the turtle class and it's constructed.
This is how we would construct our new object.
So now I have this brand new object called Timmy
and if I go ahead and print timmy
you can see that when I run this code what gets printed is a new turtle object
from the turtle module
and it's saved at this location in the computer's memory.
So this is very different from say
if we just printed a string or a number.
It works completely differently. In this case,
it's actually a object that's being printed. Now,
what can we do with this object?
[inaudible]
Now, as we saw before with our car
object it has certain attributes, right?
Like speed and fuel, data that it could keep track of that are really important
to the modeling of an actual car object. Now,
when it comes to accessing these attributes,
the syntax or the code looks like this. Car in this case is the object
and then we use a dot or a full stop to separate the object from the actual
attribute that we want to get hold of, which in this case is speed.
What this code does is it essentially identifies the object
and then it says, from this object get the speed attribute.
This is actually gonna represent the speed of this particular car
object. Coming back to our turtle.
One of the other classes that is inside this turtle module is something called
screen.
And the screen represents the window in which this turtle is going to show up.
So let's go ahead and create this screen
object. I'm going to call my object my_screen
and then I'm going to create it from this blueprint of a screen that we
imported. Now we can tap into one of the screens properties
which is called the canvas height and canvas width.
So let's see what the height is, and notice how we're using that dot notation.
So this is the object and then separated by a dot we've got the attributes that
we want to access from this object.
So I'm going to print that into the console. Now,
when I go ahead and run this code,
you'll see a window pop up very briefly
and that was the screen on which our turtle is going to show up.
Now, if you take a look in the console,
you can see that not only have we printed the turtle object,
but we've also printed the height of the canvas for this particular screen that
we've created.
So the screen here is the object and that canvas height is an attribute that's
associated with that screen.
Now, in addition to the things that an object has,
so data that it holds like the speed or the fuel
which we've already seen, it also has things that it can do, right?
So functions that are associated with that particular object. And these functions
when it's tied to an object is known as a method.
But essentially we would use it the same way as we have done with any other
function that we've created so far.
The only difference is the syntax. Firstly,
we would tap into the car object and then using that same dot notation that
we saw with the attributes, we're saying car.stop.
So essentially what we're doing is we're getting hold of the object and then
we're calling this function that's associated with that object,
which is known as a method. So coming back to our code here,
one of the things that we can do with this screen object that we've created here
called my_screen is we can tap into one of its functions.
And because it's a function that's tied to an object,
it's actually called a method.
So the method name is called exitonclick.
And what this will allow us to do is instead of having our screen show up and
then quickly disappear when our code ends,
this exitonclick will allow our program to continue running until we click on
the screen and then it exits our code.
So let's try this again and notice how we've now got our screen show up
and this canvas has a height of 300 and a width of 300,
but now we can see our turtle in the form of the arrow. So this is our
a little Timmy shown up in the middle.
And our code is only going to exit when the screen detects a click
because we've called this function that's associated with the screen object.
So now if I click on the screen,
you can see process finished with exit code zero, our code is ended and the screen
has disappeared. Now there are other functions as well.
So if we take our little timmy object and we say, well, timmy,
why don't we change the shape of you on the screen
and we'll change you into an actual turtle?
So now when I run the code,
you can see that object in the center is now in the shape of a turtle.
Now,
if you want to know how I know about all these things I can do with the turtle
graphics library, then of course,
I'm getting all of this information from the documentation.
So in the course resources,
you'll see a link to this Turtle graphics documentation,
and it tells you all the things that you can do with your turtle.
So for example,
you can call all of these methods that are associated with your turtle object to
get your turtle to move and draw,
and you can also use it to change its position or change its coordinates,
as well as doing things like changing the color or resetting.
And you can browse through this list to see how you would use each of these
things. So for example,
if I wanted to change the color of my turtle,
I can do that by simply just calling this method, color,
and then inside the parentheses
I pass in the argument of the actual color string.
Now you can choose from a whole range of colors just by putting in the name as a
string into that method which is called color.
So go ahead and see if you can change our turtle timmy's color from the default
which is just black to one of the colors that you see in this link here,
and the link, of course, is in the course resources.
So let's say I want to change it to a coral color like this one.
All I have to do is tap into my object which is called timmy,
and then call the method which is called color onto it.
And you can see all the methods are
denoted by a little M here in the circle.
Now this color, of course, expects some sort of input.
So the input that it expects is a actual string of a color.
And my string is going to be coral, which I picked up from here.
So now let's run our code and see what it looks like now.
Once the screen refreshes,
you can see that our turtle is now a nice coral color,
and we've managed to do all of this to change the shape,
change the color, all because we were able to call these functions
which belong to this
object. Now see if you can use the documentation here and figure out how to get the
turtle to move forward by a hundred paces.
Pause the video and try to complete this challenge.
All right.
So we saw earlier that it has a whole bunch of move and draw methods.
So we can simply use this forward method to get it to move forward.
And in terms of the parameters that it takes,
it takes a number which is going to determine the distance that it's going to
move forward by.
So we said that we wanted our turtle to move forward by a hundred paces.
All I have to do is specify the object that I want to move which is timmy,
and then call that method forward and pass in the distance.
So I said a hundred paces. So now let's run our code again,
and you can see that little turtle just move forward by a hundred paces.
So now we've seen how to create a new Object from a blueprint,
we've seen how we can tap into its attributes by using the object name dot and
then the name of the attribute.
So this is the same as having a variable that's associated with that object.
And finally,
we've seen how we can call methods that are associated with the object.
For example, here
when we changed the shape or when we changed the color or when we told it to go
forwards. Now in the next lesson,
I'm going to show you how you can get hold of more external libraries
not just turtle, but a whole world of packages that you can tap into.
And you can start using these packages of code that other programmers have
already developed,
and simply by creating objects from the existing blueprints and reading the
documentation to get it to do various things that you want it to.
So for all of that and more, I'll see you on the next lesson.