1

In Python I want to check the following:

if x is None and y is None and z is None and ...

I'd rather say something like:

if x, y, z is None:

But that's not valid syntax. Is there a better way to do this?

0

2 Answers 2

4

You want to make sure all expressions satisfy a condition. The Python builtin all is made precisely for that. Since we have no need to introduce a new variable name, here I'll use the name _, which is a valid Python name and is the convention for a "throw-away" variable.

all(_ is None for _ in (x,y,z))
Sign up to request clarification or add additional context in comments.

Comments

0

If you only care about falsy-ness (which is often the case):

if not all((x, y, z)): ...

1 Comment

did you mean "if not any((x,y,z)):"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.