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

Brandon W Maister bwmaister at gmail.com
Fri Mar 21 02:28:03 CET 2014


On Mar 20, 2014 8:16 PM, "Steven D'Aprano" <steve at pearwood.info> wrote:
>
> On Thu, Mar 20, 2014 at 05:04:25PM -0400, Brandon W Maister wrote:
>
> > Actually makes me think of decorators, which means that you wouldn't
even
> > need new tokens or keywords:
>
> But you would require new syntax. Your suggested syntax
>
>     s = 'a' bop 'b'
>
> is currently a syntax error.

100% agree, I was careful with my language, if not my thinking :-)


> Keep in mind that any changes to syntax would allow bop to be any
> arbitrary object. Python cannot tell at compile-time whether bop is an
> "operator" or a list or a string or a float, only at run-time.

I admit that when writing I didn't think about the fact that I was
suggesting adding dynamic names to what is currently a static table.

> >     from functools import make_operator
> >     bop = make_operator(b)
>
> Do you have any suggestion as to how make_operator would work? What
> should it do?

Originally I was thinking something along the lines of "just add it to the
'operators' dict" but obviously that won't work since there's no such thing.

Before I defend the idea any further I need to point out that I don't think
it's a *good* idea. I kind of like the idea of arbitrary infix functions,
so I threw out an idea that I haven't seen before.

We could, hypothetically, create an "operators" dict (or dicts) alongside
locals and globals. Then, if an unexpected token shows up in operator
position (which is probably well-defined?) before a SyntaxError is raised
the token is looked up in the operators dict.

I think that the biggest problem I have with that solution is that
philosophically it seems like it would take Python a bit far into the
lisp-2 camp, which I would not like.

> What sort of object is bop?
A regular function that takes one or two args. Honestly the nice thing
about the "look it up somewhere if it causes the right kind of SyntaxError"
solution is that it would probably also allow arbitrary unary operators.
Although why anyone would want *that* is beyond me, so maybe it's not nice
:-)

bwm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140320/48bd111a/attachment.html>


More information about the Python-ideas mailing list