Python 101 : The "yield" instruction and its generator


The "yield" instruction must "live" within a function. When a function that contains a "yield" statement is called, the execution of the function is halted when it reaches "yield" and a generator object is returned to the caller of the function.

We need to loop over the generator object to get its values.

For example we can use the "yield" statement to get the address our generator:


The below example, gets values from a function through "yield" and the generator it returns:


We could also combine the output of our generator using the below:

Comments

Leave as a comment:

Archive