- Python Data Structures and Algorithms
- Benjamin Baka
- 409字
- 2021-07-09 19:44:59
Generators and co-routines
We can create functions that do not just return one result, but rather an entire sequence of results, by using the yield statement. These functions are called generators. Python contains generator functions, which are an easy way to create iterators and they are especially useful as a replacement for unworkably long lists. A generator yields items rather than build lists. For example, the following code shows why we might choose to use a generator as opposed to creating a list:
# compares the running time of a list compared to a generator
import time
#generator function creates an iterator of odd numbers between n and m
def oddGen(n, m):
while n < m:
yield n
n += 2
#builds a list of odd numbers between n and m
def oddLst(n,m):
lst=[]
while n<m:
lst.append(n)
n +=2
return lst
#the time it takes to perform sum on an iterator
t1=time.time()
sum(oddGen(1,1000000))
print("Time to sum an iterator: %f" % (time.time() - t1))
#the time it takes to build and sum a list
t1=time.time()
sum(oddLst(1,1000000))
print("Time to build and sum a list: %f" % (time.time() - t1))
This prints out the following:

As we can see, building a list to do this calculation takes significantly longer. The performance improvement as a result of using generators is because the values are generated on demand, rather than saved as a list in memory. A calculation can begin before all the elements have been generated and elements are generated only when they are needed.
In the preceding example, the sum method loads each number into memory when it is needed for the calculation. This is achieved by the generator object repeatedly calling the __next__() special method. Generators never return a value other than None.
Typically, generator objects are used in for loops. For example, we can make use of the oddcount generator function created in the preceding code to print out odd integers between 1 and 10:
for i in oddcount(1,10):print(i)
We can also create a generator expression, which, apart from replacing square brackets with parentheses, uses the same syntax and carries out the same operation as list comprehensions. Generator expressions, however, do not create a list, they create a generator object. This object does not create the data, but rather creates that data on demand. This means that generator objects do not support sequence methods such as append() and insert(). You can, however, change a generator into a list using the list() function:

- Python快樂編程:人工智能深度學(xué)習(xí)基礎(chǔ)
- C語言程序設(shè)計案例教程(第2版)
- 密碼學(xué)原理與Java實現(xiàn)
- Web Scraping with Python
- Visual Basic程序設(shè)計教程
- NumPy Essentials
- Nginx Essentials
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- C++面向?qū)ο蟪绦蛟O(shè)計習(xí)題解答與上機(jī)指導(dǎo)(第三版)
- Node.js:來一打 C++ 擴(kuò)展
- GameMaker Essentials
- Python編程:從入門到實踐(第3版)
- Android移動應(yīng)用開發(fā)項目教程
- Java服務(wù)端研發(fā)知識圖譜
- 數(shù)據(jù)庫技術(shù)及應(yīng)用教程上機(jī)指導(dǎo)與習(xí)題(第2版)