Newbie: Classes

Michael Loomington mloomington at yahoo.ca
Mon Oct 27 00:04:22 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)
>
>
Wait, I just create new variables and assign them to self.r*m and it works.






More information about the Python-list mailing list