[Python-ideas] Infix functions

Andrew Barnert abarnert at yahoo.com
Sat Feb 22 02:58:39 CET 2014


Apologies for the mess I'm likely to make trying to reply to a top-post…

From Greg Ewing:

>> For me that totally misses one of the main points of
>> infix operators, which is that they're *concise*. There's
>> little advantage in writing the above rather than


No, it's not about conciseness, it's about readability.

I included longer examples in my original post, and repeated them again because people apparently missed them, but here's one of the examples yet again:

    a `cross` b + c `cross` (d `dot` e)


vs.

    add(cross(a, b), cross(c, dot(d, e))


Sure, it's only one character shorter (or four characters using @cross instead of `cross`), but you can't tell me it's not more readable. The function-calling version is out of order, and has an extra level of nesting, and has a lot more syntactic noise.

And look at my motivating example from Haskell: the infix `catch` is not any more _concise_ than the prefix version, but it puts things in the right order (the same order as the proposed exception expression for Python)—and that's why Haskell doesn't need an exception expression. (Well, that plus the ability to create functions from most expressions without lambda, but that's a separate issue.) Again, I'm not sure there are many such cases in Python (after all, 2-argument higher-order functions make up half of Haskell's library, while Python only has a handful of them…), but if there are, they'd benefit the same way.

From Haoyi Li:

>On the other hand, it does give you the other reason for infix operators, which is that the evaluation order of the things happening flow from left to right, rather than right-to-left or around in a spiral.


Exactly.


More information about the Python-ideas mailing list