Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
incompatible_range_type: Make range() return a non-list iterable #5264
range() currently returns a list of integers. This takes memory proportional to the size of the range, even though it can easily be represented using just its bounds and step. It's especially bad when used for iteration in a loop that breaks early. We should make range() return a list-like object as in Python 3.
If we take the Python 3 approach, range() would return a completely separate type of sequence from lists and tuples, with observably different behavior on the operations str(), repr(), type(), ==, ordering, +, and slicing.
I've tested the implementation. It seems to behave the same as Python3 and https://github.com/google/skylark/.
r = range(7) print(r) # range(7) print(r[2:]) # range(2, 7) print(r[1:9:2]) # range(1, 7, 2) print(r[1:5:2]) # range(1, 5, 2) print(repr(r[1:5:2])) # range(1, 5, 2) print(type(r)) # range # print(r + ) # type error (like Python3, unlike Python2) # print(r * 2) # type error (like Python3, unlike Python2)
Bug: We should throw an error when comparing range objects:
r < r
print(range(0, 5, 10) == range(0, 5, 11))
This returns False with the Go implementation; True with both Python3 and Bazel (I suppose it iterates over both ranges?).
Anything else we should test or consider?