Parse the generator in Python and its differences with the iterator.
Generator
A generator is an iterator and a special function. It is constructed into an iterator using the yield operation. A common function has an entry and a return value. When a function is called, it is executed from the entry and the return value is returned at the end. The function defined by the generator has multiple entries and multiple return values. Execute the next () operation on the generator to start code execution. The yield operation returns a value to the caller, then, the function is suspended. When the function is suspended, the environment and parameters of the function execution are saved. When another next () operation is executed on the generator, the parameter is called again from the suspended state, go to the execution environment of the previous suspension and continue the following operations. Repeat the above process in the next yield operation. Python's cyclic operations are different from the implementation of C language. If a List or other data structure is used, a large amount of content is required. In cyclic operations, a generator only needs to instantiate an object in the memory, this reduces memory usage and increases the execution speed of cyclic operations.
>>>def myG():... yield 1... yield 2... yield 3...>>>g=myG()>>>next(g)1>>>next(g)2>>>next(g)3>>>next(g)Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration>>>g2=myG()>>>for i in g2:... print(i)123
Generator expression
The for... [if]... statement can be used to construct a List in a concise manner and build a generator.
>>>a=[7,8,9]>>>b=[i**2 for i in a]>>>b[49, 64, 81]>>>ib=(i**2 for i in a)>>>ib<generator object <genexpr> at 0x7f72291217e0>>>>next(ib)49>>>next(ib)64>>>next(ib)81>>>next(ib)Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration
Differences between Iterator and Generator
An iterator is a more abstract concept. Any object, if its class contains the next method (next python3) and iter method, returns itself.
Each generator is an iterator, but this is not the case. Generally, the generator is generated by calling the function s composed of one or more yield expressions. It also satisfies the definition of the iterator.
When you need a class that has the generator feature and some custom methods, you can use a custom iterator. Generally, the generator is more convenient and easier.
def squares(start, stop): for i in xrange(start, stop): yield i*i
Equivalent to the generator expression:
(i*i for i in xrange(start, stop))
The list type is as follows:
[i*i for i in xrange(start, stop)]
To build a custom iterator:
class Squares(object): def __init__(self, start, stop): self.start = start self.stop = stop def __iter__(self): return self def next(self): if self.start >= self.stop: raise StopIteration current = self.start * self.start self.start += 1 return current
You can also define your own methods as follows:
def current(self): return self.start
Similarities between the two: After an object iteration is complete, the iteration cannot be rewritten.