Here’s my situation:
if var:
if len(var) == 5:
do something...
else:
do the same thing...
To avoid repeating the same piece of code, I would like to combine those 2 if conditions, in one. But if var is None, I can’t check its length… Any idea?
I would like something like this:
if var and len(var) == 5:
do something...
Did you try that? It works:
The
andoperator doesn’t evaluate the RHS if the LHS is false. Try this: