Home  >  Article  >  Backend Development  >  The difference between python iterator and for loop

The difference between python iterator and for loop

(*-*)浩
(*-*)浩Original
2019-06-28 15:32:576189browse

Iterator (iterator):The iterator object must implement both the __iter__ and __next__ methods to be an iterator. For iterators, __iter__ returns its own self, and __next__ returns the next value in the iterator. When there is no element at the end, an exception is thrown (the exception can be seen by the developer).

The difference between python iterator and for loop

1. The iterator must be an iterable object because it implements the __iter__() method; (Recommended learning: Python video tutorial

2. Through the iter() method (__iter__ inside the class), an iterable object can return an iterator.

3. The __iter__ method of the iterator returns itself and does not generate a new iterator object.

The third property is why iterable objects can be traversed repeatedly (returning an independent iterator each time can ensure that different iteration processes will not affect each other); and iterators because Returns itself, so it can only be traversed once.

The working mechanism of for loop

When the object itself is an iterator, the working mechanism of For loop:

Call_ The _iter__ method returns itself, which is an iterator. Continuously call the iterator's next() method, each time returning a value in the iterator in order. When there are no elements at the end of the iteration, an exception StopIteration is thrown

In an iterable object, the for loop working mechanism:

First determine whether the object is an iterable object ( Equivalent to judging whether there is a __iter__ or __getitem__ method), if not, an error will be reported directly and a TypeError exception will be thrown. If so, call the __iter__ method and return an iterator. The __next__ method of the iterator is continuously called inside python, and each time a value in the iterator is returned in order. When there is no element at the end of the iteration, the StopIteration exception will be thrown. Python will handle this exception by itself and will not expose it to developers.

In addition, please note that the for loop in python is actually compatible with two mechanisms:

If the object has __iter__, an iterator will be returned. If the object does not have __iter__ but implements __getitem__, subscript iteration will be used instead. __getitem__ can help an object perform count and slice operations.

When for finds that there is no __iter__ but there is __getitem__, it will read the corresponding subscripts starting from 0 until an IndexError occurs. This is an old iteration protocol. The iter method will also handle this situation. When __iter__ does not exist, it will return an iterator object with subscript iteration instead. An important example is str. The string does not have the __iter__ method, but it can still be iterated. The reason is that the __getitem__ method is called during the for loop.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of The difference between python iterator and for loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn