python: bug or feature?

Grant Edwards ge at nowhere.none
Wed Aug 9 18:02:45 EDT 2000


In article <8msimd$bia$1 at nnrp1.deja.com>, Keith Murphy wrote:

>the first one works, but the second one always returns a 0.
>
>( (e%4 == 0) & (self.current == e-1) )
>
>( e%4 == 0 & self.current == e-1 )
>
>is there some reason the ordering in python can't accept the second
>one?  thanks,

It can and does.  Maybe it doesn't do what you want it to...

"&" is the bitwise-and operator, and it has a higher precidence
than the '==' comaprison operator (just like '-' and '%' do),
so you end up with

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

I don't know how '==' group, but well just guess left-to-right:

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

Since I don't know what you're trying to do, I've no idea if
this is the above is the right expression, but my first guess
is that you want the logical boolean operator 'and' rather than
the bitwise operator '&':

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

-- 
Grant Edwards                   grante             Yow!  Yow!! That's a GOOD
                                  at               IDEA!! Eating a whole FIELD
                               visi.com            of COUGH MEDICINE should
                                                   make you feel MUCH BETTER!!



More information about the Python-list mailing list