主要内容:

小目标:理解闭包

主要内容:闭包原理,闭包应用

如果看完这篇文章,你还是弄不明白闭包;

你来找我,我保证不打你,我给你发100的大红包。

1. 闭包

闭包:函数内部定义函数,内部函数使用外部变量

闭包要点:

函数内部定义函数

内部函数引用外部变量

函数返回值为内置函数

闭包场景:代码封装复用,装饰器基础

2. 案例:将数字字符串转成N进制:

例如:定义两个函数实现10进制与16进制转换

基本代码实现:

def strToHex(s):
return int(s, 16)
def strToInt(s):
return int(s, 10)
print(strToHex('10'))
print(strToInt('20'))

结果:16,20

问题:如果字符串中,有空白字符,如何处理?

代码实现:

def strToHex(s):
s = s.strip()
return int(s, 16)
def strToInt(s):
s = s.strip()
return int(s, 10)
print(strToHex(' 10'))
print(strToInt('20 '))

结果:16.20

如果还有其他需求,我们需要改多次,换个思路。

3. 闭包实现

def strToN(n):
def func(s):
s = s.strip()
return int(s, n)
return func
strToInt = strToN(10)
strToHex = strToN(16)
print(strToInt(' 10 '))
print(strToInt(' 16 '))

分析:

1.strToN(n):返回内置函数func;

2.strToInt指向func函数;

3.内置函数func中的n是外部变量,为10,16等值;

4.strToInt调用就是对func调用

4 一个问题:func函数中的n?

n对于strToN是局部变量,当strToN调用结束后,理论上就会被释放;

n对于func是外部变量,strToInt指向func函数,所以func函数不会释放,n就被作为外部半两存储到了func中

来验证:

def strToN(n):
def func(s):
s = s.strip()
print('in func locals():',locals())
return int(s, n)
return func
strToInt = strToN(10)
strToHex = strToN(16)
print(strToInt(' 10 '))

结果:

in func locals(): {'s': '10', 'n': 10}
10

5. closure属性

函数有一个属性:closure,用于存放外置变量

def strToN(n):
def func(s):
s = s.strip()
#地址,16进制表示
print('id(n):0x%X'%id(n))
print('in func locals():',locals())
return int(s, n)
return func
strToInt = strToN(10)
print(strToInt(' 10 '))
print(strToInt.__closure__)

结果:

id(n):0x7FFE9CB6A2B0
in func locals(): {'s': '10', 'n': 10}
10
(,)

可以看到:id(n)与strToInt.closure相同:0x7FFE9CB6A2B0

总结:

闭包要点:

1.函数内部定义函数

2.函数返回值为函数

3.内置函数引用外部变量