简体   繁体   English

Python 2:为什么不将xrange称为irange?

[英]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 . Python 2中有出色的itertools模块,尤其为Python内置函数(例如mapfilterzipslice提供了等效的生成filter And the equivalents are called imap , ifilter , izip , and islice , respectively. 等效项分别称为imapifilterizipislice As I understand, the prefix i in their names means i ​terator. 据我了解,它们名称中的前缀i表示i But there's the same thing about xrange : it is an equivalent of range returning generator object instead of large list. 但有同样的事情xrange :它的等效range返回发生器对象,而不是大名单。 So, why isn't it called irange ? 那么,为什么不叫irange呢? What does prefix x actually mean? 前缀x实际上是什么意思?

Because xrange() was added to the language before it had iterators and it is itself not an iterator. 因为xrange()是在它具有迭代器之前添加到语言的,所以它本身不是迭代器。

Python 1.0 was released in 1994, so xrange() was added very early on. Python 1.0于1994年发布,因此xrange()很早就添加了。 The x most likely doesn't have a specific meaning here. x很可能在这里没有特定含义。

But xrange() is not really an iterator, it is an iterable ; 但是xrange()并不是真正的迭代器,它是可迭代的 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. 这样,该对象已重命名为range() (完全取代了Python 2 range()函数),并且其序列行为得到了进一步扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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