官术网_书友最值得收藏!

Generators and comprehensions

A generator is a special kind of iterator in Python. In other words, a Python generator is a function that returns us a generator iterator by issuing the yield command, which can be iterated upon. There might be occasions in which we would want a method or function to return us a series of values, instead of just one. We might, for example, want our method to partially carry out a task, return the partial results to the caller, and then resume the work right from the place where it returned the last value. Usually, when a method terminates or returns a value, its execution begins again from the start. This is what generators try to address. A generator method returns a value and a control to the caller and then continues its execution right from where it left off. A generator method is a normal Python method with a yield statement. The following code snippet, generators.py, explains how generators can be used:

 

Note that since genMethod has a yield statement in it, it becomes a generator. Every time the yield statement is executed, the value of "a" is returned to the caller as well as the control (remember that generators return series of values). Every time the next() call is made to the generator method, it resumes its execution from where it left off previously. 

We know that every time a yield is executed, the generator method returns a generator iterator. Thus, as with any iterator, we can use a for loop to iterate over the generator method. This for loop will continue until it reaches the yield operation in the method. The same example with a for loop would look as follows:

You might be wondering why we would use generators when the same result can be achieved with lists. Generators are very memory- and space-efficient. If a lot of processing is required to generate values, it makes sense to use generators, because then we only generate values according to our requirements.

Generator expressions are one-line expressions that can produce generator objects, which can be iterated over. This means that the same optimization in terms of memory and processing can be achieved. The following code snippet shows how generator expressions can be used:

主站蜘蛛池模板: 射洪县| 托里县| 留坝县| 康乐县| 延津县| 博白县| 蓝田县| 明水县| 泸溪县| 玛曲县| 平陆县| 金山区| 汝南县| 南宁市| 华容县| 宕昌县| 正定县| 霸州市| 杨浦区| 城固县| 宜宾市| 潢川县| 平潭县| 文登市| 漠河县| 大埔区| 大埔区| 临沧市| 宜州市| 高碑店市| 阜平县| 华宁县| 利津县| 积石山| 内黄县| 丁青县| 嘉鱼县| 比如县| 洛宁县| 凭祥市| 石泉县|