Defining new infix operators in Python
code.activestate.com
code.activestate.com
Hope someone finds it useful, though to be honest, I don't think this sort of thing is very commonly useful.
Still, I really like the inrex module in the last comment. Great extension to the very clumsy re.
If you want to do this level of hacking with the language, why not just use Ruby?
Go, check out Ocaml or Haskell, even some Lisp. Operators are functions. And there's nothing mythical around it.
It's only that most parsers weren't able to handle user-defined operators for a long time that makes the idea seem so foreign. (And C++'s `clever' idea to re-define operators already in use did not help the situation, either.)
It has nothing to do with the concept of defining new infix operators. It is to do with hacking the language in a way that is unexpected and (initially) confusing.
Hacks like this are clever and interesting, but not Pythonic. Doing this over using an "ordinary" function just makes Python harder to read because it is not conventional and not idiomatic. Any programmer competent in the language should be able to dive in without having to understand which set of hacks have been used. It doesn't matter whether it is mythical or not; it should not be used because it is a hack.
Perhaps somebody will even write up a PEP for properly defining your own operators?
For eg:
sub infix:<.oO>($name, $thought) {
say "$name thinks $thought"
}
"szabgab" .oO "Perl 6 is cool";
ref: http://news.ycombinator.com/item?id=1607254It's useful to know how to achieve these things without throwing the baby out with the bathwater.
Also, not everyone who uses Ruby likes crap like this. Just as not everyone who uses Python dislikes crap like this.