Newbie: Classes

Sean Ross sross at connectmail.carleton.ca
Mon Oct 27 00:08:39 EST 2003


"Michael Loomington" <mloomington at yahoo.ca> wrote in message
news:bni80i$11dkvg$1 at ID-198839.news.uni-berlin.de...
> 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)
>

Hi. Python allows you to define behaviour for both left and right
multiplication (instance*2 or 2*instance). See if this helps:

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

def __rmul__(self, m):
    return self.__mul__(m)

Sean







More information about the Python-list mailing list