<blockquote>
<ul>
<li>Considering how often I and I'm sure many others have typed range(somelist) when they meant range(len(somelist)) the meaning should be obvious</li>
</ul>
</blockquote>
<p>实际上,这种情况只会“经常”发生在新的Python用户身上。但迟早你应该学会不那样做。使用索引循环很容易因一个错误而关闭。正确的python方法是<code>for item in somelist</code>,如@MSeifert的答案所示。在</p>
<blockquote>
<ul>
<li>It would remove redundancy and one level of parenthesis nesting and save typing and line width in a location where linebreaks are especially harmful to readability</li>
</ul>
</blockquote>
<p>我个人认为这不是一个有力的论据。在传统的C/C++符号中,我们总是做{{CD2}}。这是括号嵌套的一个级别,人们对此很满意。所以Python的<code>len(somelist)</code>同样正常。{cd4>显然需要一个整数来表示。考虑到这个设计契约,用整数以外的任何东西调用range(…)很可能是一个bug。因此,静默地将输入转换为一个整数会使这个错误在早期阶段更难被注意到。(注:我也被其他语言的类似演员给咬了一口,但这不是话题。)</p>
<blockquote>
<ul>
<li>I couldn't see it breaking any code</li>
<li>I fail to see any other ways it could be interpreted in</li>
</ul>
</blockquote>
<p>由于标准范围(…)已经设计为不接受列表,所以是的,您可以始终重写它以添加范围(somelist)行为,而不破坏现有代码。这是你的选择。在</p>
<blockquote>
<p>So my question is (please refrain from merely venting opinions)</p>
<ul>
<li>Are there any good (objective, Python Zen is acceptable) arguments against which I may have missed?</li>
</ul>
</blockquote>
<p>另外,您知道<code>range(...)</code>也接受多个输入参数吗?<code>range(stop)</code>,<code>range(start, stop)</code>,<code>range(start, stop, step)</code>。我不喜欢<code>range(1, 10, 2)</code>和{<cd10>}都是有效的但却意味着不同的观点。在</p>
<blockquote>
<ul>
<li>Is there perhaps already a convenience function for range(len(x))?</li>
</ul>
</blockquote>
<p>可能没有。但话说回来,你不应该经常需要这个。我发现自己只有在写一些关于算法练习的“学术代码”时才会这样做。在</p>