python learning function returns

When learning function's return, but when calling the function will be run has not understood, the next day to understand some perspective when recording it.

Look at the code

>>> def fun1(n):
...   def fun2(x):
...     return pow(x,n)
...   return fun2
...
>>> pow2=fun1(2)
>>> pow2
<function fun1.<locals>.fun2 at 0x7f8bdfe2e378>
>>> pow2(9)
81

See the definition of the function is a function fun2 func1 () as defined under () is returned when the operation when the func1 (2) assigned to the address pow2 pow2 (9) is only running fun1 () returns the definition and i.e., a function fun2 (), so fun2 (9,2).

 

There is a reference to returns for external scope (but not global scope) variable function in an internal function, the function is called internal closure (closure). Which uses a loop

DEF COUNT (): 
    FS = []
     for I in Range (l, 4 ):
         DEF f ():
             return I * I
         # the address of the function f is added to the list FS 
        fs.append (f)
     # returns containing three list function f addresses 
    return FS 
 
Print ( ' COUNT function returns a list, which respectively function f address ' , COUNT ()) 
F1, F2, F3 = COUNT ()
 Print (F1, ' F1 execution result ' , F1 ( ))
 Print (F2, ' F2 execution result ' , F2 ())
 Print(F3, ' F3 execution result ' , F3 ())
The count function returns a list, which respectively address the function f [<function count. <locals> .f at 0x7fc5d30cac80>, <function count. <locals> .f at 0x7fc5d30cad08>, <function count. <locals> .f at 0x7fc5d30cad90 > ]
 <function COUNT. <about locals> .F AT 0x7fc5d30cac80> F1 execution result. 9 
<function COUNT. <about locals> .F AT 0x7fc5d30cad08> F2 execution result. 9 
<function COUNT. <about locals> .F AT 0x7fc5d30cad90> F3 performed As a result 9

The results are all 9 instead of the desired 1,4,9

In this function the local variable i is a function of the count

When i = 1, i points to 1, the return address of the function results in the first position of fs.

When i = 2, i in turn points to 2, the function returns the address to the second position fs.

When i = 3, i points to 3, then the function returns the address in the third position of fs.

So when the calling function display, i have a 3, so the result is the same.

 

DEF COUNT (): 
    FS = []
     for I in Range (l, 4 ):
         DEF F (J):
             DEF G ():
                 return J * J
             return G 
        fs.append (F (I)) 
    # returns containing a list of three functions f addresses 
    return FS 
 
Print ( " COUNT function returns a list, which respectively function f address ' , COUNT ()) 
F1, F2, F3 = COUNT ()
 Print (F1, ' F1 execution result ' , F1 ())
 Print (F2, ' F2 execution result is' , F2 ())
 Print (F3, ' F3 execution result ' , F3 ())
The count function returns a list, each list of address for the function f [<function count. <locals> .f. <locals> .g at 0x7ff488311d08>, <function count. <locals> .f. <locals> .g at 0x7ff488311c80> , <function COUNT. <about locals> .F. <about locals> .g AT 0x7ff488311d90> ]
 <function COUNT. <about locals> .F. <about locals> .g AT 0x7ff488311c80> F1 execution result. 1 
<function COUNT. <about locals> .f. <locals> .g at 0x7ff488311d08 > f2 execution result. 4 
<function COUNT. <about locals> .F. <about locals> .g AT 0x7ff488311d90> F3 execution result is 9

This amendment has been after the desired results.

Code F (j) i is the value passed to the loop j, i is returned when the function is not the point in the cycle i, but its value will be passed and point j j.

 

Guess you like

Origin www.cnblogs.com/miaorn/p/11622355.html