Archive Pages Design$type=blogging

Yield in Python

Yield keyword is used to define return of generator, replacing the return of a function to provide a
result to its caller without destroying local variables. Unlike a function, where on each call it starts with new set of variables, a generator will resume the execution where it was left off.


Generators are iterators, but you can only iterate over them once. It's because they do not store all the values in memory, they generate the values on the fly:

>>> pythonfaq_generator = (x*x for x in range(4))
>>> for i in pythonfaq_generator:


Yield is a keyword that is used like return, except the function will return a generator.\

>>> def createGenerator():
...    mylist = range(3)
...    for i in mylist:
...        yield i*i
>>> mygenerator = createGenerator() # create a generator
>>> print(mygenerator) # mygenerator is an object!
<generator object createGenerator at 0xb7555c34>
>>> for i in mygenerator:
...     print(i)

Image Source-
advance algorithms analytics basic comprehension data analysis data mining data science data structure ebook google interview latest online learning overview python yield
Python Faq: Yield in Python
Yield in Python
yield in python example python 2.4 yield generators in python call function in python python yield statement python yield return python yield tutorial python yield vs return python iterator yield python 3 yield python using yield yield keyword python python reset iterator python iterator length using python generators python cycle list
Python Faq
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago