程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python中的遲綁定機制

編輯:Python

獲取更多python小知識,歡迎訂閱本專欄

在函數裡嵌套函數,嵌套函數如果使用到了外面的變量,則使用的是外面函數棧中的變量,相當於是淺復制一份給內函數,直到外函數執行結束才會復制一份給內函數

def fun_list():
fun_list=[]
for i in range(1,10):
print('the id of i is:',id(i))
def fun():
''' 定義用到i,i是fun_list2()內存中的i,改變了i的值函數打印的也會變 '''
print('I have got number %d, '%i,'its id is:',id(i))
fun()
print('a new fun() is defined')
fun_list.append(fun)
''' 函數執行結束時,才會把i復制傳遞給之前定義的各個fun() '''
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()在for之前就定義了,但是此是它裡面使用的i是func()的棧裡面的i,i變了打印的內容就會變,我們運行一下

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):
''' 解決方案:在外面再嵌套一層函數,把內層函數用到的外層參數傳給它,然後在裡面定義內函數,把內層函數作為返回值,調用這個函數來定義內層函數 '''
def fun():
''' 定義用到i,i是fun_list2()內存中的i,改變了i的值函數打印的也會變 '''
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執行結束時,必須把i深復制給它的內層函數fun() '''
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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved