python: bug or feature?

Bernhard Herzog herzog at online.de
Wed Aug 9 18:32:20 EDT 2000


Keith Murphy <kpmurphy at my-deja.com> writes:

> the first one works, but the second one always returns a 0.
> 
> ( (e%4 == 0) & (self.current == e-1) )

You probably meant to use 'and' instead of '&' (& is bitwise and):

   ( (e%4 == 0) and (self.current == e-1) )

which is indeed the same as 

   ( e%4 == 0 and self.current == e-1 )

> ( e%4 == 0 & self.current == e-1 )

This one is equivalent to 

  ( e%4 == (0 & self.current) == e-1 )

which is indeed always 0, assuming e is an integer.

If you come from C or C++, note, that &, | and ^ have higher precedence
than the comparison operators and that the comparison operators can be
chained, i.e. a < b < c does what you'd expect from maths and not what
you'd expect from C.

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list