12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

Create successful ePaper yourself

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

Specially named methods such as _ _init_ _ and _ _add_ _ are inherited by subclassesand instances, just like any other names assigned in a class. If the methods are notcoded in a class, <strong>Python</strong> looks for such names in all its superclasses, as usual. Operatoroverloading method names are also not built-in or reserved words; they are justattributes that <strong>Python</strong> looks for when objects appear in various contexts. <strong>Python</strong>usually calls them automatically, but they may occasionally be called by your code aswell (more on this later; the _ _init_ _ method, for example, is often called manuallyto trigger superclass constructors).Notice that the _ _add_ _ method makes and returns a new instance object of its class(by calling ThirdClass with the result value), but _ _mul_ _ changes the currentinstance object in-place (by reassigning the self attribute). This is different from thebehavior of built-in types such as numbers and strings, which always make newobjects for the * operator. Because operator overloading is really just an expressionto-methoddispatch mechanism, you can interpret operators any way you like in yourown class objects. *Why Use Operator Overloading?As a class designer, you can choose to use operator overloading or not. Your choicesimply depends on how much you want your object to look and feel like a built-intype. As mentioned earlier, if you omit an operator overloading method, and do notinherit it from a superclass, the corresponding operation will not be supported foryour instances; if it’s attempted, an exception will be thrown (or a standard defaultwill be used).Frankly, many operator overloading methods tend to be used only when implementingobjects that are mathematical in nature; a vector or matrix class may overload theaddition operator, for example, but an employee class likely would not. For simplerclasses, you might not use overloading at all, and would rely instead on explicitmethod calls to implement your objects’ behavior.On the other hand, you might decide to use operator overloading if you need to passa user-defined object to a function that was coded to expect the operators availableon a built-in type like a list or a dictionary. Implementing the same operator set inyour class will ensure that your objects support the same expected object interface,and so are compatible with the function.One overloading method seems to show up in almost every realistic class: the _ _init_ _constructor method. Because it allows classes to fill out the attributes in their newlycreated instances immediately, the constructor is useful for almost every kind of class* But you probably shouldn’t. Common practice dictates that overloaded operators should work the same waythe built-in operator implementations do. In this case, that means our _ _mul_ _ method should also return anew object as its result, rather than changing the instance (self) in place; for in-place changes, a mul methodcall may be better style than a * overload here (e.g., a.mul(3) instead of a*3).Classes Can Intercept <strong>Python</strong> Operators | 475

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

Saved successfully!

Ooh no, something went wrong!