Saturday, 11 October 2008

Operators

In Python, most other operators have higher priority than not, this means
not 1 == 2
gives us the result we'd expect, being True. I've had a lengthy discussion on IRC with someone stating that not has the higher priority, which it hasn't. It's very easy to observe in the example above, because not 1 evaluates to False, and so does False == 2. So the result of the example would be False, which it is not. What it really does is evaluate 1 == 2, which is False, and then not takes the opposite, which is True.

Why am I discussing this anyway? Today, I've written a tool that converts not foo == bar to foo != bar, not a in b to a not in b, not a not in b to a in b and so forth. It's about 70 lines of source code that I will never understand when I re-read it, as it is full of regular expressions.

I will be releasing it to public once I've cleaned up the code a bit and decided on which license to use, meaning to choose from MIT and GNU GPL.