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:
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.