Cement your knowledge of objects
Note: There is no speech on the video above, and all of the steps in the video are described below.
So far, you have learnt that an object has attributes (or pieces of data) stored inside it, and methods we can call on it to give it instructions.
As I mentioned briefly earlier, a class is like a blueprint for creating objects. I like to think of a class as being similar to a cookie cutter: it is a template for all the cookie objects you make.
You can make as many instances of cookie objects as you want, and they will all start off from the same template.
If you like, you can customise each instance of a cookie object, perhaps by adding some icing or some sprinkles. But whenever you make cookies, you use the same cookie cutter template.
Imagine you have created a
Cookie class in a module called
baking which accepts a list of toppings.
You could create some cookies using the following code.
from baking import Cookie sprinkled = Cookie(["sprinkles"]) iced_chocolate = Cookie(["icing", "chocolate chips"])
Think back to the turtle race program that you wrote. You created four instances of turtle objects from the
Turtle class and customised the attributes of each turtle object by changing its colour and position. You used the
forward method to move each turtle object, which drew a line across the screen based on that turtle’s colour attribute.
Over the next few steps you’ll use some different objects and combine them into a single program.