Yield after the return in Python function.

Frank Millman frank at chagford.com
Mon Apr 5 08:38:14 EDT 2021


On 2021-04-05 2:25 PM, Bischoop wrote:
> The return suspends the function execution so how is it that in below
> example I got output: <generator object doit at 0x7f57fd2912e0>
> 
> def doit():
>      return 0
>      yield 0
>      
> print(doit())
> 

The 'yield' in the function makes the function a 'generator' function.

'Calling' a generator function does not execute the function, it returns 
a generator object.

You have to iterate over the generator object (e.g. by calling next() on 
it) in order to execute the function and return values.

Frank Millman




More information about the Python-list mailing list