yield

say it: “YEELD” /jiːld/

Hands back one value from a generator and pauses until the next one is needed.

What does yield do?

yield hands one value back from a generator and pauses there. The next time a value is asked for, the function carries on from where it stopped.

def read_orders(path):
    with open(path) as file:
        for line in file:
            yield line.strip()     # one order at a time

for order in read_orders("orders.txt"):
    print(order)

Level: Freelancer · More Python terms at this level

Updated

In other subjects