In the daily situation, it may involve a data model, that is, the sum of the first two numbers added together, equal to the third number, where we can refer to the Fibonacci series data model, for example, the following numbers
0,1,1,2,3,5,13,13,21,34,55,144,233,377,610
#打印出如上述数列, the first two numbers are added equal to the number of subsequent numbers (here is a restriction condition if arg3>10, return ARG3, otherwise an unrestricted loop)
#顶一个函数func
def func (ARG1,ARG2): if arg1 = = 0:pass Arg3 = arg1 + arg2 if Arg3 >10:return arg3 func (arg2,arg3) result = Fun C (0,1) print result
The results of the operation are as follows:
None
To analyze the above code, let's look at the following code:
#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' Ryan ' def fun5 (): Return def fun4 (): Fun5 ():d EF fun3 () : Fun4 () def fun2 (): Fun3 () def fun1 (): fun2 () result =fun1 () print result
The code is an example of a function calling another function, which runs the code with the following result:
None
That is, result is none, all places in the code are functions, the final definition is to assign the function func1 () to the variable result, the output variable to none, the function Func1 no return value, or the definition of the return value is None, By the above code can be found that FUNC1 does not define a return value of None, so the default returns none, so the code is interpreted as follows:
When we execute RESULT=FUNC1 (), FUNC1 executes the following code:
Def fun1 (): Fun2 ()
When the Def func1 () is executed, FUNC2 () is executed, and the FUNC2 content is as follows:
Def fun2 (): Fun3 ()
Similarly, when the Def Func2 () is executed: the FUNC3 () is executed again, and if there is a return value in Func2, the return value is returned to the function that called it func1
Again to execute func3 (), i.e.:
Def fun3 (): Fun4 ()
Then execute FUNC4 (), i.e.:
Def fun4 (): Fun5 ()
and the function Func5 code is as follows:
Def FUNC5 ():
return 13
From the above code can be found, when Func4 () call Func5, FUNC5 will return a number 13, the value is the return value of the function Func5, the return value will be assigned to call his function, where the FUNC5 function is called FUNC4, So at this point the Func5 in Func4 is actually the result of the return 13 statement, which is the result of 13, so Func4 has a number 13 (return value 13), namely:
Def Func4 (): Def func4 (): Func5 () ======================================> 13
The FUNC4 does not return a value at this point, so the actual code is:
Def func3 (): Func4
Therefore, this call also has no return value, because FUNC3 has no return value, so the subsequent FUNC2 call has no return value, so the final result above is None
So the essence of the code is:
#!/usr/bin/env python#-*-coding:utf-8-*-def fun5 (): Return def fun4 (): ret = Fun5 (): #return none# because func 4 By default there is no return value, that is, the return value is none, so 13 stops at this step to continue delivery, Def FUN3 (): Fun4 () #return None #此处func3获取func4的返回值Nonedef fun2 (): Fun3 () #return None #此处func2获取func3的返回值None def fun1 (): fun2 () #return none# here func1 get Func2 return value None #resut =func1 (), R Esult equals func1 return value none, so output Noneresult =fun1 () print result
To implement the code return value, you only need to make the following modifications:
#!/usr/bin/env python# -*- coding:utf-8 -*-def fun5 (): return 13 def fun4 (): ret = fun5 (): #return none return ret# Because the FUNC4 default does not return a value, that is, the return value is None, so 13 arrived at this step stopped the continuation of delivery, DEF FUN3 (): return fun4 () #return None #此处func3获取func4的返回值Nonedef fun2 (): return fun3 () #return None #此处func2获取func3的返回值None def  FUN1 (): retrun fun2 () #return none# Here func1 gets the return value of Func2 none #resut =func1 (), that is, result equals the return value of func1 none, so output noneresult =fun1 () print result
Explanation: Func5 has a return value of 13, in the FUNC4 we have assigned the variable ret = FUNC5 (), so ret=13, and then add a return ret, so that the Func4 return value that return RET returns the result, that is, 13, and func3 down with the FUNC4, so only need to func3 under the Func4 () to return Func4 () can be implemented FUNC3 has a return value, also under Func2 adjustment fun3 () is return func3 () is FUNC2 with return value, The final func1 will change Func2 () to return Func2 () so that func1 has a return value, the essence of the process is to say that the return 13 in the FUNC5 is passed through the final pass to func1, thereby outputting. So the result of running the above code is:
13
So go back and look at the code for the first time:
def func (ARG1,ARG2): if arg1 = = 0:pass Arg3 = arg1 + arg2 if arg3 > 10:return arg3 return func (ARG2,ARG3) re Sult = func (0,1) print result
The output is:
13
Explanation: Put 0, 1 in the function to perform:
ARG1=0,ARG2=1,ARG3 = 0 + 1=1,
When executed to the IF statement, it will not continue because of the arg3<10 at this time, so execute the following func (ARG2,ARG3) statement at this time func (ARG2,ARG3) is func (a), arg2=1,arg3=1, that is, func , ARG3=1+1=2,ARG3<10,
Continue execution
Func (ARG2,ARG3), arg2=1,arg3=2, i.e. func (arg1=1,arg2=2), arg3=1+2=3,arg3<10,
Continue execution
Func (ARG2,ARG3), arg2=2,arg3=3, i.e. func (arg1=2,arg2=3), arg3=arg1+arg2=2+3=5,arg3<10,
Continue execution
Func (ARG2,ARG3), arg2=3,arg3=5, i.e. func (arg1=2,arg2=5), Arg3 =arg1+arg2=3+5=8,arg3<10,
Continue execution
Func (ARG2,ARG3), arg2=5,arg3=13, i.e. func (arg1=5,arg2=13), arg3=arg1+arg2=5+8=13,arg3>10,
Now return ARG3, that is, after the 13,IF statement is established, run return ARG3, at this time only in the arg3>10 as the result of this call return, but we require that the results of this return to return to the last call, can be normal output otherwise the result is displayed as none, Here you can refer to the above example of the FUN1.....FUN5 function to understand, so in the end to have to assign the result to the last call, here you can change the code to see the analysis:
def func (ARG1,ARG2): if arg1 = = 0:pass Arg3 = arg1 +arg2 #print arg3 if Arg3 >10:return arg 3 Print arg3 print "###########" return func (ARG2,ARG3) result =func (0,1) print result
Operation Result:
1########## #2 ########## #3 ########## #5 ########## #8 ########## #13
If you change return func (ARG2,ARG3) to Func (ARG2,ARG3), then look at the results of the operation:
def func (ARG1,ARG2): if arg1 = = 0:pass Arg3 = arg1 +arg2 #print arg3 if Arg3 >10:return arg 3 Print arg3 print "###########" func (ARG2,ARG3) result =func (0,1) print result
Operation Result:
1########## #2 ########## #3 ########## #5 ########## #8 ########## #None
From here can be seen when the arg3<10, can be output of the ARG3, and when arg3>10 is not arg3 value output, the root cause is not to return the ARG3 to the Func function body, that is, the value of Arg3 stay in return ARG3 here, Without further passing, because it is a recursive call, the return value of this time should be returned to the last called function body itself, namely Func (ARG), so the return value at this time is stuck in return arg3 here, so the return value is the default value (that is, none), so here should be in Func (Arg2, ARG3) Add return in front of
def func (ARG1,ARG2): if arg1 = = 0:pass Arg3 = arg1 +arg2 #print arg3 if Arg3 >10:return arg 3 Print arg3 print "###########" return func (ARG2,ARG3) result =func (0,1) print result
This article from "Flat Light is true" blog, please be sure to keep this source http://rookies.blog.51cto.com/10837891/1729259
Python's Fibonacci sequence