Learning python (15)-iterators and generators

1. Iterator

Columnar containers have a common feature, they all support the use of a for loop to traverse the stored elements, and they are all iterable, so they have another name, namely iterators. Literally understood, an iterator refers to a container that supports iteration. More specifically, it is a container-like object that supports iteration. The container here can be  a basic container provided by Python such as lists and tuples  , or it can be a custom An object of the container class, as long as the container supports iteration.

If you want to customize an iterator, the following two methods must be implemented in the class:

  1. __next__(self): Returns the next element of the container.
  2. __iter__(self): This method returns an iterator.

In addition, Python's built-in iter() function also returns an iterator. The syntax of the function is as follows:

iter(obj[, sentinel])

Among them, obj must be an iterable container object, and sentinel is an optional parameter. If this parameter is used, obj must be a callable object. Callable object refers to the instance object of this class can be used directly in the form of "object name ()" like a function. By adding the __call__() method to the class, the instance object of the class can be programmed as a callable object. Regarding the __call__() method, we usually use the iter() function with only 1 parameter. By passing in an iterable container object, we can get an iterator, by calling __next__() in the iterator Method can achieve iteration. In addition, you can also use the next() built-in function to iterate, namely next(myIter), which is exactly the same as the __next__() method.

It can be seen from the execution result of the program that after iterating all the stored elements, if the iteration continues, the __next__() method will throw a StopIteration exception. Here is an introduction to the role of the second parameter of the iter() function. If this parameter is used, the first obj parameter must be passed into the callable object (iteration may not be supported), so that when the returned iterator is used to call __next__() Method, it will call the __call__() method by executing obj(). If the return value of the method is the same as the second parameter value, it will output StopInteration exception; otherwise, it will output the return value of the __call__() method. The iterator itself is a low-level feature and concept that is not commonly used in programs, but it provides the basis for the more interesting feature of generators.

2. Generator

When using the container to iterate a set of data, all the data must be stored in the container in advance before iterating can start; but the generator is different, it can generate elements while iterating. In other words, for multiple data that can be calculated by a certain algorithm, the generator will not generate them at once, but when they are needed and when they are generated. Not only that, the way to create a generator is much simpler than that of an iterator, which is roughly divided into the following two steps:

  1. Define a function whose return value is identified by the yield keyword;
  2. You can create a generator by calling the function you just created.

To enable the generator function to be executed, or to continue the execution of the program that has been suspended immediately after the yield statement is executed, there are two ways:

  1. Call the next() built-in function or __next__() method through the generator (num in the above program);
  2. Traverse the generator through a for loop.

Note that the __next__() method cannot be used in the Python 2.x version, the next() built-in function can be used, and the generator also has the next() method (that is, called by num.next()). In addition, you can also use the list() function and tuple() function to directly store all the values ​​that the generator can generate in the form of a list or tuple. Compared with iterators, the most obvious advantage of generators is to save memory space, that is, it does not generate all the data at once, but when it is needed and when.

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/113662834