Part 1: Iterators
Generators & Coroutines
Edited Version of Slides by David Beazely
Introduction to Iterators and Generators
Part 1: Iterators
Monday, May 16, 2011
Copyright (C) 2008,
1- 11
Iteration
? As you know, Python has a "for" statement
? You use it to loop over a collection of items
>>> for x in [1,4,5,10]:
...
print x,
...
1 4 5 10
>>>
? And, as you have probably noticed, you can
iterate over many different kinds of objects
(not just lists)
Copyright (C) 2008,
1- 12
Monday, May 16, 2011
Iterating over a Dict
? If you loop over a dictionary you get keys
>>> prices = { 'GOOG' : 490.10,
...
'AAPL' : 145.23,
...
'YHOO' : 21.71 }
...
>>> for key in prices:
...
print key
...
YHOO
GOOG
AAPL
>>>
Copyright (C) 2008,
1- 13
Copyright (C) 2008,
1- 13
Iterating over a String
Iterating over a File ? If you loop over a string, you get characters >>> s = "Yow!"
>>> for c in s:
? I..f....you loproinpt ocver a file you get lines
Y
>>o> for line in open("real.txt"):
..w.
print line,
..!.
>>>
Real Programmers write in FORTRAN
Maybe they do now,
in this decadent era of
Lite beer, hand calculators, and "user-friendly" software
but back in the Good Old Days,
when the and Real Copyright (C) 2008,
term "software" sounded Computers were made out
funny of drums
and
vacuu1m- 1t4ubes,
Real Programmers wrote in machine code.
Not FORTRAN. Not RATFOR. Not, even, assembly language.
Machine Code.
Raw, unadorned, inscrutable hexadecimal numbers.
Directly.
Copyright (C) 2008,
1- 15
Consuming Iterables
? Many functions consume an "iterable" object ? Reductions:
sum(s), min(s), max(s)
? Constructors
list(s), tuple(s), set(s), dict(s)
? in operator
item in s
? Many others in the library
Copyright (C) 2008,
1- 16
Iterating over a File
? If you loop over a file you get lines
>>> for line in open("real.txt"):
...
print line,
...
Real Programmers write in FORTRAN
Maybe they do now, in this decadent era of Lite beer, hand calculators, and "user-friendly" software but back in the Good Old Days, when the term "software" sounded funny and Real Computers were made out of drums and vacuum tubes, Real Programmers wrote in machine code. Not FORTRAN. Not RATFOR. Not, even, assembly language. Machine Code. Raw, unadorned, inscrutable hexadecimal numbers. Directly.
Copyright (C) 2008,
1- 15
Consuming Iterables
? Many functions consume an "iterable" object
? ReIdtuectironas:tion Protocol sum(s), min(s), max(s)
??ThCeornesatrsuocntowrhsy you can iterate over different objleicstts(si)s, tthuapltet(hs)e,reseits(sa),spdieccti(fisc) protocol
? >i>n> oitpeemsra=to[1r, 4, 5] >>> it = iter(items) >>>itietm.nienxts()
?1 >M>>ainty.noextth(e) rs in the library 4 >>> it.next() 5
Copyright (C) 2008, http://w>w>w.>dabeaiz.ctom.next() Traceback (most recent call last): File "", line 1, in StopIteration >>>
1- 16
Copyright (C) 2008,
1- 17
Copyright (C) 2008,
1- 17
Iteration Protocol
? An inside look at the for statement
for x in obj: # statements
? USnudeprnepatoh trhetcionvegrs Iteration
_iter = iter(obj)
# Get iterator object
? while 1: Usert-rdy:efixn=ed_itoebr.jenecxtts()can s#upGeptonretxtiteitreamtion
? Examexpceleptb: rSeCtaokopuItnetriantgiodn:own.#.. No more items
>>> #.f.os.rtaxteimnenctosuntdown(10):
? ...
print x,
A..n.y object that supports iter() and next() is
s1>a0>>id9 t8o7 b6e5"4ite3r2ab1le."
? To do this, you just have to make the object
Copyright (C) 2008,
1-18
implement __iter__() and next()
Copyright (C) 2008,
1-19
Supporting Iteration
? Sample implementation
class countdown(object): def __init__(self,start): self.count = start def __iter__(self): return self def next(self): if self.count >> for x in countdown(10):
...
print x,
...
10 9 8 7 6 5 4 3 2 1
>>>
? To do this, you just have to make the object implement __iter__() and next()
Copyright (C) 2008,
1-19
Supporting Iteration
? Sample implementation
Iteration Example class countdown(object): def __init__(self,start):
self.count = start
def __iter__(self):
return self
def next(self):
if self.count >> c =seclofu.nctoduonwtn(-5=) 1 >>> forreituirnncr:
...
print i,
...
5 4 3 2 1
>>>
Copyright (C) 2008,
1-20
Copyright (C) 2008,
1-21
Copyright (C) 2008,
1-21
Iteration Commentary
? There are many subtle details involving the design of iterators for various objects
? However, we're not going to cover that ? This isn't a tutorial on "iterators" ? We're talking about generators...
Copyright (C) 2008,
1-22
Part 2: Generators
Monday, May 16, 2011
Generators
? A generator is a function that produces a sequence of results instead of a single value
def countdown(n):
while n > 0:
yield n
n -= 1
>>> for i in countdown(5):
...
print i,
...
5 4 3 2 1
>>>
? Instead of returning a value, you generate a series of values (using the yield statement)
Copyright (C) 2008,
1-23
Generator Functions
? The function only executes on next()
>>> x = countdown(10)
>>> x
>>> x.next() Counting down from 10 10
Function starts executing here
>>>
? yield produces a value, but suspends the function
? Function resumes on next call to next()
>>> x.next() 9 >>> x.next() 8 >>>
Copyright (C) 2008,
1-25
Copyright (C) 2008,
1-25
Generator Functions
Generator Functions ? When the generator returns, iteration stops
>>> x.next() 1 >>> x.next() Traceback (most recent call last):
File "", line 1, in ?
? StopIteration A>>g>enerator function is mainly a more
convenient way of writing an iterator
? You don't have to worry about the iterator protocol (.next, .__iter__, etc.)
? It just works Copyright (C) 2008,
1-26
Copyright (C) 2008,
1-27
Generators vs. Iterators
? A generator function is slightly different than an object that supports iteration
? A generator is a one-time operation. You can iterate over the generated data once, but if you want to do it again, you have to call the generator function again.
? This is different than a list (which you can iterate over as many times as you want)
Copyright (C) 2008,
1-28
Generator Functions
? A generator function is mainly a more convenient way of writing an iterator
? You don't have to worry about the iterator protocol (.next, .__iter__, etc.)
? It just works
Copyright (C) 2008,
1-27
Generators vs. Iterators
? A generator function is slightly different than an object that supports iteration Generator Expressions A generator is a one-time operation. You
??caAn giteenreartaeteodvevretrhsieongeonfearaltisetdcdoamtaproenhceen,sion
bu>t>>if ayo=u[1w,2a,n3,t4t]o do it again, you have to cal>>l>>>>thbbe=ge(2n*exrfaotroxr ifun nac)tion again.
? Th>i>s>isfodr ififeirnebn:t ptrhiantn ba, list (which you can ite.2r.a.4te6 8over as many times as you want)
>>>
? This loops over a sequence of items and applies
an operation to each item Copyright (C) 2008,
1-28
? However, results are produced one at a time using a generator
Copyright (C) 2008,
1-29
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- part 1 illuminating photosynthesis answers
- part 1 illuminating photosynthesis worksheet
- ielts writing part 1 tips
- ielts speaking part 1 questions and answers
- ielts speaking part 1 education
- ielts speaking part 1 sample
- ielts speaking part 1 questions
- ielts speaking part 1 vocabulary
- ielts speaking part 1 question
- ielts speaking part 1 history
- ielts speaking part 1 samples
- breaking dawn twilight part 1 full movie