[Python-Dev] Infix operators

Fredrik Johansson fredrik.johansson at gmail.com
Fri Jul 25 02:02:41 CEST 2008


On Fri, Jul 25, 2008 at 12:06 AM, Scott Dial
<scott+python-dev at scottdial.com> wrote:

> Perhaps I'm nobody, but I think this would be ridiculous. Matrices are
> not native objects to the language. There is no type(matrix). The notion
> of what makes a Python object a matrix is a convention and to have
> built-in operators dedicated to such objects makes no sense. There are
> multiple ways to stuff matrices into Python. Please submit a PEP for a
> type(matrix) first. Until a matrix is a first-order object in Python,
> there is no logic to making operators for them.

Though I would personally find a matrix multiplication operator
useful, I have to agree with this.

Anyway, it is easy to define pseudo-operators in Python; just create
an Operator class and implement its __mul__ and __rmul__ methods
appropriately (there are recipes for this around somewhere). Then you
can define various custom multiplication operators with syntax like
this:

A *matrixmul* B
A *dot* B
A *cross* B
A *elementwise* B

Some other fun possibilities:

A +concat+ B
A /solve/ B
A **left_inverse** (-1)
A **right_inverse** (-1)
x **tetrate** y
n |choose| k

Fredrik


More information about the Python-Dev mailing list