16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 7 ■ MORE ABSTRACTION 155<br />

class TalkingCalcula<strong>to</strong>r(Calcula<strong>to</strong>r, Talker):<br />

pass<br />

The subclass (TalkingCalcula<strong>to</strong>r) does nothing by itself; it inherits all its behavior from its<br />

superclasses. The point is that it inherits both calculate from Calcula<strong>to</strong>r and talk from Talker,<br />

making it a talking calcula<strong>to</strong>r:<br />

>>> tc = TalkingCalcula<strong>to</strong>r()<br />

>>> tc.calculate('1+2*3')<br />

>>> tc.talk()<br />

Hi, my value is 7<br />

This is called multiple inheritance, and can be a very powerful <strong>to</strong>ol.<br />

■Note When using multiple inheritance, there is one thing you should look out for. If a method is implemented<br />

differently by two or more of the superclasses, you must be careful about the order of these superclasses<br />

(in the class statement): The methods in the earlier classes override the methods in the later ones. So if<br />

the Calcula<strong>to</strong>r class in the preceding example had a method called talk, it would override (and make<br />

inaccessible) the talk method of the Talker. Reversing their order, like this:<br />

class TalkingCalcula<strong>to</strong>r(Talker, Calcula<strong>to</strong>r): pass<br />

would have made the talk method of the Talker accessible. The normal way of handling multiple inheritance<br />

is <strong>to</strong> have one “substantial” base class, and <strong>to</strong> add so-called mix-in classes that implement a few methods,<br />

“modifying” the inheritance. If the mix-ins are <strong>to</strong> override something in the base class, they must be put first,<br />

and, by convention, they usually are anyway—just in case. If the superclasses share a common superclass,<br />

the order in which the superclasses are visited while looking for a given attribute or method is called the<br />

method resolution order (MRO), and follows a rather complicated algorithm. Luckily, it works very well, so you<br />

probably needn’t worry about it.<br />

Interfaces and Introspection<br />

The “interface” concept is related <strong>to</strong> polymorphism. When you handle a polymorphic object,<br />

you only care about its interface (or “pro<strong>to</strong>col”)—the methods and attributes known <strong>to</strong> the<br />

world. In <strong>Python</strong>, you don’t explicitly specify which methods an object needs <strong>to</strong> have <strong>to</strong> be<br />

acceptable as a parameter. For example, you don’t write interfaces explicitly (as you do in Java);<br />

you just assume that an object can do what you ask it <strong>to</strong>. If it can’t, the program will fail.<br />

■Note There is some talk of adding explicit interface functionality <strong>to</strong> <strong>Python</strong>. For more information,<br />

take a look at <strong>Python</strong> Enhancement Proposal number 245 (http://www.python.org/peps/<br />

pep-0245.html).<br />

Usually, you simply require that objects conform <strong>to</strong> a certain interface (in other words,<br />

implement certain methods), but if you want <strong>to</strong>, you can be quite flexible in your demands.

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

Saved successfully!

Ooh no, something went wrong!