Analysis of the Absence of xrange in Python 3 and the Evolution of the Range Object

Nov 19, 2025 · Programming · 15 views · 7.8

Keywords: Python | xrange | range | performance optimization | iterator

Abstract: This article delves into the reasons behind the removal of the xrange function in Python 3 and its technical background. By comparing the performance differences between range and xrange in Python 2 and 3, and referencing official source code and PEP documents, it provides a detailed analysis of the optimizations and functional extensions of the range object in Python 3. The article also discusses how to properly handle iterative operations in practical programming and offers code examples compatible with both Python 2 and 3.

Background of xrange Removal in Python 3

In Python 2, range and xrange were two commonly used functions for generating integer sequences. range returned a list directly, while xrange returned a generator object that produced values on-demand, thus saving memory. However, in Python 3, xrange was removed, and its functionality was integrated into range. This change has caused confusion among developers, especially those migrating from Python 2 to Python 3.

Performance Comparison Analysis

The example code provided by the user demonstrates performance differences in iterative operations between Python 2 and Python 3. In Python 2, using xrange to generate 10,000,000 integers and filter those divisible by 4 took approximately 1.54 seconds. In Python 3, using a custom xrange function (implemented via iter(range(x))) increased the time to 3.22 seconds, suggesting lower iteration efficiency in Python 3.

However, more rigorous performance tests using the timeit module reveal that on 64-bit systems, iterating with Python 2.7's xrange took about 1.05 seconds, while Python 3.3's range iteration took about 1.32 seconds, a difference of approximately 30%. On 32-bit systems, the difference was more pronounced, with Python 3 taking nearly twice as long as Python 2. This is primarily due to optimizations in Python 3 for 64-bit systems, which may not perform as well on 32-bit architectures.

Further analysis shows that list construction operations (e.g., [x for x in range(10000000) if x%4 == 0]) in Python 3 took about 3.65 seconds, significantly longer than the iteration itself. This indicates that in practical applications, differences in iteration performance may be overshadowed by other operations, such as list building.

xrange Not Removed but Renamed

The key point is that xrange was not removed in Python 3 but was renamed to range. The Python 2 range function (which returned a list) was removed in Python 3, replaced by the behavior of Python 2's xrange (returning an iterable object). Official source code (e.g., rangeobject.c) confirms this, showing that Python 3's range object directly inherits from Python 2's xrange.

For example, in Python 3, using range directly provides lazy evaluation without additional wrapping:

# Python 3 code
for i in range(10000000):
    if i % 4 == 0:
        print(i)

If a list is needed in Python 3, list(range(...)) can be used, mirroring the behavior of Python 2's range function.

Optimizations and New Features in Python 3 Range

Python 3's range object not only inherits the lazy evaluation特性 of xrange but also adds new features such as support for slicing, negative indexing, and richer comparison operations. These improvements enhance code flexibility and readability but may introduce slight performance overhead. For example:

# Python 3 range supports slicing
r = range(10)
print(r[2:5])  # Outputs range(2, 5)
print(list(r[2:5]))  # Outputs [2, 3, 4]

Additionally, Python 3 has undergone global optimizations in iteration mechanisms, focusing on improving performance in common use cases, which may slightly sacrifice performance in edge scenarios like pure iteration.

Code Practices for Python 2 and 3 Compatibility

For code that needs to be compatible across versions, conditional checks can simulate xrange:

try:
    xrange
except NameError:
    xrange = range

# Using xrange, effective in both Python 2 and 3
for i in xrange(10000000):
    if i % 4 == 0:
        print(i)

This approach ensures code consistency between Python 2 and 3, avoiding errors due to version differences.

Summary and Recommendations

The "absence" of xrange in Python 3 is part of the language's evolution, aimed at simplifying the API and improving consistency. Developers should use range directly for iterative operations and focus on optimizing overall code structure in performance-sensitive scenarios, rather than minor differences in iteration itself. Official documentation and PEPs (e.g., PEP 3100) provide further details and are recommended for in-depth understanding of design decisions.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.