python笔记 内嵌函数

1.访问的两种形式

A

>>> def funOUT():
    def funIN():
        print('in now')
    return funIN()

>>> funOUT()
in now

B

>>> def funOUT():
    def funIN():
        print('in now')
    return funIN

>>> funOUT()()
in now

2.

  1. def funX():
  2.     x = 5
  3.     def funY():
  4.         nonlocal x
  5.         x += 1
  6.         return x
  7.     return funY
  8.  
  9. a = funX()
  10. print(a()) 
  11. print(a())
  12. print(a())

6
7
8

猜你喜欢

转载自blog.csdn.net/weixin_41948344/article/details/81271018