overloading __mul__

Mathias Waack mathias-usenet at valpo.de
Wed Mar 12 10:56:51 CET 2003


zunlatex at hotmail.com (zunbeltz) wrote:
> I want to be able to write things like that 
>   2*matrix 
>   matrix * 2
>   2*vector
>   vector *2
> but also
>   matrix*vector
>   vector*matrix
> 
> How can i overload __mul__ for matrix in two ways depending if the
> other elemetn is a number or a vecotr? 

This doesn't work. But you can test the type of the argument of the __mul__    
method and call different operations. 

> and in wich class have i to
> overload __mul__ to get vector * matrix and matrix * vector; in the
> vector class? in the matrix class? in both?

In both. 

Mathias




More information about the Python-list mailing list