Discussion about the Range object in Python is not an iterator

Source: Internet
Author: User
Tags iterable
An iterator (iterator) is a lazy, iterative object (lazy iterable), and the range function is an inert, iterative object in Python 3, and range is not an iterator? Why.

The Range object in Tlnr:python 3 (the Xrange object in Python 2) is lazy, but the Range object is not an iterator.



Yes, it's confusing.

When you talk about Iterators (iterator) and iterative objects (iterable) in Python, you are likely to hear that someone repeating range is a misunderstanding of the iterator. I think this is a very serious misunderstanding, and if you think the Range object is an iterator, then your mental model of how "iterators work" is not clear enough. In a sense, range and iterators are "inert", but they are "inert" in quite different ways.


What is an iterator (iterator)

In Python, an iterative object is anything you can iterate over, and an iterator is something that actually iterates.

iter-Ables is able to being iterated over. iter-ators is the agents that perform the iteration.

You can use the ITER function to get iterators from any iterator object:

Once you have an iterator, the only thing you can do with it is to get the next element of it:

If there are no more elements, a stop iteration exception will be thrown:

All iterators are iterative objects, which means that you can get an iterator from an iterator, so you can traverse an iterator:

It should be noted that the iterator is stateful, and if you try to loop again after iterating through the iterator, it will be empty:

in Python 3, enumerate、zip、reversed and some other built-in functions return iterators:

The generator, whether from a generator function or a builder expression, is a simple way to create an iterator:

I often say iterators are lazy, disposable, iterative objects. "Lazy" is because they only loop through the project, "one-time use is because once an element has been" consumed "from an iterator, the element disappears forever.


What is range

The Range object in Python 3 (xrange in Python 2) can be reused like any other iterative object:

Because range is an iterative object, you can get an iterator from it:

But the Range object itself is not an iterator, and we cannot call next on the Range object:

Unlike iterators, we can traverse a Range object without "consuming" it:


If we use iterators to do this, then the second loop will not get any elements:


zip Code style= "box-sizing:inherit;margin-right:2px;margin-left:2px;padding:3px 4px;border-radius:3px;" >enumerate generator


So, exactly what range is

The Range object is "lazy" in a sense because it does not generate every number that is contained in the creation, but instead returns the numbers to us when we need them in the loop.

The following is a Range object and a generator (an iterator):

Unlike generators, a Range object has a length:

and can be indexed:

Unlike iterators, you can ask them if they contain an element without changing their state:

If you want a description of a Range object, you can call them 懒序列 , Range is a sequence (such as a list, a tuple, and a string), but does not contain any in-memory content, but rather computes to answer the question.


Why this distinction is important

If I tell you that an object is an iterator, you know that when you call the ITER function on this object, you always get the same object (by definition):

Be sure to call the next function on this object, because you can call the next function on all iterators:

And you'll know that when you traverse it, these elements will be consumed from the iterator, and sometimes this feature can come in handy (to handle iterators in a special way):

So while it may seem that the difference between lazy, iterative objects and iterators is subtle, these terms do mean something different. Although lazy iterative objects is a very common term with no specific meaning, the term "iterator" implies an object with a very specific behavior.


Summarize

If you know that you can iterate over an object, this is an iterative object (iterable).

If you know that the object you are looping through is computed at the time of the loop, then this is an inert iterative object (lazy iterable).

If you know that you can pass something to the next function, it is an iterator (this is the most common lazy iterative object).

related recommendations:

python an understanding of objects and iterator objects can be iterated

range object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.