423,851 Members | 2,762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

Have you heard of the yield keyword in Python?

P: 5
Have you heard of the yield keyword in Python?
3 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


P: 5
Yes, I have. This keyword bears the ability to turn any function into a generator. Much like the standard return keyword, but returns a generator object. It is also true that one function may observe multiple yields.

>>> def odds(n):
odd=[i for i in range(n+1) if i%2!=0]
for i in odd:
yield i
>>> for i in odds(8):
print(i)
1
3
5
7
2 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.