Newbie: Classes
Andrew Bennetts
andrew-pythonlist at puzzling.org
Mon Oct 27 00:31:49 EST 2003
On Mon, Oct 27, 2003 at 12:08:39AM -0500, Sean Ross wrote:
>
> "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)
Uh, I don't think you want to mutate this instance...
-Andrew.
More information about the Python-list
mailing list