Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2: why xrange isn't called irange?

Tags:

python

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?

like image 950
Tigran Saluev Avatar asked Oct 01 '22 03:10

Tigran Saluev


1 Answers

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

  • Iterators were added in PEP 234, a proposal created in 2001.
  • xrange() was added to Python in 1993.

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.

like image 85
Martijn Pieters Avatar answered Oct 12 '22 23:10

Martijn Pieters