def max(a,b):
return a if a >b else b
def the_max(x,y,z):
c = max(x,y)
return max(c,z)
print(the_max(1,2,3))
#一个简单的嵌套例子
声明全局变量 global
声明上几层中最近那一层中的局部变量 nonlocal
def func():
print(123)
func2 = func
lis = [func,func2]
for i in lis:
i()
#这个例子表明函数名可以作为容器类型的元素
def func():
print(123)
def a(f):
f()
return f
b = a(func)
#函数名可以作为函数的参数,可以作为返回值
函数名是第一类对象,符合:
1.可在运行期间创建
2.可用作函数参数或返回值
3.可存入变量的实体
闭包:嵌套函数,内部函数调用外部函数的变量
def outer():
a = 1
def inner()
print(a)
return inner
inn = outer()
inn()
#闭包最常见的形式