獲取更多python小知識,Welcome to subscribe to this專欄
Nest functions within functions,Nested functions use outer variables,The variables in the outer function stack are used,It is equivalent to a shallow copy to the inner function,A copy of the inner function will not be copied until the outer function is executed
def fun_list():
fun_list=[]
for i in range(1,10):
print('the id of i is:',id(i))
def fun():
''' define to usei,i是fun_list2()內存中的i,改變了iThe value of the function printed will also change '''
print('I have got number %d, '%i,'its id is:',id(i))
fun()
print('a new fun() is defined')
fun_list.append(fun)
''' 函數執行結束時,才會把iCopies passed to each of the previously defined onesfun() '''
return fun_list
fun_list=fun_list()
print('------------------------')
for i in range(len(fun_list)):
fun_list[i]()
運行結果
the id of i is: 1652060816
I have got number 1, its id is: 1652060816
a new fun() is defined
the id of i is: 1652060848
I have got number 2, its id is: 1652060848
a new fun() is defined
the id of i is: 1652060880
I have got number 3, its id is: 1652060880
a new fun() is defined
the id of i is: 1652060912
I have got number 4, its id is: 1652060912
a new fun() is defined
the id of i is: 1652060944
I have got number 5, its id is: 1652060944
a new fun() is defined
the id of i is: 1652060976
I have got number 6, its id is: 1652060976
a new fun() is defined
the id of i is: 1652061008
I have got number 7, its id is: 1652061008
a new fun() is defined
the id of i is: 1652061040
I have got number 8, its id is: 1652061040
a new fun() is defined
the id of i is: 1652061072
I have got number 9, its id is: 1652061072
a new fun() is defined
------------------------
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
I have got number 9, its id is: 1652061072
Process finished with exit code 0
再舉一個例子:
def func():
i=1
print('The number of i is: %d now'%i)
def i_printer():
print('Hello! The number of i is %d'%i)
for i in range(2,10):
print('The number of i is: %d now'%i)
i_printer()
return i_printer
func()
雖然i_printer()在fordefined before,But this is what is used in iti是func()inside the stacki,iChange the printed content will change,我們運行一下
The number of i is: 1 now
i_printer: The number of i is: 2 now
Hello! The number of i is 2
i_printer: The number of i is: 3 now
Hello! The number of i is 3
i_printer: The number of i is: 4 now
Hello! The number of i is 4
i_printer: The number of i is: 5 now
Hello! The number of i is 5
i_printer: The number of i is: 6 now
Hello! The number of i is 6
i_printer: The number of i is: 7 now
Hello! The number of i is 7
i_printer: The number of i is: 8 now
Hello! The number of i is 8
i_printer: The number of i is: 9 now
Hello! The number of i is 9
解決方案
def fun_list():
fun_list=[]
for i in range(1,10):
print('the id of i is:',id(i))
def new_fun(i):
''' 解決方案:Nest another layer of functions outside,Pass it the outer parameters used by the inner function,Then define the inner function inside,Use the inner function as the return value,Call this function to define inner functions '''
def fun():
''' define to usei,i是fun_list2()內存中的i,改變了iThe value of the function printed will also change '''
print('I have got number %d, '%i,'its id is:',id(i))
return fun
print('a new fun() is defined')
fun_list.append(new_fun(i))
''' new_fun執行結束時,必須把iDeep copy to its inner functionfun() '''
return fun_list
fun_list=fun_list()
print('------------------------')
for i in range(len(fun_list)):
fun_list[i]()
the id of i is: 1652060816
a new fun() is defined
the id of i is: 1652060848
a new fun() is defined
the id of i is: 1652060880
a new fun() is defined
the id of i is: 1652060912
a new fun() is defined
the id of i is: 1652060944
a new fun() is defined
the id of i is: 1652060976
a new fun() is defined
the id of i is: 1652061008
a new fun() is defined
the id of i is: 1652061040
a new fun() is defined
the id of i is: 1652061072
a new fun() is defined
------------------------
I have got number 1, its id is: 1652060816
I have got number 2, its id is: 1652060848
I have got number 3, its id is: 1652060880
I have got number 4, its id is: 1652060912
I have got number 5, its id is: 1652060944
I have got number 6, its id is: 1652060976
I have got number 7, its id is: 1652061008
I have got number 8, its id is: 1652061040
I have got number 9, its id is: 1652061072
Process finished with exit code 0