Tigran Saluev
Tigran Saluev

Reputation: 3582

Python 2: why xrange isn't called irange?

This question is kind of philosophical. There is splendid itertools module in Python 2, providing, in particular, generator equivalents for Python built-in functions like map, filter, zip, or slice. And the equivalents are called imap, ifilter, izip, and islice, respectively. As I understand, the prefix i in their names means i​terator. But there's the same thing about xrange: it is an equivalent of range returning generator object instead of large list. So, why isn't it called irange? What does prefix x actually mean?

Upvotes: 6

Views: 712

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124548

Because xrange() was added to the language before it had iterators and it is itself not an iterator.

Python 1.0 was released in 1994, so xrange() was added very early on. The x most likely doesn't have a specific meaning here.

But xrange() is not really an iterator, it is an iterable; you can iterate over it multiple times, unlike iterators. It is also a sequence, as it has a length and can be indexed.

As such the object has been renamed to range() (replacing the Python 2 range() function altogether), and its sequence behaviour has been further expanded.

Upvotes: 5

Related Questions