Max Frai
Max Frai

Reputation: 64356

How do I check if a variable exists?

I want to check if a variable exists. Now I'm doing something like this:

try:
    myVar
except NameError:
    # Do something.

Are there other ways without exceptions?

Upvotes: 1411

Views: 1873787

Answers (16)

Abhishek R
Abhishek R

Reputation: 4773

I created a custom function.

def exists(var):
     return var in globals()

Then the call the function like follows replacing variable_name with the variable you want to check:

exists("variable_name")

Will return True or False

To check if a variable exists in the local scope in Python, you can use the locals() function, which returns a dictionary containing all local variables. You can modify your exists function to check both global and local scopes like this:

def exists(var):
    return var in globals() or var in locals()

Here's how you can use it to check if a variable exists in the local scope:

def my_function():
    local_var = 42
    print(exists('local_var'))

my_function()  # Output: True
print(exists('local_var'))  # Output: False

In the above example, exists('local_var') returns True within the my_function because local_var is defined locally inside the function. However, it returns False outside the function because local_var is not accessible in the global scope.

Upvotes: 8

rsswtmr
rsswtmr

Reputation: 1816

Huh. I just simply wrap the variable like this:

globals().get('myVar', <non-existant-value>)

or ...

locals().get('myVar', <non-existant-value>)

depending on scope, where <non-existant-value> is something you know will be out of range, like None or False. This works for 99% of the cases I've encountered. Always avoid the temptation to overengineer. 😉

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882326

The use of variables that have yet to been defined or set (implicitly or explicitly) is often a bad thing, since it tends to indicate that the logic of the program hasn't necessarily been thought through completely, and is likely to result in unpredictable behaviour.

If you do need to do it in Python, the following trick, which is similar to yours, will ensure that a variable has some value before use:

try:
    myVar
except NameError:
    myVar = None      # or some other default value.

# Now you're free to use myVar without Python complaining.

The value you choose to give it will, of course, depend on what you want to do in such a case. For example, it it was a starting value for some accumulator, you would set it to zero.


However (and this is my opinion), it would probably be better to refactor your code so that this situation does not occur.

By way of an example, the following code was given a comment to this answer, to allow line drawing from a previous point to the current point:

if last:
    draw(last, current);
last = current

In the case where last has not been bound to a value, that won't help in Python at all since even the checking of last will raise an exception. A better idea would be to ensure last does have a value, one that can be used to decide whether or not it is valid. That would be something like:

last = None

# some time passes ...

if last is not None:
    draw(last, current);
last = current

That ensures the variable exists and that you only use it if it's valid for what you need it for.

You can still add the code to force this (if you have no control over the initial setting of the variable) by using the exception method given above:

# Variable 'last' may or may not be bound to a value at this point.

try:
    last
except NameError:
    last = None

# It will always now be bound to a value at this point.

if last is not None:
    draw(last, current);
last = current

Upvotes: 193

Mihai.Mehe
Mihai.Mehe

Reputation: 504

Simple one line:

a=1
exec("try: a \nexcept NameError: print('Does not exist.')\nelse:print(a)")

and do something:

exec("def my_try(): \n\ttry: a \n\texcept NameError: print('Does not exist.Creating a=');a=1;print(a);\n\telse:print(a)\n\n\nmy_try()")

Upvotes: 0

James
James

Reputation: 2764

It may not be performant, but you generalise the solution to a function that checks both local variables and global variables.

import inspect
def exists_var(var_name):
    frame = inspect.currentframe()
    try:
        return var_name in frame.f_back.f_locals or var_name in globals()
    finally:
        del frame

Then you can use it like this:

exists_var('myVar')

Upvotes: 2

Gulzar
Gulzar

Reputation: 28044

Also a possibility for objects, use __dict__.

class A(object):
    def __init__(self):
        self.m = 1

a = A()
assert "m" in a.__dict__
assert "k" not in a.__dict__

Upvotes: 0

RGD2
RGD2

Reputation: 469

Like so:

def no(var):
    "give var as a string (quote it like 'var')"
    assert(var not in vars())
    assert(var not in globals())
    assert(var not in vars(__builtins__))
    import keyword
    assert(var not in keyword.kwlist)

Then later:

no('foo')
foo = ....

If your new variable foo is not safe to use, you'll get an AssertionError exception which will point to the line that failed, and then you will know better. Here is the obvious contrived self-reference:

no('no')

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-88-d14ecc6b025a> in <module>
----> 1 no('no')

<ipython-input-86-888a9df72be0> in no(var)
      2     "give var as a string (quote it)"
      3     assert( var not in vars())
----> 4     assert( var not in globals())
      5     assert( var not in vars(__builtins__))
      6     import keyword

AssertionError: 

Upvotes: 3

Jens
Jens

Reputation: 9140

This was my scenario:

for i in generate_numbers():
    do_something(i)
# Use the last i.

I can’t easily determine the length of the iterable, and that means that i may or may not exist depending on whether the iterable produces an empty sequence.

If I want to use the last i of the iterable (an i that doesn’t exist for an empty sequence) I can do one of two things:

i = None  # Declare the variable.
for i in generate_numbers():
    do_something(i)
use_last(i)

or

for i in generate_numbers():
    do_something(i)
try:
    use_last(i)
except UnboundLocalError:
    pass  # i didn’t exist because sequence was empty.

The first solution may be problematic because I can’t tell (depending on the sequence values) whether i was the last element. The second solution is more accurate in that respect.

Upvotes: 0

Short variant:

my_var = some_value if 'my_var' not in globals() else my_var:

Upvotes: 1

Chinedum Ukejianya
Chinedum Ukejianya

Reputation: 1246

A simple way is to initialize it at first saying myVar = None

Then later on:

if myVar is not None:
    # Do something

Upvotes: 96

samwyse
samwyse

Reputation: 2996

I will assume that the test is going to be used in a function, similar to user97370's answer. I don't like that answer because it pollutes the global namespace. One way to fix it is to use a class instead:

class InitMyVariable(object):
  my_variable = None

def __call__(self):
  if self.my_variable is None:
   self.my_variable = ...

I don't like this, because it complicates the code and opens up questions such as, should this confirm to the Singleton programming pattern? Fortunately, Python has allowed functions to have attributes for a while, which gives us this simple solution:

def InitMyVariable():
  if InitMyVariable.my_variable is None:
    InitMyVariable.my_variable = ...
InitMyVariable.my_variable = None

Upvotes: 12

Wyrmwood
Wyrmwood

Reputation: 3609

for objects/modules, you can also

'var' in dir(obj)

For example,

>>> class Something(object):
...     pass
...
>>> c = Something()
>>> c.a = 1
>>> 'a' in dir(c)
True
>>> 'b' in dir(c)
False

Upvotes: 28

Roger Dahl
Roger Dahl

Reputation: 15734

A way that often works well for handling this kind of situation is to not explicitly check if the variable exists but just go ahead and wrap the first usage of the possibly non-existing variable in a try/except NameError:

# Search for entry.
for x in y:
  if x == 3:
    found = x

# Work with found entry.
try:
  print('Found: {0}'.format(found))
except NameError:
  print('Not found')
else:
  # Handle rest of Found case here
  ...

Upvotes: 6

SilentGhost
SilentGhost

Reputation: 319889

catch is called except in Python. other than that it's fine for such simple cases. There's the AttributeError that can be used to check if an object has an attribute.

Upvotes: 9

user97370
user97370

Reputation:

Using try/except is the best way to test for a variable's existence. But there's almost certainly a better way of doing whatever it is you're doing than setting/testing global variables.

For example, if you want to initialize a module-level variable the first time you call some function, you're better off with code something like this:

my_variable = None

def InitMyVariable():
  global my_variable
  if my_variable is None:
    my_variable = ...

Upvotes: 27

Ayman Hourieh
Ayman Hourieh

Reputation: 137346

To check the existence of a local variable:

if 'myVar' in locals():
  # myVar exists.

To check the existence of a global variable:

if 'myVar' in globals():
  # myVar exists.

To check if an object has an attribute:

if hasattr(obj, 'attr_name'):
  # obj.attr_name exists.

Upvotes: 2346

Related Questions