python中的内嵌函数

 

python中允许在函数内定义另一个函数,这种函数称为内嵌函数或者内部函数。

1、

>>> def a():         ## 外层函数
    print("hello world!")
    def b():         ## 内层函数
        print("xxxxxxx!")
    return b()

>>> a()
hello world!
xxxxxxx!
>>> 

 

2、python中内层函数可以引用外层函数的局部变量

>>> def a():
    x = 100
    def b():
        print(x * 5)
    return b()

>>> a()
500

 

3、python中函数内部可以引用全局变量

>>> x = 500
>>> def a():
    print(x + 30)

    
>>> a()
530

 

posted @ 2021-03-05 17:04  小鲨鱼2018  阅读(177)  评论(0编辑  收藏  举报