[Python-ideas] Infix matrix-multiply, but not general infix operators?

Andrew Barnert abarnert at yahoo.com
Thu Mar 20 17:06:09 CET 2014


On Mar 20, 2014, at 3:24, Steven D'Aprano <steve at pearwood.info> wrote:

> On Thu, Mar 20, 2014 at 12:41:40AM -0700, Jared Grubb wrote:
> 
> The beauty of infix operators is that they give an extremely compact 
> representation, which is valuable for mathematics but not so much for 
> general purpose computing. Consequently, it would be useful to write 
> something like:
> 
>    matrix @ vector
> 
> versus
> 
>    matrix.mult(vector)
> 
> because the symbol for the operator is so compact. Having to write it 
> as this instead:
> 
>    matrix`mult`vector
> 
> saves you one character over the method syntax,

It's not about saving one character, it's about saving the (possibly overly-nested) parentheses. The PEP explains that, as does the earlier thread, and all of the previous threads and PEPs on related ideas, so I won't go into it again.

However, the other answers are right; matrix multiplication _is_ special. As an alternative to the 2000 PEP that proposed a suite of new operators by allowing ~ as a prefix to 11 existing operators )or to the other proposal to allow any string of operator characters to be defined as an operator), I think general infix wins. However, the NumPy community is no longer suggesting either of those; after 14 years of living with the limitations of the current system, they've decided that only one operator is really a problem. And against that proposal, general infix loses. One is a good number of special cases for the same reason 11 is a bad number of special cases.

Of course I still like my general infix proposal, but even if we had it, I think @ for matmul might be worth adding anyway.


More information about the Python-ideas mailing list