python函数嵌套如何执行

1248
2021/2/18 10:25:03
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

python函数嵌套如何执行

在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如何找出列表中最小的元素