简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM