[Numpy-discussion] logical_and operator, &&, is missing?
efiring at hawaii.edu
Sat Jun 24 15:57:19 CDT 2006
Robert Kern wrote:
> Eric Firing wrote:
>>Robert Kern wrote:
>>>Eric Firing wrote:
>>>>It seems that the logical operators || and &&, corresponding to
>>>>logical_or and logical_and are missing; one can do
>>>>z = logical_and(x,y)
>>>>z = x && y
>>>>Is there an inherent reason, or is this a bug?
>>>Python does not have a && operator. It has an "and" keyword, but that cannot be
>>>overridden. If you know x and y to be boolean arrays, & and | work fine.
>>Out of curiosity, is there a simple explanation as to why "and" cannot
>>be overridden but operators like "&" can? Is it a fundamental
>>distinction between operators and keywords?
> Sort of. "and" and "or" short-circuit, that is they stop evaluating as soon as
> the right value to return is unambiguous.
> In : def f():
> ...: print "Shouldn't be here."
> In : False and f()
> Out: False
> In : True or f()
> Out: True
> Consequently, they must yield True and False only.
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.
More information about the Numpy-discussion