Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
3.13. Object Oriented Concepts¶
It’s been fun drawing things with the turtles. In the process, we’ve slipped in some new concepts and terms. Let’s pull them out and examine them a little more carefully. We mentioned previously, and we’ll mention it again here - the turtle module is object-oriented (as are many other modules in Python). Object-oriented programming is not the focus of this course at all. If you continue in computer science, you will learn a lot more about object-oriented programming, which is a really powerful way of thinking about the world and how to solve problems.
3.13.1. User-defined Classes¶
First, just as Python provides a way to define new functions in your programs, it also provides a way to define new classes of objects. Later in the book you will learn how to define functions, and in later computing courses you will learn how to define your own object classess. For now, you just need to understand how to use them.
Given a class like
Screen, we create a new instance with a syntax that looks like a function call,
Turtle(). The Python interpreter figures out that Turtle is a class rather than a function, and so it creates a new instance of the class and returns it. Since the Turtle class was defined in a separate module, (confusingly, also named turtle), we had to refer to the class as turtle.Turtle. Thus, in the programs we wrote
turtle.Turtle() to make a new turtle. We could also write
turtle.Screen() to make a new window for our turtles to paint in.
Each instance can have attributes, sometimes called instance variables. These are just like other variables in Python. We use assignment statements, with an =, to assign values to them. Thus, if alex and tess are variables bound (linked) to two instances of the class Turtle, we can assign values to an attribute, and we can look up those attributes. For example, the following code would print out 1100.
alex.price = 500 tess.price = 600 print(alex.price + tess.price)
Classes have associated methods, which are just a special kind of function. Consider the expression
alex.forward(50) The interpreter first looks up alex and finds that it is an instance of the class Turtle. Then it looks up the attribute forward and finds that it is a method. Since there is a left parenthesis directly following, the interpreter invokes the method, passing 50 as a parameter.
The only difference between a method invocation and other function calls is that the object instance itself is also passed as a parameter. Thus
alex.forward(50) moves alex, while
tess.forward(50) moves tess.
Some of the methods of the Turtle class set attributes that affect the actions of other methods. For example, the method pensize changes the width of the drawing pen, and the color method changes the pen’s color.
Methods return values, just as functions do. However, none of the methods of the Turtle class that you have used return useful values the way the
len function does. Thus, it would not make sense to build a complex expression like
tess.forward(50) + 75. It could make sense, however to put a complex expression inside the parentheses:
tess.forward(x + y)