Generator
Python generator usage and characteristics
In Python, Generator functions are special functions that return a lazy iterator (or an iterator that is only used when you call it). Lazy iterators are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their items in memory. This makes them memory efficient when working with large data.
The difference is that while a return
statement terminates a function entirely, yield
statement pauses the function saving all its states and later continues from there on successive calls.
Python Generator
The performance improvement from the use of python generators is the result of the on-demand generation of values. This means we donβt need to wait for values to be generated to use them. We can create and use them one by one.
Note: Generator will provide performance benefits only if we do not intend to use that set of generated values more than once.
Generators are an advanced Python topic. 99% of the time regular functions (with return
statements) will work for you code. If you are running into performance problems, consider using generators with your loops.
Letβs take a look at a python generator code sample.