In the For loop, control is returned to the for loop after each yield value
The generator is similar to a function that returns an array. The generator has parameters, can be called, and generates a sequence of values. Unlike functions that return the entire array at once, the generator generates only one value at a time, which consumes very little memory and the caller can immediately process the resulting value. In summary, the generator looks like a function, but it is used like an iterator.
A tool provided by Python that produces results when needed:
-Generator function:
Use DEF definitions, but each time yield is used to generate a return value value, suspend, and continue running.
-Generator expression:
Similar to list derivation, but instead of creating a list of results, each time you build the object as needed.
Because neither the generator function nor the generator expression produces a list of results at a time, this saves memory space and divides the calculation time by iterating over the protocol.
Generator functions: Yield and return
Generator functions are a bit like regular functions, and are defined using DEF. When created, the iterative protocol is implemented automatically.
The regular function returns a value and exits. When the iterator function returns a value, it is automatically suspended and then executed again.
The main difference between a generator function and a regular function is that the former yields a value, which returns a value. Yield suspends the function, returning a value to the caller.
The generator is tied to the iteration protocol. An iterative object defines a method: __next__ (), which either returns the next value of the iterator or throws an exception.
When defining a generator, you need to use the keyword: yield.
Let's look at an example:
1. The keyword yield indicates that the function is not a regular function, but rather an iterator function.
2. Generate an instance of an iterator, similar to calling a regular function. But the call does not actually execute the function code.
3.counter () returns an Iterator object
For example, the following iterator produces a cubic value of a number
>>> def cubic_generator (n): For I in range (n): yield i**3>>> CG = cubic_generator (3) >>> cg< Generator Object Cubic_generator at 0x0000000002a72cf0>>>> cg.next () 0>>> cg.next () 1>>> Cg.next () 8>>> cg.next () Traceback (most recent call last): File ' <pyshell#32> ', line 1, in <module& Gt
In the For loop, control is returned to the for loop after each yield value:
If you use return instead of yield:
>>> def cubic_generator (n): For I in range (n): Return i**3>>> for I in Cubic_generator (5):p rint (i) Traceback (most recent): File "<pyshell#54>", line 1, in <module> for I in Cubic_generator (5 ): TypeError: ' int ' object is not iterable>>> Cubic_generator (5) 0>>>
Example 1 using the generator:
To implement a Fibonacci sequence using a generator:
Builder expression: An iterator that can be deduced
The combination of iterators and list derivation creates a new feature: the generator expression.
The generator expression and the list expression are similar, but the former is enclosed by parentheses, which are enclosed in square brackets.
>>> gen= (x**3 for x in range (5)) >>> Gen.next () 0>>> gen.next () 1>>> gen.next () 8> >> gen.next () 27>>> gen.next () 64>>> gen.next () Traceback (most recent call last): File "< Pyshell#17> ", line 1, in <module>
Generator: function vs expression
The same iterations can be implemented with a generator function or builder expression. Both can be automatically iterated or manually iterated.
Python--Generator