12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

foster object interfaces that are more consistent and easier to learn, and it allows classbasedobjects to be processed by code written to expect a built-in type’s interface.Here is a quick rundown of the main ideas behind overloading operators:• Methods named with double underscores (_ _X_ _) are special hooks. <strong>Python</strong>operator overloading is implemented by providing specially named methods tointercept operations. The <strong>Python</strong> language defines a fixed and unchangeablemapping from each of these operations to a specially named method.• Such methods are called automatically when instances appear in built-inoperations. For instance, if an instance object inherits an _ _add_ _ method, thatmethod is called whenever the object appears in a + expression. The method’sreturn value becomes the result of the corresponding expression.• Classes may override most built-in type operations. There are dozens of specialoperator overloading method names for intercepting and implementing nearlyevery operation available for built-in types. This includes expressions, but alsobasic operations like printing and object creation.• There are no defaults for operator overloading methods, and none are required.If a class does not define or inherit an operator overloading method, it justmeans that the corresponding operation is not supported for the class’ instances.If there is no _ _add_ _, for example, + expressions raise exceptions.• Operators allow classes to integrate with <strong>Python</strong>’s object model. By overloadingtype operations, user-defined objects implemented with classes act just likebuilt-ins, and so provide consistency as well as compatibility with expectedinterfaces.Operator overloading is an optional feature; it’s used primarily by people developingtools for other <strong>Python</strong> programmers, not by application developers. And, candidly,you probably shouldn’t try to use it just because it seems “cool.” Unless a class needsto mimic built-in type interfaces, it should usually stick to simpler named methods.Why would an employee database application support expressions like * and +, forexample? Named methods like giveRaise and promote would usually make moresense.Because of this, we won’t go into details on every operator overloading method availablein <strong>Python</strong> in this book. Still, there is one operator overloading method you arelikely to see in almost every realistic <strong>Python</strong> class: the _ _init_ _ method, which isknown as the constructor method, and is used to initialize objects’ state. You shouldpay special attention to this method, because _ _init_ _, along with the self argument,turns out to be one of the keys to understanding OOP code in <strong>Python</strong>.Classes Can Intercept <strong>Python</strong> Operators | 473

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

Saved successfully!

Ooh no, something went wrong!