[Tutor] A complex number class, making a class instance of same class inside one of its methods

Alan Gauld alan.gauld at yahoo.co.uk
Wed Aug 25 14:25:01 EDT 2021


On 25/08/2021 18:50, Manprit Singh wrote:

> I have written a class Complex_num,

Given that Python has a built-in complex number type,
that's probably not the best learning example you could
have chosen. However...


>  from which i can set the values of real
> and imaginary part of complex number(setvalue)

Numbers in Python are immutable so to follow that example
you should try to make your objects follow similar patterns.
Thus setting the real/imaginary parts after the number
as been created is probably a mistake, instead perform
an operation that returns the new values and use those
to create a new object.


>  , and compare the magnitude
> of two instances of this class(__gt__()) . Add real and imaginary parts of
> two instances of this class(__add__()) and print the values of instance
> objects with __str__().

Thats all good stuff. The built-in version does a lot more of course!

> class Complex_num:
> 
>     def __init__(self):
>         self.real = 0
>         self.imag = 0

Why not pass these attributes as parameters with defaults of zero:

def __init__(self, real=0, imag=0):
    self.real = real
    self.imag = imag

And now you initialise as

zer_comp = Complex_num()
val_comp = Complex_num(3,4)


>     def setvalue(self, real, imag):
>         self.real = real
>         self.imag = imag

You then don't need this which probably shouldn't exist anyway.

>     def __gt__(self, com_num):
>         c1 = (self.real**2 + self.imag**2)**0.5
>         c2 = (com_num.real**2 + com_num.imag**2)**0.5
>         return c1 > c2
> 
>     def __add__(self, com_num):
>         temp = Complex_num()
>         rpart = self.real + com_num.real
>         ipart = self.imag + com_num.imag
>         temp.setvalue(rpart, ipart)
>         return temp

If you put the attributes in the Init():

     def __add__(self, com_num):
         rpart = self.real + com_num.real
         ipart = self.imag + com_num.imag
         return Complex_num(rpart,ipart)

>     def __str__(self):
>         sign ="+" if self.imag >= 0 else ""
>         com = "("+str(self.real)+sign +str(self.imag)+"j)"
>         return com
> 
> x = Complex_num()
> x.setvalue(4, 1)
> y = Complex_num()
> y.setvalue(4, -5)
> print(x)  - returns (4+1j)
> print(y) - returns (4-5j)
> x > y returns False
> print(x+y) returns (8-4j)
> 
> The answers are correct , but i have a question :
> 
> Can we call the class inside any method of the class ? If I need an
> instance object of the class inside one of the methods of the same class.

Yes, you can create instances inside methods of the class.
That is perfectly normal in OOP.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list