Operator symbol for "nb_intdivide"

Peter Hansen peter at engcorp.com
Fri Jul 27 21:30:42 EDT 2001


Bernhard Herzog wrote:
> 
> Guido van Rossum <guido at zope.com> writes:
> 
> > ...  Programmers coming from languages where only
> > "|" existed were tempted to use that, but the priorities are so broken
> > that a==b | c==d is parsed as (a == (b|c)) == d.
> 
> Interesting that even a language designer with lots of C-experience can
> get this wrong :) | has in fact almost the same precedence as || and
> it's in fact lower than the comparison operators, so that a==b | c==d is
> actually parsed as the user expected ( (a == b) | (c == d)) so it even
> works as expected, although in cases where the operands are e.g. a
> normal int and a comparison this is likely to break down.
> 
> The problem with |'s and &'s precedence in C is that in an expressions
> like 0x0f == a & 0x0f, which seems to test whether the 4 lower bits of a
> are 1, is in fact parsed as (0x0f == a) & 0x0f.

But good C programmers, whether language designers or not, would 
write both of the above with parentheses to show clearly what the
desired precedence was.  They would not rely on the assumption that 
the next programmer to read the code would know C precedence rules 
perfectly:

 (a == b) | (c == d)

 0x0f == (a & 0x0f)

Whether or not "|" or "==" are correct is not my point;
that getting caught by precedence errors is just plain sloppy, is.

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list