[Python]Generators
Definition
Generators are special python objects that have the following features:
- iteratable. so one can use next() or with for loop to get the elements.
- return elements when asked, one element each time; unlike list, the elements are not generated until asked for.
- the local state is saved after each call/request, so it can resume when next call arrives.
Advantages
Because of the above features, generators are more memory efficient than lists, and thus used in handling large datastream.
How to construct
There are two ways to make generators, summarized in the following table:
Category | Explanation |
---|---|
Generator functions | use yield other than return to return a value from a function. |
Generator expression | replace the surrounding ‘[]’ of list comprehension with ‘()’. |
Below are examples for each of them:
|
|
In the above first example, yield is used to return a value, and in the second example, a generator is constructed in the format of ‘(xx for xx in an-iterator)’.
Notes
-
A generator object can only be looped over once. To loop again, regenerate it.
-
Generators are more efficient than list only when the dataset size is near or over the limit of momery.
Enjoy programming 😃
References
Last modified on 2018-10-17