Best way to test for existence of a variable in Python
<p>I am reading a million plus files to scrape out some data.  The files are generally pretty uniform but there are occasional problems where something that I expected to find is not present.</p>

<p>For example, I expect some sgml code to identify a value I need</p>

<pre><code>for data_line in temp  #temp is a list of lines from a file
    if &lt;VARIABLENAME&gt; in data_line:
        VARIABLE_VAL=data_line.split('&gt;')[-1]
</code></pre>

<p>Later on I use VARIABLE_VAL in a dictionary that I am using to preserve the data I have extracted.  </p>

<pre><code>myDict['identifier']=[VARIABLE_VAL]
</code></pre>

<p>So I am humming along and I get to an exception- no line in the file that has</p>

<pre><code>&lt;VARIABLENAME&gt;theName
</code></pre>

<p>So to handle this I have added this line after all the lines have been processed</p>

<pre><code>try:
    if VARIABLE_VAL:
        pass
except NameError:
    VARIABLE_VAL=somethingELSE
</code></pre>

<p>The reason I posted this is I was sure that I have seen somewhere a solution that looks like</p>

<pre><code>if not VARIABLE_VAL:
    VARIABLE_VAL=somethingELSE
</code></pre>

<p>But I could not get that to work after beating my head against the monitor for thirty minutes.</p>

<p>Any help would be appreciated</p>
