-2

I tried creating a Complex number class. I have an add method in the class which returns a new complex number after adding, without changing the argument complex numbers.

code

class complex:
    real = 0.00
    imag = 0.00

    def __init__ (self,r,i):
        complex.real = r
        complex.imag = i

    def add(comp1, comp2):
        x = comp1.real + comp2.real
        y = comp1.imag + comp2.imag
        result = complex(x,y)
        return result

Something is wrong with this code. I cannot find it. Please tell me where I went wrong.

enter image description here

I also tried passing self object but it didin't work. def add(self, comp):

Charan
  • 1,051
  • 3
  • 12
  • 32
  • 1
    First of all why are you updating class attributes? Use `self`. – Ashwini Chaudhary Aug 07 '15 at 06:56
  • This has already been asked time and time again, the problem is that you use class variables (which are shared among all instances) instead of instance variables (which are specific for each instance). I can't find the original question right now. – skyking Aug 07 '15 at 07:03
  • possible duplicate of [How do I avoid having Python class data shared among instances?](http://stackoverflow.com/questions/1680528/how-do-i-avoid-having-python-class-data-shared-among-instances) – skyking Aug 07 '15 at 07:08

3 Answers3

2
class complex:
    real = 0.00
    imag = 0.00

    def __init__ (self,r,i):
        complex.real = r
        complex.imag = i
  1. You're using class properties which are globally the same for all instances of complex. You only want to assign those values to self, not complex itself.
def add(comp1, comp2):
    x = comp1.real + comp2.real
    y = comp1.imag + comp2.imag
    result = complex(x,y)
    return result
  1. You're missing the self parameter, or you want to make the method a @staticmethod.

Correct implementation:

class Complex:
    def __init__ (self, real=0.00, imag=0.00):
        self.real = real
        self.imag = imag

    def add(self, comp):
        return Complex(self.real + comp.real, self.imag + comp.imag)


c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1.add(c2)
Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    Your `add` method is exactly the same as OP's one, just using a different naming. (even though I understand that using such naming, the OP understanding of a method might not be complete.) – 301_Moved_Permanently Aug 07 '15 at 07:19
  • Yes, strictly speaking that's true, but as you say, it's probably an indicator of a misunderstanding. – deceze Aug 07 '15 at 07:21
1

When you do -

complex.real = r
complex.imag = i

This is the reason for your issue, when you create a new instance of complex class, the real and imag values for the class change to the new one, and this is reflected across all instances of the complex object.

real and imag are actually class variables, that are shared accross all objects/instances of the class. You should define them as instance variables as -

def __init__ (self,r,i):
    self.real = r
    self.imag = i

self points to the current instance, so when you do - self.real it points to the real variable of the current instance/object.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

In your __init__, you should refer to self instead of complex for object / dynamic properties, otherwise you're modifying them as class / static properties:

def __init__ (self,r,i):
    self.real = r
    self.imag = i
shevron
  • 3,463
  • 2
  • 23
  • 35