yield

The keyword that makes a function a generator: yield value hands one value to the caller and pauses until the next one is asked for.

In more detail

Local variables keep their values between yields. When the function body ends, the generator stops and the for loop using it finishes. Testing tools such as pytest also use yield to split a fixture into setup code before it and teardown code after it.

Related terms

Taught in

References

Certification courses