Many decades ago I spent years developing software in a couple of languages that make you very aware of the order of operations. One was Forth and the other APL (yes, they couldn't be farther apart).
Anyhow, things are not very different with other languages. It's just that most people simply aren't aware of order of operations. Once you are, you look for opportunities to make performance and functional gains just by moving things around.
Try this:
Code: Select all
# The order of evaluation matters
import time
# Returns True after waiting 5 seconds
def slow_return_true():
time.sleep(5)
return True
def faster_return_true():
time.sleep_ms(500)
return True
# Returns False without delay
def fast_return_false():
return False
# ------------------------------------------------------------
print("\nif slow_return_true() and faster_return_true():")
if slow_return_true() and faster_return_true():
print("This will happen.")
print("done")
# ------------------------------------------------------------
print("\nif faster_return_true() and slow_return_true():")
if faster_return_true() and slow_return_true():
print("This will happen.")
print("done")
# ------------------------------------------------------------
print("\nif slow_return_true() and fast_return_false():")
if slow_return_true() and fast_return_false():
print("Not going to happen")
print("done")
# ------------------------------------------------------------
print("\nif fast_return_false() and slow_return_true():")
if fast_return_false() and slow_return_true():
print("Not going to happen")
print("done")
How do you explain what happens with the second case vs. the last?
If you are looking for performance, you can't leave any stones unturned. Always measure, don't make assumptions.