In Ruby I can do something like:
irb(main):001:0> x = []
=> []
irb(main):003:0> puts "hi ho" if x.count > 0 and x[0]
=> nil
Ruby first evaluates x.count >0 as false and does not bother to evaluate x[0].
In Python I can have for example a file with:
x = []
if x.count > 0 and x[0]:
print "hi ho"
When I run:
[onknows:/tmp] $ python test-if.py
Traceback (most recent call last):
File "test-if.py", line 2, in <module>
if x.count > 0 and x[0]:
IndexError: list index out of range
[onknows:/tmp] 1
So Python evaluates x[0] as well although there is no reason to evaluate it. Is there a way to use only one if and not resort to nested if in Python? I don't want to do something like:
if x.count > 0:
if x[0]:
print "hi ho"