Over 10 Million Study Resources Now at Your Fingertips

Download as :
Rating : ⭐⭐⭐⭐⭐
Price : $10.99
Pages: 2

Mutator methods are called the same accessor methods




screen, its direction, and its color, among other things. When a Turtle object is created, all the information is stored in the object. This is called constructing an object and it happens when we call the constructor. So, when we write the following line of code or similar lines of code for other types of objects:

Except in a few special circumstances, a constructor is always called by writing the name of the class then a left paren, then any arguments to pass to the constructor, followed by a right paren. Calling a constructor returns an instance of the class, called an object. For a few of the built-in classes there is some syntactic sugar available for creating objects. In Example 4.3, the variables u and r are initialized to point to an integer object and a string object, respectively. Syntactic sugar makes constructing objects for some of the built-in classes more convenient and it is necessary in some cases. Without some syntactic sugar, how would you create an object containing the integer 6?

Practice 4.4 Using Wing, or some other IDE, run the code in Example 4.2. Set a
breakpoint at the line where screen is initialized. Then, look at the Stack Data and
specifically at the t variable. Expand it out so you can see the state of the turtle and
specifically the _position of the turtle. This is the (x, y) location of the turtle on the
screen. When the turtle is at the peak of the pentagon from Example 4.2 what is its

(x, y) location?

98 4
Accessor Methods

import turtle

t = turtle.Turtle()

Practice 4.5 Is the forward method an accessor method? What about the xcor

Mutator methods, as the name suggests, change or mutate the state of the object. Sec-tion 3.5 introduced the mutability of lists. Mutator methods are called the same as accessor methods. Where an accessor method usually gives you information back, a mutator method may require you to provide some information to the object.

Example 4.5 Here are some calls to mutator methods.

How It Works
Login account
Login Your Account
Add to cart
Add to Cart
Make payment
Document download
Download File
PageId: ELIAE87A42
Uploaded by :
Page 1 Preview
mutator methods are called the same accessor metho
Sell Your Old Documents & Earn Wallet Balance