[Numpy-discussion] logical_and operator, &&, is missing?
Eric Firing
efiring at hawaii.edu
Sat Jun 24 19:08:00 EDT 2006
Robert Kern wrote:
> Eric Firing wrote:
>
>>That makes sense, and implies that the real solution would be the
>>introduction of operators && and || into Python, or a facility that
>>would allow extensions to add operators. I guess it would be a matter
>>of having hooks into the parser. I have no idea whether either of these
>>is a reasonable goal--but it certainly would be a big plus for Numpy.
>
>
> I don't really see how. We already have the & and | operators. The only
> difference between them and the && and || operators would be that the latter
> would automatically coerce to boolean arrays. But you can do that explicitly, now.
>
> a.astype(bool) | b.astype(bool)
>
Another difference pointed out in the Wiki is precedence, which requires
one to be more careful about parentheses when using the bitwise
operators. This arises because although the bitwise operators
effectively do the right thing, given boolean arguments, there really is
a difference between & and &&--that is why C, for example, has both.
Using & when one means && is a hack that obscures the meaning of the
code, and using logical_and is clear but cluttered--a significant step
away from the goal of having code be clear, concise and readable.
I suspect that many other people will trip over the lack of && in the
same way that I have, and will similarly consider it an irritant that we
work around because we have to, not because it is good.
> Of course, it's highly likely that you are applying & and | to arrays that are
> already boolean. Consequently, I don't see a real need for more operators.
>
> But if you'd like to play around with the grammar:
>
> http://www.fiber-space.de/EasyExtend/doc/EE.html
>
Interesting, thanks--but I will back off now.
Eric
More information about the NumPy-Discussion
mailing list