Pythons 'if' conditions

Dawid Laszuk published on
1 min, 137 words

The 'if' condition, being frequently used as it is, is not always well understood. Most of the people don't care about the order of referenced conditions, which is fine. However, wanting to get most of it, one should consider putting the less compute demanding conditions first. Here is an example in Python:

[sourcecode language="python"] >>> def one(): ... print '1' ... return True ... >>> def two(): ... print '2' ... return False ... >>> one() and two() 1 2 False >>> two() and one() 2 False [/sourcecode]

As you can see, having two conditions linked with 'and', they are checked one at the time starting from left most. It is not magic. This is exactly what one would expect. If 'if' statement is able to make a decision during the evaluation, it will!