By John Paul Mueller

Occasionally, you want to be able to do something special as the result of using a standard operator such as add (+). Sometimes Python doesn’t provide a default behavior for operators because it has no default to implement. No matter what the reason might be, overloading operators makes it possible to assign new functionality to existing operators so that they do what you want, rather than what Python intended.

The following steps demonstrate how to overload an operator and use it as part of an application.

  1. Open a Python File window.

    You see an editor in which you can type the example code.

  2. Type the following code into the window — pressing Enter after each line:

    class MyClass:
     def __init__(self, *args):
      self.Input = args
     def __add__(self, Other):
      Output = MyClass()
      Output.Input = self.Input + Other.Input
      return Output
     def __str__(self):
      Output = "
      for Item in self.Input:
       Output += Item
       Output += " "
      return Output
    Value1 = MyClass("Red", "Green", "Blue")
    Value2 = MyClass("Yellow", "Purple", "Cyan")
    Value3 = Value1 + Value2
    print("{0} + {1} = {2}"
      .format(Value1, Value2, Value3))

    The example demonstrates a few different techniques. The constructor, __init__(), demonstrates a method for creating an instance variable attached to the self object. You can use this approach to create as many variables as needed to support the instance.

    When you create your own classes, no + operator is defined until you define one, in most cases. The only exception is when you inherit from an existing class that already has the + operator defined. In order to add two MyClass entries together, you must define the __add__() method, which equates to the + operator.

    The code used for the __add__() method may look a little odd, too, but you need to think about it one line at a time. The code begins by creating a new object, Output, from MyClass. Nothing is added to Output at this point — it’s a blank object.

    The two objects that you want to add, self.Input and Other.Input, are actually tuples. The code places the sum of these two objects into Output.Input. The __add__() method then returns the new combined object to the caller.

    Of course, you may want to know why you can’t simply add the two inputs together as you would a number. The answer is that you’d end up with a tuple as an output, rather than a MyClass as an output. The type of the output would be changed, and that would also change any use of the resulting object.

    To print MyClass properly, you also need to define a __str__() method. This method converts a MyClass object into a string. In this case, the output is a space-delimited string (each of the items in the string is separated from the other items by a space) containing each of the values found in self.Input. Of course, the class that you create can output any string that fully represents the object.

    The main procedure creates two test objects, Value1 and Value2. It adds them together and places the result in Value3. The result is printed onscreen.

  3. Choose Run→Run Module.

    Here is the result of adding the two objects together, converting them to strings, and then printing the result. It’s a lot of code for such a simple output statement, but the result definitely demonstrates that you can create classes that are self-contained and fully functional.

    image0.jpg