python中的内嵌函数

时间:2021-03-06 14:22:06   收藏:0   阅读:0

 

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

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!