嵌套函数

这节主要分析两串代码

x=0

def grandpa():

      x=1

      def dad():

            x=2

            def son():

                  x=3

                  print (x)

             son()

        dad()

grandpa()

这其实是一个3个函数的嵌套,首先定义了一个grandpa这个函数,后面的几行是他的函数体,最后一行代表运行函数,然后函数体内又定义了一个新的函数,以此类推。

def foo():

      print('in the foo')

      def bar():

            print('in the bar')

      bar()

foo()

同样是一个函数的嵌套,很容易理解。

原文地址:https://www.cnblogs.com/zaizaiaipython/p/7804475.html