0
What is the difference between return and yield in a function?
1 Respuesta
+ 7
Return stops function execution, and when you call a function it will RETURN some value.
If you are using yield, function can return values multiple times
For example:
def foo():
for i in range(10):
yield i
when you will execute foo it will return GENERATOR. It means, that it will generate some values for you.
You can get them all using list(foo()), that gets all values and stores them in list
Or simply loop trough each value instantly:
for i in foo()
The same thing with return will NOT work. It will return first number(0), and then stop the function