1

I want to write a statement like this javascript in Python:

if (variable1 && variable2) { // do something only if both variables exist }

I'm trying:

if not (variable1 and variable2) is None: # do something only if both variables exist

but it isn't working...when I debug, variable 2 is not defined but the function still tries to run. What's going wrong?

YPCrumble
  • 26,610
  • 23
  • 107
  • 172

3 Answers3

9
try:
    variable1
    variable2
except NameError:
    # Not there
else:
    # They exist

This is a pretty rare thing to want to do. Make sure it's actually a good idea before you do it.

Note that a variable being set to None is different from it not existing. If you want to check whether the variables are None, you're just messing up the boolean logic syntax:

if variable1 is not None and variable2 is not None:
    do_whatever()

This can be simplified if the not-None values are guaranteed to be considered true in a boolean context. For example, if variable1 and variable2 are the results of re.search calls, they'll either be match objects or None, and you can use:

if variable1 and variable2:
    do_whatever()
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • +1. However this may have some side effects when variable1 shapes like `foo.bar` and there's a getter property for member `bar` in `class foo` – starrify Feb 24 '14 at 07:03
  • @starrify: It won't even work in that case, considering `AttributeError`s aren't `NameError`s. – user2357112 Feb 24 '14 at 07:04
  • I didn't mean that. Could you please have a look at this code example? https://gist.github.com/starrify/9183346 – starrify Feb 24 '14 at 07:25
  • @starrify: I see what you mean, but now try that with an `f` that doesn't even have a `bar` attribute. `f=None`, for example. – user2357112 Feb 24 '14 at 07:44
  • Oh I see your point. Thank you. :) I was wring my own answer when I saw your answer came up, and what's unlucky is that I'm using exactly the same method as yours. However now I think it's better to manually seek the identifier in `locals()` XD – starrify Feb 24 '14 at 07:50
  • @starrify: `locals` runs into weird edge cases with closure variables, though. – user2357112 Feb 24 '14 at 08:15
4

You can do the samething in Python.

if variable1 and variable2:
    ...

But this means that, both the variables have Truthy values in them.

Note: Using a variable which hasn't been assigned a value, is an error in Python.

If you really want to check if the variables are defined already, you can use this hack but I won't recommend this

if (variable1 in locals() and variable2 in locals()):
   ...

If there are more variables you want to check,

if all(var in locals() for var in (variable1, variable2, variable3)):
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
3

You connect the two variables by a boolean operator. What you actually want is to and the outcome of the two comparisons.

if variable1=!None and variable2=!None:
    # do something

Apart from that a non-existing variable is not None but not existing. If you want to check whether a variable exists, check if it is defined in the globals() or locals():

if "variable1" in locals() and "variable2" in locals():
    # do something

Note that the variables are quoted here, since you do not want to evaluate them!

Denis
  • 136
  • 6