27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

pythonhtp1_08.fm Page 261 Monday, December 10, 2001 6:49 PM<br />

Chapter 8 Customizing Classes 261<br />

Outline<br />

8.1 Introduction<br />

8.2 Customizing String Representation: Method __str__<br />

8.3 Customizing Attribute Access<br />

8.4 Operator Overloading<br />

8.5 Restrictions on Operator Overloading<br />

8.6 Overloading Unary Operators<br />

8.7 Overloading Binary Operators<br />

8.8 Overloading Built-in Functions<br />

8.9 Converting Between Types<br />

8.10 Case Study: A Rational Class<br />

8.11 Overloading Sequence Operations<br />

8.12 Case Study: A SingleList Class<br />

8.13 Overloading Mapping Operations<br />

8.14 Case Study: A SimpleDictionary Class<br />

Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises<br />

8.1 Introduction<br />

In Chapter 7, we introduced the basics of <strong>Python</strong> classes and the notion of abstract data<br />

types (ADTs). We discussed how methods __init__ and __del__ execute when an object<br />

is created and destroyed, respectively. These methods are two examples of the many<br />

special methods that a class may define. A special method is a method that has a special<br />

meaning in <strong>Python</strong>; the <strong>Python</strong> interpreter calls one of an object’s special methods when<br />

the client performs a certain operation on the object. For example, when a client creates an<br />

object of a class, <strong>Python</strong> invokes the __init__ special method of that class.<br />

A class author implements special methods to customize the behavior of the class. The<br />

purpose of customization is to provide the clients of a class with a simple notation for<br />

manipulating objects of the class. For example, in Chapter 7, manipulations on objects were<br />

accomplished by sending messages (in the form of method calls) to the objects. This<br />

method-call notation is cumbersome for certain kinds of classes, especially mathematical<br />

classes. For such classes, it would be nice to use <strong>Python</strong>’s rich set of built-in operators and<br />

statements to manipulate objects. In this chapter, we show how to define special methods<br />

that enable <strong>Python</strong>’s operators to work with objects—a process called operator overloading.<br />

It is straightforward and natural to extend <strong>Python</strong> with these new capabilities.<br />

Operator overloading also requires great care, because, when overloading is misused, it can<br />

make a program difficult to understand.<br />

Operator + has multiple purposes in <strong>Python</strong>, for example, integer addition and string concatenation.<br />

This is an example of operator overloading. The <strong>Python</strong> language itself overloads<br />

operators + and *, among others. These operators perform differently to suit the context in<br />

integer arithmetic, floating-point arithmetic, string manipulation and other operations.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!