function函数

 

  1. 内层函数没有引用外层函数局部变量,dir()是不会显示的
    def b(a):
        def p():
            print('function p dir ->',dir())
        p()
        print('function b dir ->',dir())
    b(2)

     

     

    def b(a):
        def p():
            nonlocal a
            print('function p dir ->',dir())
        p()
        print('function b dir ->',dir())
    b(2)

     

     

    def b(a):
        def p():
            print('function p dir ->',dir())
            return a
        p()
        print('function b dir ->',dir())
    b(2)

     

     

  2. 要想调用直接调用内层函数,必须先调用外层函数,将内层函数返回,用变量保存
    def b():
        def p():
            print('pppp')
        return p
    v=b()
    v()
    print(v.__name__,v)

     

     

    indicator=0
    def b():
        global indicator
        def p1():
            print('11111111')
    
        def p2():
            print('2222222222')
    
        def p3():
            print('3333333333333')
    
        def p4():
            print('4444444444444444444')
    
        if indicator==0:
            return None
        elif indicator==1:
            return p1
        elif indicator==2:
            return p2
        elif indicator==3:
            return p3
        elif indicator==4:
            return p4
        else:
            raise NotImplementedError('indicator is wrong!')
    
    v=b()
    print(v)
    indicator=1
    v1=b()
    print(v1.__name__,v1)
    indicator=2
    v2=b()
    print(v2.__name__,v2)

     

     

posted @ 2020-11-11 14:50  ascertain  阅读(149)  评论(0编辑  收藏  举报