[Numpy-discussion] logical priority

Fernando Perez Fernando.Perez at colorado.edu
Wed Feb 9 10:24:27 CST 2005


Stephen Walton wrote:
> Fernando Perez wrote a good deal clarifying how Python's logical 
> operators work, for which I'm grateful.  One brief comment:
> 
> 
>>Bytecode analysis can be really useful to understand this kind of 
>>subtle issues.  I can't think of any language where this approach is 
>>as easy to do and friendly as in python.
> 
> 
> Pardon me, but this sounds a bit like looking at the assembly language 
> Fortran generates in order to understand the subtleties of that language 
> :-) .  I know people who used to do that, and were proud of it, but I'd 

Oh, and it is :)  It's just that it's so immediate to do it in python, that it 
actually becomes a normal approach.  I've often used it to clarify behavior or 
performance issues to myself.  I'm not proud of it, it's just a useful trick 
which can come in handy in certain cases.

> rather the language itself didn't behave in surprising ways.  At least 
> in Fortran, 'a .and. b' only made sense if a and b were both of type 
> LOGICAL;  in fact, it was a compile time error if they weren't.

You can argue that this is a wart (I don't think it is), but it was certainly 
done by design:

http://docs.python.org/ref/bitwise.html#l2h-423
http://docs.python.org/ref/Booleans.html#l2h-440

'and' is meant to provie truth checking, with (critically important in many 
applications) short-circuit behavior.  '&' is a binary operation (hence 
necessarily non short-circuiting, due to python's argument evaluation rules), 
most often used for bitwise manipulations.  It seems clear to me that they 
both have a valid use.

> Anyway, we've wandered afar from the main point:  what is the best way 
> to duplicate the functionality of the following MATLAB snippet:
> 
> f = ~isnan(data1) & ~isnan(data2);
> plot(x,data1(f),x,data2(f))

I don't have a clean answer for this one, sorry.

Best,

f




More information about the Numpy-discussion mailing list