- Hands-On Penetration Testing with Python
- Furqan Khan
- 387字
- 2021-07-02 14:13:56
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:

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:

- Go Web編程
- Java程序設計(慕課版)
- Visual FoxPro程序設計教程(第3版)
- 造個小程序:與微信一起干件正經事兒
- Mastering C# Concurrency
- Data Analysis with Stata
- 21天學通C++(第5版)
- 代替VBA!用Python輕松實現Excel編程
- 深入實踐Kotlin元編程
- Deep Learning with R Cookbook
- 大數據時代的企業升級之道(全3冊)
- WordPress Search Engine Optimization(Second Edition)
- 零基礎C語言學習筆記
- Java程序性能優化實戰
- Netty 4核心原理與手寫RPC框架實戰