- 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:

- C++程序設(shè)計(jì)教程
- Oracle 12c中文版數(shù)據(jù)庫管理、應(yīng)用與開發(fā)實(shí)踐教程 (清華電腦學(xué)堂)
- Web Development with Django Cookbook
- Visual C#通用范例開發(fā)金典
- Learning Apache Karaf
- 速學(xué)Python:程序設(shè)計(jì)從入門到進(jìn)階
- Spring Boot+Vue全棧開發(fā)實(shí)戰(zhàn)
- JavaScript程序設(shè)計(jì)(第2版)
- Java圖像處理:基于OpenCV與JVM
- Deep Learning with R Cookbook
- Illustrator CS6設(shè)計(jì)與應(yīng)用任務(wù)教程
- JavaScript悟道
- Practical Maya Programming with Python
- Keil Cx51 V7.0單片機(jī)高級語言編程與μVision2應(yīng)用實(shí)踐
- Scratch編程從入門到精通