Categories

Dec 22, 2019 in Python
Q: What Does The Yield Keyword Do In Python?

1 Answer

Dec 22, 2019

The yield keyword can turn any function into a generator. It works like a standard return keyword. But it’ll always return a generator object. Also, a method can have multiple calls to the yield keyword.

See the example below.

def testgen(index):

  weekdays = ['sun','mon','tue','wed','thu','fri','sat']

  yield weekdays[index]

  yield weekdays[index+1]

day = testgen(0)

print next(day), next(day)

#output: sun mon

Click here to read more about Python
Click here to read more about Insurance

Related questions

Madanswer
Aug 30 in Python
Aug 29 in Python
Dec 14, 2019 in Python
...