0

What is the differences between using if object is None versus if not object?

So while I was doing some coding where I messed up the signals in the QListWidget, where in a selection instance, it is returning me 2 signals, where it returns 2 objects - QListWidgetItem and None.

To address to the None return, I used if object is None, but I was told that if not object would be better. Even so, it seems that using either of the 2 if would still allow me to achieve the same result.

Is there a better case scenario as to when to use either?

Teh Ki
  • 455
  • 1
  • 3
  • 14

1 Answers1

2

not object is true when object has any falsey value, such as 0 or False.

If you know a priori that the variable contains either a QListWidgetItem or None, then None is the only falsey possibility, so if not object: is a simpler way to write the condition.

You just have to be careful about this in other context. Consider a variable that can contain a number or None. I've seen many errors due to not considering the possibility that the number might be 0.

Barmar
  • 741,623
  • 53
  • 500
  • 612