At this point you know how to use functions to organize code and built-in types to organize data. The next step is to learn “object-oriented programming”, which uses programmer-defined types to organize both code and data. Object-oriented programming is a big topic; it will take a few chapters to get there.
We have used many of Python’s built-in types; now we are going to define a new type. As an example, we will create a type called Point that represents a point in two-dimensional space.
In mathematical notation, points are often written in parentheses with a comma separating the coordinates. For example, represents the origin, and represents the point units to the right and units up from the origin.
There are several ways we might represent points in Python:
We could store the coordinates separately in two variables, x and y.
We could store the coordinates as elements in a list or tuple.
We could create a new type to represent points as objects.
Creating a new type is more complicated than the other options, but it has advantages that will be apparent soon.
A programmer-defined type is also called a class. A class definition looks like this:
The header indicates that the new class is called Point. The body is a docstring that explains what the class is for. You can define variables and methods inside a class definition, but we will get back to that later.
Defining a class named Point creates a class object.
Because Point is defined at the top level, its “full
name” is __main__.Point
.
The class object is like a factory for creating objects. To create a Point, you call Point as if it were a function.
The return value is a reference to a Point object, which we assign to blank.
Creating a new object is called instantiation, and the object is an instance of the class.
When you print an instance, Python tells you what class it belongs to and where it is stored in memory (the prefix 0x means that the following number is in hexadecimal).
Every object is an instance of some class, so “object” and “instance” are interchangeable. But in this chapter I use “instance” to indicate that I am talking about a programmer-defined type.
You can assign values to an instance using dot notation:
This syntax is similar to the syntax for selecting a variable from a module, such as math.pi or string.whitespace. In this case, though, we are assigning values to named elements of an object. These elements are called attributes.
As a noun, “AT-trib-ute” is pronounced with emphasis on the first syllable, as opposed to “a-TRIB-ute”, which is a verb.
Figure 27.1 is a state diagram that shows the result of these assignments. A state diagram that shows an object and its attributes is called an object diagram.
The variable blank refers to a Point object, which contains two attributes. Each attribute refers to a floating-point number.
You can read the value of an attribute using the same syntax:
The expression blank.x means, “Go to the object blank refers to and get the value of x.” In the example, we assign that value to a variable named x. There is no conflict between the variable x and the attribute x.
You can use dot notation as part of any expression. For example:
You can pass an instance as an argument in the usual way. For example:
print_point
takes a point as an argument and displays it in
mathematical notation. To invoke it, you can pass blank as
an argument:
Inside the function, p is an alias for blank, so if the function modifies p, blank changes.
As an exercise, write a function called distance_between_points
that takes two Points as arguments and returns the distance between
them.
Sometimes it is obvious what the attributes of an object should be, but other times you have to make decisions. For example, imagine you are designing a class to represent rectangles. What attributes would you use to specify the location and size of a rectangle? You can ignore angle; to keep things simple, assume that the rectangle is either vertical or horizontal.
There are at least two possibilities:
You could specify one corner of the rectangle (or the center), the width, and the height.
You could specify two opposing corners.
At this point it is hard to say whether either is better than the other, so we’ll implement the first one, just as an example.
Here is the class definition:
The docstring lists the attributes: width and height are numbers; corner is a Point object that specifies the lower-left corner.
To represent a rectangle, you have to instantiate a Rectangle object and assign values to the attributes:
The expression box.corner.x means, “Go to the object box refers to and select the attribute named corner; then go to that object and select the attribute named x.”
Figure 27.2 shows the state of this object. An object that is an attribute of another object is embedded.
Functions can return instances. For example, find_center
takes a Rectangle as an argument and returns a Point
that contains the coordinates of the center of the Rectangle:
Here is an example that passes box as an argument and assigns the resulting Point to center:
You can change the state of an object by making an assignment to one of its attributes. For example, to change the size of a rectangle without changing its position, you can modify the values of width and height:
You can also write functions that modify objects. For example,
grow_rectangle
takes a Rectangle object and two numbers,
dwidth and dheight, and adds the numbers to the
width and height of the rectangle:
Here is an example that demonstrates the effect:
Inside the function, rect is an alias for box, so when the function modifies rect, box changes.
As an exercise, write a function named move_rectangle
that takes
a Rectangle and two numbers named dx and dy. It
should change the location of the rectangle by adding dx
to the x coordinate of corner and adding dy
to the y coordinate of corner.
When you start working with objects, you are likely to encounter some new exceptions. If you try to access an attribute that doesn’t exist, you get an AttributeError:
You can also use isinstance to check whether an object is an instance of a class:
If you are not sure whether an object has a particular attribute, you can use the built-in function hasattr:
The first argument can be any object; the second argument is a string that contains the name of the attribute.
You can also use a try statement to see if the object has the attributes you need:
This approach can make it easier to write functions that work with different types; more on that topic is coming up.
A programmer-defined type. A class definition creates a new class object.
An object that contains information about a programmer-defined type. The class object can be used to create instances of the type.
An object that belongs to a class.
To create a new object.
One of the named values associated with an object.
An object that is stored as an attribute of another object.
To copy the contents of an object, including any references to embedded objects; implemented by the copy function in the copy module.
To copy the contents of an object as well as any embedded objects, and any objects embedded in them, and so on; implemented by the deepcopy function in the copy module.
A diagram that shows objects, their attributes, and the values of the attributes.
Write a definition for a class named Circle with attributes center and radius, where center is a Point object and radius is a number.
Instantiate a Circle object that represents a circle with its center at and radius 75.
Write a function named point_in_circle
that takes a Circle and
a Point and returns True if the Point lies in or on the boundary of
the circle.
Write a function named rect_in_circle
that takes a Circle and a
Rectangle and returns True if the Rectangle lies entirely in or on the boundary
of the circle.
Write a function named rect_circle_overlap
that takes a Circle
and a Rectangle and returns True if any of the corners of the Rectangle fall
inside the Circle. Or as a more challenging version, return True if
any part of the Rectangle falls inside the Circle.
Write a function called draw_rect
that takes a Turtle object
and a Rectangle and uses the Turtle to draw the Rectangle. See
Chapter 6 for examples using Turtle objects.
Write a function called draw_circle
that takes a Turtle and
a Circle and draws the Circle.