[Numpy-discussion] (a and b) != (b and a) ?

David M. Cooke cookedm at physics.mcmaster.ca
Wed Jun 12 10:48:01 CDT 2002


At some point, Geza Groma <groma at nucleus.szbk.u-szeged.hu> wrote:

> Using Numeric-21.0.win32-py2.2 I found this:
>
> Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from Numeric import *
>>>> a = array((1, 1), 'b')
>>>> b = array((1, 0), 'b')
>>>> a and b
> array([1, 0],'b')
>>>> b and a
> array([1, 1],'b')
>>>>
>
> It looks like a bug, or at least very weird. a&b and b&a work correctly.

Nope. From the Python language reference (5.10 Boolean operations):

The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is
returned.

Since in your case both a and b are true (they aren't zero-length
sequences, etc.), the last value will be returned.

It works for other types too, of course:

Python 2.1.3 (#1, May 23 2002, 09:00:41) 
[GCC 3.1 (Debian)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> a = 'This is a'
>>> b = 'This is b'
>>> a and b
'This is b'
>>> b and a
'This is a'
>>> 

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm at mcmaster.ca




More information about the Numpy-discussion mailing list