Regarding Infix Operators January 25, 2012
I got some reactions to the Infix Operators post, and wanted to point out some things. First of all, I’m not the one who came up with it – it’s a recipe from the Python Cookbook that’s been posted in 2005. I’m not taking credit for it or anything, I just said I loved the idea and I adapted the code a little.
Second, regarding coding style or the pythonicity of this scheme – let’s be clear, it’s a hack.
In order for it to work, the two arguments must refuse to support __or__
on Infix
objects,
which means you can’t compose them properly:
It’s also magical and unpythonic by nature. You can read in the cookbook comments about a less-experienced programmer who complained he had to wrap his head around around this. I’d say this feature is a kin to metaclasses: they are useful (at times), but there are better, more pythonic ways to do the same without the magic.
So why is it useful? First of all, it’s an interesting pattern, worth more knowing about than actually using it. But on the more practical side, it could be very useful in domain-specific languages (DSL), where it could increase your expressiveness. Consider something like Construct, where you define data structures declaratively:
We could replace all sorts of built-in constructs by such “operators”, thus building “data expressions”. So here’s a very early sketch of what it could look it:
Beware: I just made this up, there’s no solid concept behind it.
See Also
If you liked the infix operators idea, have a look at https://github.com/JulienPalard/Pipe, which allows for easy functional composition in python.