温馨提示×

python函数嵌套如何执行

九三
251
2021-02-18 10:25:03
栏目: 编程语言

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

0