<blockquote>
<p><strong>Possible Duplicate:</strong><br/>
<a href="https://stackoverflow.com/questions/750298/easy-way-to-check-that-variable-is-defined-in-python">Easy way to check that variable is defined in python?</a><br/>
<a href="https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists-in-python">How do I check if a variable exists in Python?</a> </p>
</blockquote>
<p>如何知道在运行时是否在代码中的特定位置设置了变量?这并不总是显而易见的,因为(1)变量可以有条件地设置,(2)变量可以有条件地删除。我正在寻找Perl中的<code>defined()</code>,或者PHP中的<code>isset()</code>,或者Ruby中的<code>defined?</code>。</p>
<pre><code>if condition:
a = 42
# is "a" defined here?
if other_condition:
del a
# is "a" defined here?
</code></pre>