![]() yield is similar to return as it returns a value from a function, but the only difference is that yield returns a value only when it is needed and does not try to keep the entire dataset in memory. If you head over to your browser, you should see data being displayed on the page. Given the appropriate time, the browser eventually displays the data. Note: Generators can only be used from a function. There are times when we might want to parse a large dataset (it can be log files), perform computation on a large database result, etc. We don’t want actions like this hogging all the memory. We should try to conserve memory as much as possible. The data doesn’t necessarily need to be large - generators are effective no matter how small a dataset is. Don’t forget, our aim is speed while using less memory. There are times when our data only make sense when they are key-value based. When using generators, we can yield key-value pairs like this. ![]() We can then go ahead and use the pair as we would do with any array like this. This means that generators allow us to inject values into them, maybe as a command or something. For example, we can send a value to our generator telling to stop execution or change the output. Using the getRange function above, we can do this. ![]() Note: Using return in a generator breaks out of the generator function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |