Generator is the responsible to generate the
sequence of values. It is going to generator the values by using ‘yield’
keyword.
It generators one value at a time, one by one value.
Here ‘return’ statement and ‘yield’ statement almost
same
return
|
Yield
|
It is keyword
|
It is also keyword
|
It executes only once
|
Multiple times can be executed
|
When return statement is executed, control comes
out of the function
|
Since it executes multiples times, a function can
return no of yield statements
|
When do we use ‘Generators’ in your
program?
Whenever you are fetching huge data or records from
the data base, you want to print all the lakhs of records (data), while
executing the huge records, these records will be loaded in the memory, unnecessarily
memory will be wasted with huge data (records) so I want to work one value at a
time in that case we can use generators concept instead of getting the entire
data at a time.
Related Video: https://youtu.be/pNpmiKhOhKQ
Previous: decorator chaining
https://youtu.be/d-qTxNTiZPM
Next:
No comments:
Post a Comment