<p><a href="https://stackoverflow.com/a/5893946/918959">ncoghlan's answer</a>列出了Python中<code>_</code>的3种常规用法:</p>
<blockquote>
<ol>
<li>To hold the result of the last executed statement in an interactive
interpreter session. This precedent was set by the standard CPython
interpreter, and other interpreters have followed suit</li>
<li><p>For translation lookup in i18n (imported from the corresponding C
conventions, I believe), as in code like: </p>
<pre><code>raise forms.ValidationError(_("Please enter a correct username"))`
</code></pre></li>
<li><p>As a general purpose "throwaway" variable name to indicate that part
of a function result is being deliberately ignored, as in code like:</p>
<pre><code> label, has_label, _ = text.partition(':')
</code></pre></li>
</ol>
</blockquote>
<hr/>
<p>您的问题是在代码的示例中使用了其中的哪一个。答案是这是一个一次性变量(情况3),但其内容打印在这里是为了调试目的。</p>
<p>但是,如果以任何方式使用<code>_</code>作为循环变量,则它不是一般的Python约定。因此,您可能会经常看到:</p>
<pre><code> for _ in range(10):
print("Hello world")
</code></pre>
<p>其中<code>_</code>立即通知读取器该值不重要,循环只重复10次。</p>
<p>但是在诸如</p>
<pre><code> for i in range(10):
do_something(i)
</code></pre>
<p>在使用循环变量值的地方,通常使用变量名,例如<code>i</code>,<code>j</code>,而不是<code>_</code>。</p>