高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回。
def hello(name="Fred"):
def hi():
return "now you are in the hi() function"
def bye():
return "now you are in the by() function"
if name == "Fred":
return hi
else:
return bye
a = hello()
print(a)
#outputs: <function hi at 0x7f2143c01500>
#上面清晰地展示了`a`现在指向到hello()函数中的hi()函数
#现在试试这个
print(a())
#outputs: now you are in the hi() function
在 if/else 语句中我们返回 hi和 bye,而不是 hi() 和 bye()。为什么那样?这是因为当你把一对小括号放在后面,这个函数就会执行;然而如果你不放括号在它后面,那它可以被到处传递,并且可以赋值给别的变量而不去执行它。 当我们写下 a = hello(),hello() 会被执行,而由于 name 参数默认是 Fred,所以函数 hi 被返回了。如果我们把语句改为 a = hi(name = “other”),那么 bye函数将被返回。我们还可以打印出 hello()(),这会输出 now you are in the hi() function.
闭包中内函数修改外函数局部变量:
在基本的python语法当中,一个函数可以随意读取全局数据,但是要修改全局数据的时候有两种方法:1 global 声明全局变量 2 全局变量是可变类型数据的时候可以修改
1 在python3中,可以用nonlocal 关键字声明 一个变量, 表示这个变量不是局部变量空间的变量,需要向上一层变量空间找这个变量。
2 在python2中,没有nonlocal这个关键字,我们可以把闭包变量改成可变类型数据进行修改,比如列表。
#利用闭包返回一个计数器函数,每次调用它返回递增整数:
#方法一
def createCounter():
#把闭包变量改成可变类型数据进行修改,比如列表
#可变类型数据进行修改全局变量,list
fs = []
def counter():
fs.append(1)
return len(fs)
return counter
# 测试:
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
print('测试通过!')
else:
print('测试失败!')
#方法二
def createCounter():
i=0
def counter():
#用nonlocal 关键字声明 一个变量, 表示这个变量不是局部变量空间的变量,需要向上一层变量空间找这个变量
nonlocal i
i = i+1
return i
return counter
# 测试:
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
print('测试通过!')
else:
print('测试失败!')