If I have the following:
if a(my_var) and b(my_var):
do something
Can I assume that b()
is only evaluated if a()
is True
? Or might it do b()
first?
Asking because evaluating b()
will cause an exception when a()
is False
.
b()
will only be evaluated if a(my_var)
is True
, yes. The and
operator short-circuits if a(my_var)
is falsey.
From the boolean operators documentation:
The expression
x and y
first evaluatesx
; ifx
is false, its value is returned; otherwise,y
is evaluated and the resulting value is returned.
You can test this yourself with a function that prints something when called:
>>> def noisy(retval):
... print "Called, returning {!r}".format(retval)
... return retval
...
>>> noisy(True) and noisy('whatever')
Called, returning True
Called, returning 'whatever'
'whatever'
>>> noisy(False) and noisy('whatever')
Called, returning False
False
Python consideres empty containers and numeric 0 values as false:
>>> noisy(0) and noisy('whatever')
Called, returning 0
0
>>> noisy('') and noisy('whatever')
Called, returning ''
''
>>> noisy({}) and noisy('whatever')
Called, returning {}
{}
Custom classes can implement a __nonzero__
hook to return a boolean flag for the same test, or implement a __len__
hook if they are a container type instead; returning 0
means the container is empty and is to be considered false.
On a closely related note, the or
operator does the same thing, but in reverse. If the first expression evaluates to true the second expression will not be evaluated:
>>> noisy('Non-empty string is true') or noisy('whatever')
Called, returning 'Non-empty string is true'
'Non-empty string is true'
>>> noisy('') or noisy('But an empty string is false')
Called, returning ''
Called, returning 'But an empty string is false'
'But an empty string is false'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With