在python中执行嵌套函数的方法
1.执行不带参数的嵌套函数
def outer_func():
x=1
def inner_func():
result=x+1
print(result)
return inner_func
f1=outer_func()
f1()
输出结果为:
2
2.执行带参数的嵌套函数
1)外函数带有参数
def outer_func(x):
def inner_func():
print(x+1)
return inner_func
f1 = outer_func(1) # 返回inner函数对象
f1()
输出结果为:
2
2)内函数带有参数
def outer_func():
def inner_func(x):
print(x+1)
return inner_func
f1 = outer_func() # 返回inner函数对象
f1(10)
输出结果为:
11
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python嵌套函数怎么调用