0
Generator use case in python
I googled this question but I did not understand the use case for generator.
3 Respostas
+ 8
Generator stores the "potential" to create a collection-like data type. The memory needed to store the generator pattern is usually orders of magnitude lower than the full data structure it creates when called.
+ 7
Generator are functions that are runned to return a sequence of results, paused and resumed between each item generated, as a dunamicaly builded list...
They are use to write custom function as the range() built-in function generator, but with customized data returned/generated ;)
- 1
Expanding what @kuba said: the memory footprint for generators is constant. the current element and the function to compute the next.
map, reduce, filter, range all return a generator.
they are used where lists can be used but not all of the list elements are required at once.
what generators and the generator pattern is to OOP, streams are to FP. that is to say, if you look up streams in eager languages such as sml, scheme, lisp, any ml for that matter, you will be pretty close. lazy ones such as Haskell have just streams in place of lists with slightly different style of list evaluation.