0
Why Generater in python is memory-efficient?
Does it only store the state of the function required to yield value next time. Does it store only currently yielded result but not previous result? (This way it going to save memory as generator is one time operation, correct me if I'm wrong) If it only store currently executed result, thus it will compute everything again from beginning to yield next result? please explain me thank you.
5 odpowiedzi
+ 1
While a very long [list] alocate alot of memory. It will cause memory error.
& genarator iterate thus alocating less memory.
+ 1
Yes. A big dataset returns the full dataset thus using more memory.
You can think of generators as returning multiple items, as if they return a list, but instead of returning them all at once they return them one-by-one, and the generator function is paused until the next item is requested.
+ 1
Pranto Thank you! Also as it is not storing previous result does it going to compute from beginning again to yield next value?
+ 1
No it wont start over. Pause yeield and start again
0
Pranto Yes we iterating as generator return iterator, but I'm asking reason why it take less memory does it only store currently yielded value in memory when we demand for it?