[Python-Dev] Conditional expressions and sequences (original) (raw)

Paul F. Dubois paul@pfdubois.com
Tue, 16 Oct 2001 08:49:13 -0700


One of the most cherished of Numpy operations is:

x = where (condition, t, f)

which creates an array x, shaped like condition. Each element of this result is set to the corresponding element of t or f depending on whether the corresponding element of condition is true or false. Either or both of t and f can be a scalar but otherwise must be the same shape as condition.

The reason this is cherished is, of course, that it avoids a scalar loop with an if-test in it. Scalar loops make Nummies cringe.

Now in your new syntax this is essentially

x = if condition then t else f

Therefore it would be cool if the implementation was x=condition.where(t,f). This slot could be overwritten by classes like Numeric's array. If condition was a list this might be useful too.

Never mind how I spelled where, please. The naming would require thought.

Ducking-down-behind-the-barricade-'ly yours,

Paul

P.S. I once learned a language where you could say x = y if c and it was yummy.