Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

4.1 Constructors

To create an object of a certain type or class we must write

< objectref > = < Class >( < args >)

This creates an object of type Class and then points the objectref variable at the object that was just created. Figure 4.1 shows what happens in memory as a result of executing the t=Turtle() line of code in Examples 4.1 and 4.2. Several things happen when we create an object. Python first reserves enough space in memory to hold the object's data. Then, the object is initialized with the data that must be stored in it. All objects have some data associated with them. For instance, a Turtle object knows its current location on the 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:

t = Turtle ()

we are instructing Python to create a Turtle object using the constructor and we make the variable t point to the turtle object that was just created. There are lots of constructors that are available to us for creating different types of objects in Python.

Example 4.3 Here are some examples of objects being created using constructors. The types (i.e. classes) str, int, float, Turtle, and list each have their own constructors. In fact, sometimes a class has more than one constructor. Look at the float examples below. There are at least two ways to create a float object. You can either pass the constructor a string and it will convert the float in the string to a float object, or you can pass an integer to the float constructor.

1 s = s t r (6)

2 x = i n t ("6")

3 y = f l o a t ("6.5")

4 z = f l o a t (6)

5 t = Turtle ()

6 lst = l i s t ("a b c")

7 u = 6

8 r = "hi there"

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?

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel