Overloading and? was <RE: Should I prefer an external database>

Ian Bicking ianb at colorstudy.com
Tue Apr 22 14:46:35 EDT 2003


On Tue, 2003-04-22 at 13:38, Andrew Dalke wrote:
> BTW, what I've done for other projects which build up a parse
> tree then evalute it is to hijack the arithmetic operators.  I've
> used '*' for and, '+', for or, and '-' for 'but not'.  In that case, the
> above expression is
> 
> > for paper in Paper.select(Paper.q.title == 'foo' +
> >                                  Paper.q.author.startswith('Bob')):

This won't work, though, because of the order of precedence.  It parses
like (Paper.q.title == ('foo' + Paper.q.author.startswith('Bob'))).

Better operators are &, |, and ~ (the binary logic operators).  You can
use these with SQLObject, but I don't like to, because they have the
same problems of precedence and they are more trouble than they are
worth.  AND() isn't pretty, but it's very straight-forward to use and
unlikely to cause surprise.

  Ian







More information about the Python-list mailing list