Python checks if NoneType is working

I am trying to check if an object is of type None before checking its length. To do this, I made an if statement with the or operator:

if (cts is None) | (len(cts) == 0): return 

As far as I can tell, the cts object will be checked if it is None, and if so, the length check will not be performed. However, the following error occurs if cts is None:

TypeError: object of type 'NoneType' has no len()

Does python check both expressions in an if statement, even if the first is true?

+7
python if-statement nonetype
source share
2 answers

In python | is bitwise or . Do you want to use boolean or here:

 if (cts is None) or (len(cts) == 0): return 
+24
source share

You can also use -

 if not cts: return 
+16
source share

All Articles