Using '__mul__' within a class

Piet van Oostrum piet at cs.uu.nl
Sat Sep 24 17:14:16 EDT 2005


>>>>> James Stroud <jstroud at mbi.ucla.edu> (JS) wrote:

>JS>      def Multiply(self, other):
>JS>          self.a = self.a * other.a + self.b * other.b
>JS>          self.b = self.a * other.b + self.b * other.c
>JS>          self.c = self.b * other.b + self.c * other.c

I gues this will give the wrong result because the calculation of self.b is
supposed to use the original value of self.a, not the newly calculated one.
Similar for self.c. The following should do that:

self.a, self.b, self.c = (self.a * other.a + self.b * other.b,
                          self.a * other.b + self.b * other.c,
                          self.b * other.b + self.c * other.c)
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list