-1

the expression if (mask1 | mask2) is None: returns this error

TypeError: unsupported operand type(s) for |: 'int' and 'NoneType' 

How can I check if one of the two variables is None?

2
  • 3
    should this be true if any of them is None, or only if one is None but the other is not None Commented Dec 19, 2019 at 16:18
  • 1
    Does this answer your question? How to test multiple variables against a value? It is not an exact duplicate as they are testing for values and OP for None, but still the accepted answer applies here also Commented Dec 19, 2019 at 16:28

4 Answers 4

7
if mask1 is None or mask2 is None 

Should work

Sign up to request clarification or add additional context in comments.

Comments

2

Not as Pythonic but this would also work

a = 1 b = 2 None in (a, b) >> False b = None None in (a, b) >> True 

4 Comments

IMO, this might be more pythonic. See this answer
Nice, but in also uses ==, not is, so might fail in the (rather esoteric) case of an object pretending to be __eq__ to None
correct @tobias_k hence why I mention it might not be as Pythonic although that depends on OP's use case, as is being debated in another answer
This will work. The problem is it is anti-pattern to use equality checks, where identity checks are more appropriate. The place where this will bite you is if you try it with True / False... ie True in (a, b) will return True. Equivalently False in (0, 1) will return True.
1

Update: OP's exception message indicates he is checking integer vs None. Equality checks will NOT suffice. Consider mask1 = 0, mask2 = None both will fail falsey equality checks.

TypeError: unsupported operand type(s) for |: 'int' and 'NoneType'

If you are testing for identity for an arbitrary number of elements:

any(map(lambda x: x is None, (mask1, mask2, ..., maskN)) # OR operation. all(map(lambda x: x is None, (mask1, mask2, ..., maskN)) # AND operation. 

As user @Jean-François Fabre mentioned get rid of the map / lambda operation:

any(x is None for x in (mask1, mask2, ..., maskN)) # OR 

These will all short-circuit because in Python 3 list comprehension will return an iterator that any can evaluate at each step. Same with map / lambda those operations return iterators as well. So which you prefer is a stylistic choice.

If you only have two conditionals, then the other answers (that use identity checks, not falsey checks) will suffice.

15 Comments

@KonradRudolph Perhaps a different answer using any but is correct :P using any, map and a lambda seems like a huge overkill for such a simple task. Why not any(val is None for val in list_of_masks_or_explicit_tuple) ? We don't know exactly when OP wants this to be True but it's irrelevant since it can be easily changed
"Neither of the above will short-circuit though. So you could always optimize using an early exit": not true. It will stop as soon as condition is known
but better written as any(x is None for x in …)
@Jean-FrançoisFabre Depends. In Python 2, the map is evaluated entirely before checking the any (which will short-circuit). But unless noted otherwise we should certainly assume Python 3.
@DeepSpace The generator comprehension code you posted is 100% identical to the map code in Python 3, except syntactically. Which to prefer is purely a matter of preference. It’s definitely not conceptually simpler.
|
0

If you need to check for AND (i.e. all the checks are None):

if all(x is None for x in (mask1, mask2)): # execute codes 

If you need to check for OR (i.e. any of the checks are None):

if any(x is None for x in (mask1, mask2)): # execute codes 

You might also consider using set to check:

if {mask1, mask2} == {None}: # execute codes for when all are None if {mask1, mask2} > {None}: # execute codes for when any is None if {mask1, mask2} - {None}: # execute codes for when any is not None. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.