What Is the Most Pythonic Way to Check If Multiple Variables Are Not None

What is the most pythonic way to check if multiple variables are not None?

There's nothing wrong with the way you're doing it.

If you have a lot of variables, you could put them in a list and use all:

if all(v is not None for v in [A, B, C, D, E]):

Check if value is not none before comparison

Perhaps:

if not None in (x, y) and x > y:
do_something()

Elegant way to check if any of long list of variables is not None, and return the one that isn't?

Perform the check when you're retrieving the environment variables in the first place:

def needenv(name):
val = os.environ.get(name)
if val is None:
sys.exit('{} is required.'.format(name))
return val

a = needenv('a')
b = needenv('b')
...

Check if multiple variables are empty and replace only those who are empty

You could simply use an or during initial variable assignment.

DU = sheet['B18'].value or "none"
bl_n = sheet['G17'].value or "none" #on excel is None
incoterm = sheet['B23'].value or "none" #on excel is None
transporter = sheet['G23'].value or "none"

To further clarify, or is a boolean operator. It converts the expression before and after to a boolean. If the first expression evaluates to True i.e. it is not None, it returns the first value, otherwise the second value is returned, which in this case is always True.

Edit: A potential pitfall for some cases, as I've just realized. Since the or checks for the truth value, all of 0, False and None will result in the second expression being returned.

How to check in python if variable is not None and greater than something in one line?

You can also use python ternary operator. In your example, this might help you. You can extend the same further too.

#if X is None, do nothing
>>> x = ''
>>> x if x and x>0 else None
#if x is not None, print it
>>> x = 1
>>> x if x and x>0 else None
1

Dealing with string values

>>> x = 'hello'
>>> x if x and len(x)>0 else None
'hello'
>>> x = ''
>>> x if x and len(x)>0 else None
>>>

Most concise way to check if multiple variables are between the same limits

Try using all().

e.g.

if all(A < n < B for n in (x, y, z)):
...

Check if one of all variables is empty

You have very well isolated the problem:

if name or loca == None:

Here you think it says:

"If either name or loca equal none:"

But instead it says:

"if (name) is true or (loca equals None) is true:"

Where it should say:

"if (name equals none) is true or (loca equals none) is true:"

Which is:

if name is None or loca is None:

See, by the way, that None comparisons are better carried out with is, since there is only one None object.

Also, a more pythonic way of doing it is:

if not all((name, loca)):

Or, seeing as "all" it's just 2 variables:

if not (name and loca):

If you don't understand, read around a bit (the Python tutorial is excellent -- go for the conditionals part). Good luck!

not None test in Python

if val is not None:
# ...

is the Pythonic idiom for testing that a variable is not set to None. This idiom has particular uses in the case of declaring keyword functions with default parameters. is tests identity in Python. Because there is one and only one instance of None present in a running Python script/program, is is the optimal test for this. As Johnsyweb points out, this is discussed in PEP 8 under "Programming Recommendations".

As for why this is preferred to

if not (val is None):
# ...

this is simply part of the Zen of Python: "Readability counts." Good Python is often close to good pseudocode.



Related Topics



Leave a reply



Submit