On Mon, Jul 26, 2010 at 9:53 AM, Oleg Broytman <phd@phd.pp.ru> wrote:
On Mon, Jul 26, 2010 at 07:28:30AM -0700, Guido van Rossum wrote:
> other odd ducks are 'and' and 'or', though in a pinch one can use '&'
> and '|' for those. I forget in which camp 'not' falls.

  'not' is like 'and' and 'or'. '~' is like '&' and '|'; its magic method
is __invert__.

~ actually works okay (except for it reading poorly -- it's a rather obscure operator), but & and | have precedence that makes them very error-prone in my experience, e.g., (query.column == 'x' | query.column == 'y') becomes the SQL "(column == ('x' OR column)) == 'y'".

I know overriding "and" and "or" has been discussed some time ago, though I'm not sure what the exact reason is that it never went anywhere.  I remember it as one of those epic-and-boring comp.lang.python threads ;)  One obvious complication is that they are short-circuit operations.  That is, "a and b" must evaluate a, figure out if it is false, and if so then it returns a.  But "a or b" must evaluate a, figure out if it is TRUE, and if so then returns a.  So if there was anything like __and__ and __or__ it would have to simply disable any short-circuiting.

Ian Bicking  |  http://blog.ianbicking.org