SiteMap  | About    

What does the 'yield' keyword do?

If the compiler detects the yield keyword anywhere inside a function, that function no longer returns via the return statement. Instead, it immediately returns a lazy 'pending list' object called a generator. A generator is iterable. iterable is anything like a list or set or range or dict-view, with a built-in protocol for visiting each element in a certain order.

So basically, a function with 'yield' is not a normal function anymore, instead it becomes a generator . Every time the code is executed to "yield", it returns the right side of "yield", then it continues to loop the code.


In the example above, the "yield" statement suspends function's execution and sends a value back to caller in each iteration, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.