How do I check if a variable exists?

Recommended Answers

All 5 Replies

If I understand you correctly, you probably want something like

if x is None:
    print "x has no value"

for your code, you would have to hade done x = None what i want is to check if a var has been defined

First of all python has no variables. Here's how to check if a name is defined in local namespace:

"name" in locals()

and global namespace:

"name" in globals()

Oh ok cool. I understand that everything in python is an object.

Variables in Python are officially names/identifiers (keys in a dictionary) of object references. I still like to refer to them with the old fashioned name variables, this way other folks know what we are talking about.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.