栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python中的迟绑定机制

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号