I guess I need to remind folks about the reasons why infix operators are useful. It's not (primarily) about the lack of parentheses or the resemblance to English (oh horror). It's about the *associative property*.
For example, it's a good idea to use a binary operator for string concatenation, since (s1 `concat` s2) `concat` s3 produces the same result as s1 `concat` (s2 `concat` s3), so it's reasonable to write it as s1 `concat` s2 `concat` s3 rather than as concat(s1, concat(s2, s3)) or concat(concat(s1, s2), s3). And sure, you could improve by having a varargs concat() function (which we do: "".join(s1, s2, s3)), but that only goes so far. But for a binary operation that doesn't have this useful (and rare!) property, spelling it as an infix operator doesn't buy you much. This would explain why `json.dump` makes a poor operator (and probably `round` too, though I honestly have no idea what it does). There's also the benefit of being able to combine *different* operators without having to use parentheses (like 2*x + 3*y), but that depends strongly on whether people can remember the priority of the various operators. (Python, following C, takes this a bit too far, and I have to look up the associativity of shift and bitwise operators.) For newly minted operators like `round` this benefit goes away -- presumably there's only one priority for that form, and it probably is lower than all other operators. Finally there's the benefit of matching common mathematical operators that are traditionally spelled as infix operators -- but most people know only a handful of these, and some of those aren't even associative (e.g. subtraction of vector cross product). Nevertheless, it's nice to be able to follow the mathematical tradition, as long as the typical programmer is familiar with that tradition. (Maybe there's a traditional "round" operator in some field of math commonly practiced by numpy users that I've never heard of?) --Guido On Fri, May 1, 2020 at 7:42 PM Andrew Barnert via Python-ideas < python-ideas@python.org> wrote: > On May 1, 2020, at 15:35, Steven D'Aprano <st...@pearwood.info> wrote: > > > > but if it is all functions, then I think you have no choice but to > > either live with it or shift languages, because the syntax for functions > > is too deeply baked into Python to change now. > > Actually, I’m pretty sure Python could add infix calling without > complicating the grammar much, or breaking backward compatibility at all. I > don’t think it *should*, but maybe others would disagree. > > The most obvious way to do it is borrowing straight out of Haskell, so > this: > > x `spam` y > > … compiles to exactly the same code as this: > > spam(x, y) > > That should be a very easy change to the grammar and no change at all to > the later stages of compiling, so it’s about as simple as any new syntax > could be. It doesn’t get in the way of anything else to the parser—and, > more importantly, I don’t think it’s confusable as meaning something else > to humans. (Of course it would be one extra thing to learn, like any syntax > change.) Maybe something like $ instead of backticks is better for people > with gritty monitors, but no point bikeshedding that (or the precedence) > unless the basic idea is sound. > > Anyway, it’s up to the user to decide which binary functions to infix and > which to call normally, which sounds like a consenting-adults issue, but… > does it _ever_ look Pythonic? > > For this particular use case: > > isa = isinstance > > thing `isa` Fruit and not thing `isa` Apple > > … honestly, the lack of any parens here makes it seem harder to read, even > if it is a bit closer to English. > > Here’s the best use cases I can come up with: > > xs `cross` ys > array([[0,1], [1,1]]) `matrix_power` n > prices `round` 2 > > These are all things I have written infix in Haskell, and can’t in > Python/NumPy, so you’d think I’d like the improvement… but if I can’t have > real operators, I think I want dot-syntax methods with parens instead in > Python: > > prices.round(2) > > And outside of NumPy, the examples seem to just get worse: > > with open(path, 'w') as f: > obj `json.dump` f > > Of course maybe I’m just failing to imagine good examples. > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/AQPPHKL4EMFMT5NPB66W4GAFMGE5YYAB/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/RFOKEXZLTW7D7BHDDZUQ4IHPUQXKDEUB/ Code of Conduct: http://python.org/psf/codeofconduct/