Newbie: Classes

Michael Loomington mloomington at yahoo.ca
Sun Oct 26 23:47:15 EST 2003


I think i'm getting the hang of it.  But if I want to implement
multiplication? i.e. If c is an instance of Complex, then c*2 would multiply
r*2 and i*2 and return a new class that has the number multiplied by 2.
>>> class Complex:
...     def __init__(self, realpart, imagpart):
...         self.r = realpart
...         self.i = imagpart

I tried doing the following but it doesn't work:

def __mul__(self, m)
    self.r= self.r * m
    self.i= self.i * m
    return Complex(self.r, self.i)






More information about the Python-list mailing list