Iterators & Generators
Definitions
- Lazy evaluation : delays evaluation of an expression until its value is needed
- Iterable : an ovject capable of returning its members one at a time. examples include all sequences ( string, list )and some non-sequence types ( dictionaries )
- Iterator : an object that provide sequenctial access to values, one by one
( all iterators are iterables. not all iterables are iterators. )
- Metaphor : iterables are books & iterators are bookmarks
Iterators
How do we create iterators ?
iter ( iterable ) : return an iterator over the elements of an iterable values
- this method creates a bookmark from a book starting at the front
next ( iterator ) : return the nect element in an iterator
- returns the current page and moves the bookmark to the next page
- the iterator remembers when you left off. If the current apge is the end of the book, error
Check your understanding : Fibonacci
Define a function that returns an iterator that outputs up to the nth value in the Fibonacci sequence. You can assume n will always be 2 or greater
( remember, iter ( iterable ) creates an iterator. Lists are iterables )
def fib_iter(n):
prev, curr = 0, 1
list = [prev, curr]
index = 2
while index < n :
print(prev, curr)
prev, curr = curr, prev + curr
list += [curr]
index += 1
print(list)
return iter(list)
Exceptions / Errors
: sometimes, computer programs behave in non-standard ways
Raise Exceptions
: exceptions are raised with a raise statement
raise < expression >
< expression > must be exceptions
Back to Iterators _ The FOR statement
1. evaluate the header <expression>, which must evaluate to an iterable object.
2. for each element in that sequence, in order:
a. bind <name> to that element in the first frame of the current environment
b. execute the <suite>
Generators
generator : an iterator created automatically by calling a generator function
generator function : a function that contains the keyword yield anywhre in the body
when a geneartor function is called, it returns a generator instead of going into the body of the function. The only way to go into the body of a generator function is by calling next on the returned generator
Generators and Generator functions
- we are allowed to call next on generators because generators are a type of iterator
- calling next on a generator goes into the function and evaluates to the first yield statement. the next time we call next on that generator, it resumes where it left off ( just like calling next on any iterator )
- once the generator hits a return statement, it raises a StopIteration
Generators can Yield from Iterators
: a yield from statement yields all values from an iterable
'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Week 5 (0) | 2019.07.23 |
---|---|
(Higher-Order) Functions, Sequences, Recursion and TreeRecursion, Trees (0) | 2019.07.18 |
LAB_day 6 (0) | 2019.07.16 |
Mutable Functions & Growth (0) | 2019.07.16 |
Week 4 (0) | 2019.07.16 |