If this is a dictionary, you can have
mydict['ggg'] = '' // doesn't matter if it is empty value or not.
if mydict.has_key('ggg'):
print "OH GEESH"
However, has_key() is completely removed from Python 3.x, therefore, the Python way is to use in
'ggg' in mydict # this is it!
# True if it exists
# False if it doesn't
You can use in for tuple, list, and set as well.
Of course, if the variable hasn't been defined, you will have to raise an exception silently (just raise any exception... let it pass), if exception is not what you want to see (which is useful for many applications, you just need to log the exception.)
It is always safe to define a variable before you use it (you will run into "assignment before local reference" which means " var is not in the scope " in plain English). If you do something with query, the chance is, you will want to have a dictionary, and checking whether a key exists or not, use in .
vardoesn't actually exist, then you are going to get an exception raised when you try to use it. That is outside of whatif/elsecan handle.if varassumes thatvarexists, and tests if it is "true-ish" (becomes True rather than False if converted to boolean).Trueas a boolean context.