擅长:python、mysql、java
<p>因为<code>xrange</code>不返回生成器。它返回一个<a href="http://docs.python.org/library/functions.html#xrange" rel="noreferrer">xrange object</a>。在</p>
<pre><code>>>> type(xrange(10))
<type 'xrange'>
</code></pre>
<p>除了重复迭代之外,<code>xrange</code>对象还支持生成器不喜欢索引的其他功能:</p>
^{pr2}$
<p>它们还有一个长度:</p>
^{3}$
<p>它们可以逆转:</p>
<pre><code>>>> list(reversed(xrange(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
</code></pre>
<p>简而言之,<code>xrange</code>对象实现了完整的<a href="http://docs.python.org/glossary.html#term-sequence" rel="noreferrer">sequence</a><a href="http://docs.python.org/library/collections.html#collections-abstract-base-classes" rel="noreferrer">interface</a>:</p>
<pre><code>>>> import collections
>>> isinstance(xrange(10), collections.Sequence)
True
</code></pre>
<p>他们只是在不消耗大量内存的情况下做这件事。在</p>
<p>还要注意,在Python3中,<code>range</code>返回的<code>range</code>对象具有所有相同的属性。在</p>