Many languages provide a way to iterate over objects
A true iterator provides a way to iterate without storing
intermediate data structures
# Never creates a list of items in memoryfor i inrange(10):print(i)# Creates a list of items in memory, then iterates over themfor i in [0,1,2,3,4,5,6,7,8,9]:print(i)
Python Iterator
Includes an __iter__ method that returns an
iterator
Includes a __next__ method that returns the next
item
Duck Typing
We can identify the type of an object by its properties
If it walks like a duck and quacks like a duck, it is a duck
We can implement and iterator without inheriting from an iterator
class
defrange(start, stop=None, step=1):if stop ==None: stop = start start =0 current = start results = []while current < stop: results.append(current) current += stepreturn results
Generators
Provide a generalized construct for creating iterators
We can yield successive values
defrange(start, stop=None, step=1):if stop ==None: stop = start start =0 current = startwhile current < stop:yield current current += step
def squares(): i =1while(True):yield i*i i +=1results = squares()for i inrange(20):print(next(results))
def primes(): current =2whileTrue:if is_prime(current):yield current current +=1