Infix Operators in Python January 22, 2012
As you may already know, there are 3 kinds of operators calling-notations: prefix (+ 3 5
),
infix (3 + 5
), and postfix (3 5 +
). Prefix (as well as postfix) operators are used
in languages like LISP/Scheme, and have the nice property of not requiring parenthesis —
there’s only one way to read an expression like 3 5 + 2 *
, unlike 3 + 5 * 2
.
On the other hand, it reduces code readability and the locality of operators and their arguments.
This is why we all love infix operators.
Now imagine I have a function, add(x,y)
, and I have an expression like add(add(add(5,6),7),8)
…
wouldn’t it be cool if I could use infix notation here? Sadly though, Python won’t allow you to
define new operators or change how functions take their arguments… but that doesn’t
mean we have to give up!
Haskell, for instance, allows you to define custom operators and set their precedence, as well
as invoking “normal” functions as infix operators. Suppose you have a function f(x,y)
— you
can invoke it like f 5 6
or 5 \`f\` 6
(using backticks). This allows us to turn
our previous expression, add(add(add(5,6),7),8)
, into 5 \`add\` 6 \`add\` 7 \`add\` 8
,
which is much more readable. But how can we do this in Python?
Well, there’s this Cookbook recipe that provides a very nice way to achieving the same functionality in Python (adapted a little by me):
Using instances of this peculiar class, we can now use a new “syntax” for calling functions as infix operators:
Surrounding decorated functions with pipes (bitwise ORs) allows them to take their parameters infix-ly. Using this, we can do all sorts of cool things:
And even curry functions:
Ain’t that cool?