python中不能在外层函数以外调用内层函数

 

1、

内嵌函数的内层函数的作用域在外层函数之内,不能在外层函数外调用内层函数。

>>> def a():              ## 外层函数a(),内层函数b().
    print("hello world!")
    def b():
        print("good morning!")
    return b()

>>> a()
hello world!
good morning!
>>> b()
Traceback (most recent call last):
  File "<pyshell#336>", line 1, in <module>
    b()
NameError: name 'b' is not defined

 

posted @ 2021-03-06 16:25  小鲨鱼2018  阅读(600)  评论(0编辑  收藏  举报