Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between yield in Python and yield in C#

Tags:

python

c#

What is the difference between yield keyword in Python and yield keyword in C#?

like image 886
maxyfc Avatar asked Nov 09 '09 23:11

maxyfc


People also ask

What's the difference between Python yield and return statement?

Yield is generally used to convert a regular Python function into a generator. Return is generally used for the end of the execution and “returns” the result to the caller statement. It replace the return of a function to suspend its execution without destroying local variables.

What is the yield in Python?

Yield is a keyword in Python that is used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. Any function that contains a yield keyword is termed a generator.

Is yield faster Python?

At least in this very simple test, yield is faster than append.

Should I use yield in Python?

We should use yield when we want to iterate over a sequence, but don't want to store the entire sequence in memory. Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return.


1 Answers

C#'s yield return is equivalent to Python's yield , and yield break is just return in Python.

Other than those minor differences, they have basically the same purpose.

like image 156
John Millikin Avatar answered Sep 17 '22 22:09

John Millikin