[Python-ideas] Infix functions
Steven D'Aprano
steve at pearwood.info
Mon Feb 24 16:28:27 CET 2014
On Mon, Feb 24, 2014 at 11:08:47AM +0900, Stephen J. Turnbull wrote:
> Steven D'Aprano writes:
>
> > I can't think of many useful examples of infix operators that aren't
> > mathematical, and even fewer that aren't just as easily written as
> > methods or functions.
>
> The copula, not to mention all non-mathematical transitive verbs, in
> English? This feature would make it a lot easier to write DSLs in
> Python. (I don't know if that would necessarily be a good thing.)
Copula are "linking verbs" like "be", "is", "was" or "becomes".
Under what circumstances would you want to write such operators? Using a
single backtick ` as the "custom operator" syntax, I came up with:
marriage = two `become one
while not (self `be all_that_you_can_be):
self.improve()
but really, these are just jokes. I'm still no closer to actual
use-cases.
Any infix binary operator a `op b can be written as a function of two
arguments, op(a, b). It's not that there are no use-cases for infix
binary operators, but that all the obvious ones (such as string
concatenation, comparisons, list repetition, etc.) already exist and the
rest can nearly always be easily written as functions.
--
Steven
More information about the Python-ideas
mailing list