1

Is it safe to rely on condition evaluation order in if statements?

The above question asks exactly what I would like to know but is the same true of Pandas & and | operator when performing conditional selection.

Thankyou.

Edit: So this behaviour does not occur with pandas but is there any way one can achieve it?

Ivor Denham-Dyson
  • 655
  • 1
  • 5
  • 24
  • 1
    No: `&` and `|` do *not* short-circuit, since you simply call a function behind the curtains with the two operands evaluated. Of course Numpy/pandas/... can implement this function in a "lazy-functional" way, but if there is no logic around this, it thus does not shortcircuit. Note that `&` and `|` do *not* per se work with booleans: one can return any kind of value. – Willem Van Onsem Oct 08 '18 at 11:07
  • @WillemVanOnsem: That is exactly what I wanted to know, thanks. You mentioned a lazy-functional workaround. Could you elaborate? – Ivor Denham-Dyson Oct 08 '18 at 11:11
  • (particularly [this answer](https://stackoverflow.com/a/22647040/2285236)) – ayhan Oct 08 '18 at 11:12
  • @JakeDyson: see it like this: you can make a function `sum(..)` that for a given collection calculates the sum, but you can also return an *object* that acts as a *postponed* operation, so `sum(my_tuple)` does *not* calculate the sum, it simply returns an object that in case you *need* to know the sum, it will calculate it. – Willem Van Onsem Oct 08 '18 at 11:13
  • See for example here: https://en.wikipedia.org/wiki/Lazy_evaluation – Willem Van Onsem Oct 08 '18 at 11:13
  • Thankyou for your help, apologies for the duplicate @ayhan. – Ivor Denham-Dyson Oct 08 '18 at 11:17

0 Answers0