Think Python/Classes and objects

User-defined types
We have used many of Python&#X2019;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, (0, 0) represents the origin, and (x, y) represents the point x units to the right and y 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 (a little) more complicated than the other options, but it has advantages that will be apparent soon.

A user-defined type is also called a class. A class definition looks like this:

This header indicates that the new class is a Point, which is a kind of <TT>object</TT>, which is a built-in type.

The body is a docstring that explains what the class is for. You can define variables and functions inside a class definition, but we will get back to that later.

Defining a class named <TT>Point</TT> creates a class object. Because <TT>Point</TT> is defined at the top level, its &#X201C;full name&#X201D; is.

The class object is like a factory for creating objects. To create a Point, you call <TT>Point</TT> as if it were a function. The return value is a reference to a Point object, which we assign to <TT>blank</TT>. 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 <TT>0x</TT> means that the following number is in hexadecimal).

Attributes
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 <TT>math.pi</TT> or <TT>string.whitespace</TT>. In this case, though, we are assigning values to named elements of an object. These elements are called attributes.

As a noun, &#X201C;AT-trib-ute&#X201D; is pronounced with emphasis on the first syllable, as opposed to &#X201C;a-TRIB-ute,&#X201D; which is a verb.

The following diagram shows the result of these assignments. A state diagram that shows an object and its attributes is called an object diagram:



The variable <TT>blank</TT> 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 <TT>blank.x</TT> means, &#X201C;Go to the object <TT>blank</TT> refers to and get the value of <TT>x</TT>.&#X201D; In this case, we assign that value to a variable named <TT>x</TT>. There is no conflict between the variable <TT>x</TT> and the attribute <TT>x</TT>.

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:

takes a point as an argument and displays it in mathematical notation. To invoke it, you can pass <TT>blank</TT> as an argument: Inside the function, <TT>p</TT> is an alias for <TT>blank</TT>, so if the function modifies <TT>p</TT>, <TT>blank</TT> changes.

Exercise 1
Write a function called '<TT>distance</TT>' that it takes two Points as arguments and returns the distance between them.

Rectangles
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&#X2019;ll implement the first one, just as an example.

Here is the class definition: The docstring lists the attributes: <TT>width</TT> and <TT>height</TT> are numbers; <TT>corner</TT> 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 <TT>box.corner.x</TT> means, &#X201C;Go to the object <TT>box</TT> refers to and select the attribute named <TT>corner</TT>; then go to that object and select the attribute named <TT>x</TT>.&#X201D;

The figure shows the state of this object:

An object that is an attribute of another object is embedded.

Instances as return values
Functions can return instances. For example, takes a <TT>Rectangle</TT> as an argument and returns a <TT>Point</TT> that contains the coordinates of the center of the <TT>Rectangle</TT>: Here is an example that passes <TT>box</TT> as an argument and assigns the resulting Point to <TT>center</TT>:

Objects are mutable
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 <TT>width</TT> and <TT>height</TT>: You can also write functions that modify objects. For example, takes a Rectangle object and two numbers, <TT>dwidth</TT> and <TT>dheight</TT>, and adds the numbers to the width and height of the rectangle: Here is an example that demonstrates the effect: Inside the function, <TT>rect</TT> is an alias for <TT>box</TT>, so if the function modifies <TT>rect</TT>, <TT>box</TT> changes.

Exercise 2
''Write a function named  that takes a Rectangle and two numbers named <TT>dx</TT> and <TT>dy</TT>. It should change the location of the rectangle by adding <TT>dx</TT> to the <TT>x</TT> coordinate of <TT>corner</TT> and adding <TT>dy</TT> to the <TT>y</TT> coordinate of <TT>corner</TT>.''

Copying
Aliasing can make a program difficult to read because changes in one place might have unexpected effects in another place. It is hard to keep track of all the variables that might refer to a given object.

Copying an object is often an alternative to aliasing. The <TT>copy</TT> module contains a function called <TT>copy</TT> that can duplicate any object: <TT>p1</TT> and <TT>p2</TT> contain the same data, but they are not the same Point. The <TT>is</TT> operator indicates that <TT>p1</TT> and <TT>p2</TT> are not the same object, which is what we expected. But you might have expected <TT>==</TT> to yield <TT>True</TT> because these points contain the same data. In that case, you will be disappointed to learn that for instances, the default behavior of the <TT>==</TT> operator is the same as the <TT>is</TT> operator; it checks object identity, not object equivalence. This behavior can be changed&#X2014;we&#X2019;ll see how later.

If you use <TT>copy.copy</TT> to duplicate a Rectangle, you will find that it copies the Rectangle object but not the embedded Point.

Here is what the object diagram looks like:

<DIV CLASS="center"><IMG SRC="book024.png"></DIV>

This operation is called a shallow copy because it copies the object and any references it contains, but not the embedded objects.

For most applications, this is not what you want. In this example, invoking  on one of the Rectangles would not affect the other, but invoking  on either would affect both! This behavior is confusing and error-prone.

Fortunately, the <TT>copy</TT> module contains a method named <TT>deepcopy</TT> that copies not only the object but also the objects it refers to, and the objects they refer to, and so on. You will not be surprised to learn that this operation is called a deep copy.

<TT>box3</TT> and <TT>box</TT> are completely separate objects.

Exercise 3
Write a version of  that creates and returns a new Rectangle instead of modifying the old one.

Debugging
When you start working with objects, you are likely to encounter some new exceptions. If you try to access an attribute that doesn&#X2019;t exist, you get an <TT>AttributeError</TT>:

If you are not sure what type an object is, you can ask:

If you are not sure whether an object has a particular attribute, you can use the built-in function <TT>hasattr</TT>:

The first argument can be any object; the second argument is a string that contains the name of the attribute.

Glossary
<DL CLASS="description"><DT CLASS="dt-description">class:</DT><DD CLASS="dd-description"> A user-defined type. A class definition creates a new class object. </DD><DT CLASS="dt-description">class object:</DT><DD CLASS="dd-description"> An object that contains information about a user-defined type. The class object can be used to create instances of the type. </DD><DT CLASS="dt-description">instance:</DT><DD CLASS="dd-description"> An object that belongs to a class. </DD><DT CLASS="dt-description">attribute:</DT><DD CLASS="dd-description"> One of the named values associated with an object.

</DD><DT CLASS="dt-description">embedded (object):</DT><DD CLASS="dd-description"> An object that is stored as an attribute of another object.

</DD><DT CLASS="dt-description">shallow copy:</DT><DD CLASS="dd-description"> To copy the contents of an object, including any references to embedded objects; implemented by the <TT>copy</TT> function in the <TT>copy</TT> module. </DD><DT CLASS="dt-description">deep copy:</DT><DD CLASS="dd-description"> 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 <TT>deepcopy</TT> function in the <TT>copy</TT> module. </DD><DT CLASS="dt-description">object diagram:</DT><DD CLASS="dd-description"> A diagram that shows objects, their attributes, and the values of the attributes.

</DD></DL>

Exercise 4
''<TT>World.py</TT>, which is part of Swampy (see Chapter&#XA0;'4'), contains a class definition for a user-defined type called <TT>World</TT>. If you run this code:'' ''A window should appear with a title bar and an empty square. In this exercise we will use this window to draw Points, Rectangles and other shapes. Add the following lines before  and run the program again''

''You should see a green rectangle with a black outline. The first line creates a Canvas, which appears in the window as a white square. The Canvas object provides methods like <TT>rectangle</TT> for drawing various shapes.''

''<TT>bbox</TT> is a list of lists that represents the &#X201C;bounding box&#X201D; of the rectangle. The first pair of coordinates is the lower-left corner of the rectangle; the second pair is the upper-right corner.''

You can draw a circle like this:

The first parameter is the coordinate pair for the center of the circle; the second parameter is the radius.

If you add this line to the program, the result should resemble the national flag of Bangladesh (see <TT>wikipedia.org/wiki/Gallery_of_sovereign-state_flags</TT>).


 * Write a function called  that takes a Canvas and a Rectangle as arguments and draws a representation of the Rectangle on the Canvas.


 * Add an attribute named <TT>color</TT> to your Rectangle objects and modify  so that it uses the color attribute as the fill color.


 * Write a function called  that takes a Canvas and a Point as arguments and draws a representation of the Point on the Canvas.


 * Define a new class called Circle with appropriate attributes and instantiate a few Circle objects. Write a function called  that draws circles on the canvas.


 * Write a program that draws the national flag of the Czech Republic. Hint: you can draw a polygon like this:

I have written a small program that lists the available colors; you can download it from <TT>thinkpython.com/code/color_list.py</TT>.