擅长:python、mysql、java
<blockquote>
<p>so why, if the length of the line > 0 and line[0] == '#' : does it not error if I just hit enter with nothing there.</p>
</blockquote>
<p>不,它不会抛出错误。{Python使用<cd1>和short}运算符来启用lazy}。参见<a href="https://docs.python.org/3/library/stdtypes.html" rel="nofollow noreferrer">docs</a>。在</p>
<p>换句话说,布尔表达式的第二部分,即<code>line[0] == '#'</code>,只在第一部分是<code>True</code>的情况下计算。在</p>
<p>我相信这就是为什么作者称之为监护人模式的原因,这样表达式的第一部分避免了第二部分中可能出现的错误。在</p>
<p>如果您删除第一部分,如您所示的示例中所示,那么您将尝试访问<code>line</code>(<code>line[0]</code>)的第一个元素,<code>line</code>中没有元素。在</p>