Let's talk about generator expressions in this section.
Syntactically speaking, the generator expression is the same as the list parsing, except that the list parsing is placed inside the brackets, and the generator expression is enclosed in parentheses.
As you can see from the code above, the parentheses return a generator to use the list function to parse the contents.
To put it bluntly, from the results above, in fact, list parsing is using the list function to force the generator to generate all the results at once.
>>> [x for X in range (5)][0, 1, 2, 3, 4]
From the execution above, list parsing builds all the results in memory at once, and the generator expression simply returns an object, and this object supports iterative protocols, as long as it can be manipulated in an iterative environment.
For example, we can get the value of the object inside the generator by using for.
As in the above code, you can support the operation of an object in an environment that supports iterative protocols such as map, filter, and sorted.
Summary: This chapter focuses on the relationship between the generator and list resolution, and the generator supports iterative protocols that can manipulate objects in an iterative environment.
This chapter is here, thank you.
------------------------------------------------------------------
Click to jump 0 basic python-Catalogue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
0 Fundamentals python-19.8 Generator expression: When an iterator encounters a list resolution