Problem

Develop a script to demonstrate an understanding of the overload (overwrite) methods of using Python operators. This lesson will focus on the "+" operator and the "__add__" method.

The program must have the following:

  • Demonstration of an understanding of how to use the + operator for basic addition of two integer numbers
  • Demonstration of an understanding of how to use the + operator for a concatenation of lists
  • Demonstration of an understanding of how to use the + operator for a concatenation of strings
  • Demonstration of an understanding of how to overload the __add__ method

Solving the Problem

Step 1

Create a basic set of Python commands to demonstrate the addition of two integer numbers, and display the results in the operator interface window of PyCharm.

Step 2

Next, review the first example of an overloaded operator. In Python, the __add__ method that is being used to sum the two integer numbers above can also accept a list of numbers and concatenate them.

Step 3

Now, review the second example of an overloaded operator. In Python, the __add__ method that is being used to sum the two integer numbers above can also accept two strings and concatenate them.

Step 4

Lastly, overlod the __add__ method, the method that has been used to sum two numbers, combine lists, and concatenate strings. Overload the method to format the output using the following code: return '%s plus %s' % (self, x).

Step 5

Run the scripts, and verify that the output marches the screenshot below:

The sum of 2 and 2 is 4
The newly concatenated list is [1, 2, 3, 4, 5, 6, 7, 8]
The newly concatenate string is Python String Overloading Example
Overloading the __add__ method example -> 2 plus 2

Documentation Guidelines:

Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the '<' and '>' angle brackets are only placeholders. You should replace the placeholders and the comments between them with your specific information). Your cover sheet should have some of the same information, but what follows should be at the top of each program's sheet of source code. Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose. Be brief and to the point. Start your design by writing comment lines of pseudocode. Once that is complete, begin adding executable lines. Finally run and test your program.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.