Archive
Generators
“Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called, the generator resumes where it left-off (it remembers all the data values and which statement was last executed).”
Let’s rewrite our Fibonacci function using generators. In the previous approach, we specified how many Fibonacci numbers we want to get. The function calculated all of them and returned a list containing all the elements. With generators, we can calculate the numbers one by one. The new function will calculate a number, return it, and suspend its execution. When we call it again, it will resume where it left off and it runs until it computes another number, etc.
First let’s see a Fibonacci function that calculates the numbers in an infinite loop:
#!/usr/bin/env python
def fib():
a, b = 0, 1
while True:
print a # the current number is here
a, b = b, a+b
fib()
In order to rewrite it in the form of a generator, we need to locate the part where the current value is calculated. This is the line with print a. We only need to replace this with yield a. It means that the function will return this value and suspend its execution until called again.
So, with generators it will look like this:
#!/usr/bin/env python
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a+b
f = fib()
for i in range(10): # print the first ten Fibonacci numbers
print f.next(), # 0 1 1 2 3 5 8 13 21 34
It is also possible to get a slice from the values of a generator. For instance, we want the 5th, 6th, and 7th Fibonacci numbers:
#!/usr/bin/env python
from itertools import islice
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a+b
for i in islice(fib(), 5, 8):
print i # 5 8 13
More info on islice is here. For this post I used tips from here.
Update (20110406)
Here is a presentation in PDF entitled “Generator Tricks For Systems Programmers” by David Beazley (presented at PyCon 2008). (Reddit thread is here.)
Blog Stats
- 1,652,053 hits
Random Post
Recent Posts
Archives
- December 2025 (1)
- December 2024 (2)
- March 2024 (1)
- February 2024 (3)
- June 2023 (1)
- December 2022 (1)
- May 2022 (1)
- March 2022 (1)
- February 2022 (2)
- October 2020 (3)
- September 2020 (2)
- July 2020 (1)
- December 2019 (1)
- November 2019 (7)
- June 2019 (2)
- May 2019 (1)
- February 2019 (1)
- January 2019 (1)
- August 2018 (1)
- July 2018 (4)
- June 2018 (5)
- May 2018 (1)
- March 2018 (2)
- February 2018 (1)
- January 2018 (2)
- December 2017 (2)
- October 2017 (1)
- August 2017 (1)
- May 2017 (2)
- April 2017 (1)
- March 2017 (1)
- February 2017 (3)
- January 2017 (5)
- December 2016 (3)
- October 2016 (2)
- September 2016 (1)
- August 2016 (4)
- July 2016 (3)
- June 2016 (2)
- May 2016 (2)
- April 2016 (1)
- March 2016 (4)
- February 2016 (1)
- January 2016 (4)
- December 2015 (5)
- November 2015 (8)
- October 2015 (2)
- September 2015 (3)
- August 2015 (6)
- July 2015 (4)
- June 2015 (1)
- May 2015 (3)
- April 2015 (2)
- February 2015 (1)
- January 2015 (6)
- December 2014 (5)
- November 2014 (2)
- October 2014 (1)
- September 2014 (1)
- August 2014 (14)
- July 2014 (3)
- June 2014 (6)
- May 2014 (3)
- April 2014 (2)
- March 2014 (4)
- February 2014 (3)
- January 2014 (19)
- December 2013 (8)
- November 2013 (9)
- October 2013 (4)
- September 2013 (10)
- August 2013 (16)
- July 2013 (4)
- June 2013 (7)
- May 2013 (7)
- April 2013 (3)
- March 2013 (12)
- February 2013 (2)
- January 2013 (10)
- December 2012 (18)
- November 2012 (3)
- October 2012 (4)
- September 2012 (5)
- August 2012 (1)
- July 2012 (1)
- May 2012 (8)
- April 2012 (9)
- March 2012 (17)
- February 2012 (3)
- January 2012 (8)
- November 2011 (11)
- October 2011 (7)
- September 2011 (17)
- August 2011 (5)
- June 2011 (1)
- May 2011 (7)
- April 2011 (21)
- March 2011 (21)
- February 2011 (7)
- December 2010 (4)
- November 2010 (1)
- October 2010 (16)
- September 2010 (15)
