020函数:内嵌函数和闭包

global 全局变量

>>> def myfun():
	global count
	count = 10
	print(10)

	
>>> myfun()
10
>>> count
10
>>> 

内嵌函数

>>> def fun1():
		print("fun1正在被调用")
		def fun2():
			print("fun2正在被调用")
		fun2()

	
>>> fun1()
fun1正在被调用
fun2正在被调用
>>> 

闭包closure

>>> def funx(x):
	def funy(y):
		return x * y
	return funy

>>> i = funx(8)
>>> i
<function funx.<locals>.funy at 0x0000015C67A6BAF0>
>>> i(5)
40
>>> def fun1():
	x= 5
	def fun2():
		nonlocal x
		x *= x
		return x
	return fun2()

>>> fun1()
25
发布了42 篇原创文章 · 获赞 0 · 访问量 290

猜你喜欢

转载自blog.csdn.net/qq_43169516/article/details/103702081