Comment

bc

If think it's important to stress that equality testing, truth testing and identity testing are all distinct operations each with different characteristics.

Thus:

if variable: #this is truth testing

if variable == 0: #this is equality testing

if variable is None: #this is identity testing

It's important to know what objects you're expecting to handle in your conditional statement, because different objects may behave in unusual ways. In fact, of these three operations, only identity testing is guaranteed safe. For example, try "if var ==0: ..." where var is a numpy array and it'll fail with an exception.

Replies

Peter Bengtsson

Sure, use equality or identity testing when it's applicable.
But how often is that? ...compared to how often it would be more appropriate and concise to use the shorthand?