Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with self
#1
This is the assignment:
The ComplexNumber Class
Define a class ComplexNumber, which has two data fields (both numbers), real and imaginary. In addition, the class should support the following methods.

1. A default constructor (no arguments) and a constructor that takes in two numbers as arguments, which represents the real and imaginary parts. You can combine the two constructors into one constructor, by setting default values for the parameters.

2. Overload the operator +, which performs the addition of two ComplexNumber objects. You should figure out all the details of the function (e.g. return type, parameters, etc.) by yourself.

Input File
There are 4 numbers in each line of the input file, which are real part of ComplexNumber #1, imaginary part of ComplexNumber #1, real part of ComplexNumber #2, imaginary part of ComplexNumber #2, in that order from left to right. All the numbers are separated by whitespaces.

This is the sample input.txt:

0 0 0 0
3 6 9 2
-5 0 0 -3
2.5 2 3.33 2.2

This is the expected output:

0.000000 + 0.000000 * i
12.000000 + 8.000000 * i
-5.000000 + -3.000000 * i
5.830000 + 4.200000 * i

class ComplexNumber:  
               
    def __init__(self, R, I):   
        self.R, self.I = R, I                      
                                                                                     
                
    def add(C1, C2):  
        R = (C1.R + C2.R)
        I = (C1.I + C2.I)
        return ComplexNumber(R, I)
            
    def __str__(self):
        OutStr = str(self.R) + " + " + str(self.I) + " * " + "i"
        return OutStr
    
InputFile = open('input.txt')
OutFile = open('output.txt', 'w')

for lines in InputFile.readlines(): 
    values = [float(i) for i in lines.split()]
    N_1 = ComplexNumber(values[0], values[1])
    N_2 = ComplexNumber(values[2], values[3])
    # N_3 = ComplexNumber(0, 0)
    N_3 = ComplexNumber.add(N_1, N_2)
    print(N_3)
This is the issue, my add() method, I know that a function within a class, self should be the first argument, however when I put self in there I get this, it's not accepting my other arguments:

N_3 = ComplexNumber.add(N_1, N_2)
TypeError: add() missing 1 required positional argument: 'C2'

When I run it without self as an argument, I get this as my output, which is correct results, just needs formatting:

0.0 + 0.0 * i
12.0 + 8.0 * i
-5.0 + -3.0 * i
5.83 + 4.2 * i

I am confused why my program will not work with self? and also, what is the most efficient way to format this output to have proper decimal spaces
Reply
#2
Writing an add method does not override the + operator. You need to write an __add__ method (with two underscores before and after the name, this is called a dunder method). The parameters of the method will be self and other. So if you have two objects of the class C1 and C2, and do C1 + C2, that will call C1.__add__(C2), with C1 automatically assigned to the self parameter and C2 assigned to the other parameter.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you, got it working
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020