[Python-ideas] Infix matrix-multiply, but not general infix operators?
Andrew Barnert
abarnert at yahoo.com
Fri Mar 21 23:09:06 CET 2014
On Mar 21, 2014, at 13:06, Mark Daoust <daoust.mj at gmail.com> wrote:
> >>> spam `spam` eggs `baked` beans `spam` spam
>
> I'm not sure if the backticks help.
>
> When you read that, I think you're reading the spacing not the ticks.
No, you're reading a combination of the two. Compare it to the same without backticks:
>>> spam `spam` eggs `baked` brand `spam` spam
>>> spam spam eggs baked beans spam spam
>>> spam spam eggs baked beans spam spam
There's no way to differentiate the operators from the operands with _just_ spacing, but with spacing plus anything else, there is.
And lest you think this is just an artifact of passing spam to spam, here it is with more realistic names;
>>> mata mmul matb mmul matb mmul vec
>>> mata `mmul` matb `mmul` matb `mmul` vec
No matter how much you like or dislike the latter, the former is clearly a lot worse.
> You can already do something very similar by using built-in operators to "quote" the custom "operators", if you set the appropriate operator overloads.
Yes, I mentioned the |op| hack the last time I brought this up, and two other people suggested it after that. But if you want to go over it again: no symbol has a precedence right below the bitwise ops, comparison operators don't work because of chaining, there are types that will handle __op__ so your fake operator doesn't get a chance to do its __rop__, there's no way to prevent "2 |op" from being a valid but confusing expression (and there's a temptation to use it as a half-assed form of operator sectioning), it's hard for syntax highlighters and linters and the like to understand |op| is an operator, there's no way to restrict arbitrary expressions as operators, you have to declare all infixable operators with a decorator (as opposed to having the language choose whether all functions are infixable, or we use a decorator, or something else), any symbol you use already has another meaning that will confuse people, it's a bit less efficient, and, finally, it's not a consistent language feature (having |op| be an operator in one module, *op* in another, etc. makes the language harder to learn).
More information about the Python-ideas
mailing list