first, what is recursion
If a function contains a call to itself, the function is recursive. Recursion is an algorithm widely used in programming language, it usually transforms a large and complex problem layer into a small scale problem similar to the original problem to solve, the recursive strategy needs a few programs to describe the process of solving the problem of repeated computations, greatly reducing the code of the program. For example, to calculate the product of 1-9 of 9-bit numbers, the intuitive algorithm is 1*2*3*4*5*6*7*8*9, if you want to calculate the product of 1-10000, the intuitive algorithm is difficult to achieve, and recursion can be very simple implementation. Take a look at the example:
def fact (N): #计算给定数字到一的乘积 if n<=1: return 1 else: return n * fact (n-1) print (Fact (7))
The result is: 5040
Let's take a look at the recursive execution process using the example:
Def calc (n): print (n) if N/2 > 1: res = calc (N/2) return N Calc (8)
The result is:
84.02.0
Look again at this example:
Def calc (n): print (n) if N/2 > 1: res = calc (N/2) print (' res: ', res) print ("N:", N) return Ncalc (8)
The result is:
84.02.0n:2.0res:2.0n:4.0res:4.0n:8
Second, generator
The generator is a function with a yield statement. A function or subroutine is returned only once, but a generator can pause execution and return an intermediate result, returning a value to the caller and pausing execution. When the next () method of the generator is called, it will proceed exactly from where it left off
Here's a look at the example:
def func (): print (' 11111111 ') yield [1] print (2222222222) yield 2 print (3333333333) yield 3ret=func () r1=ret.__next__ () print (R1) r2=ret.__next__ () print (R2) r3=ret.__next__ () print (R3)
The result is:
11111111[1]2222222222233333333333
Because Python's for Loop has next () calls and handles to stopiteration, using a for loop instead of a manual iteration through a generator (or an iterator of that sort) is always a lot more concise and beautiful. Cases:
def func (): print (' 11111111 ') yield [1] print (2222222222) yield 2 print (3333333333) yield 3ret=func () for i in RET: print (i)
The result is the same as before.
These simple examples should give you a little idea of how the generator works. In addition to next () to get the next generated value, the user can return the value to the generator [Send ()], throw an exception in the generator, and require the generator to exit [Close ()]
Here is a simple example that shows these features.
def counter (start_at=0): Count = start_at while True:val = (yield count) if Val was not None:count = Val else: Count + = 1
The generator comes with an initialized value, with a 1 cumulative count for each call to the generator [next ()]. The user has the option to reset this value if they really want to call send with a new value () instead of calling next (). This generator is always running, so if you want to end it, call the Close () method. If we run this code interactively, we will get the following output:
>>> count = Counter (5) >>> count.next () 5>>> count.next () 6>>> count.send (9) 9> >> count.next () 10>>> count.close () >>> count.next () Traceback (most recent call last): File "< Stdin> ", line 1, in <module>stopiteration
This in-depth understanding of Python function recursion and generator is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support topic.alibabacloud.com.