samedi 27 janvier 2018

Python: Evaluation of all/any vs spelling them out

Let's consider two ways to check for all conditions to be true

Option 1

if all([cond1, cond2, cond3]):
  return true

Option 2

if (cond1 and cond2 and cond3):
  return true

Do both evaluate the same way? AFAIR that all will fail as soon as one statement fails, making it possible, that a condition can presume that all previously listed conditions evaluated to true. E.g. making it safe in Option 1 to access a variable in cond3, that was previously (in cond1 or cond2) tested to be accessible. Does that hold true also for Option 2? Also, are there performance differences?

Aucun commentaire:

Enregistrer un commentaire