擅长:python、mysql、java
<p>如果您的<code>str()</code>或<code>list()</code>对象最终可能是空的:<code>astr = ''</code>或<code>alist = []</code>,那么您可能希望使用<code>alist[-1:]</code>而不是<code>alist[-1]</code>作为对象“相同性”。</p>
<p>其意义在于:</p>
<pre><code>alist = []
alist[-1] # will generate an IndexError exception whereas
alist[-1:] # will return an empty list
astr = ''
astr[-1] # will generate an IndexError exception whereas
astr[-1:] # will return an empty str
</code></pre>
<p>区别在于,返回空列表对象或空str对象更像是“最后一个元素”,然后是异常对象。</p>