Call a function defined in another function

前端 未结 4 2002
梦谈多话
梦谈多话 2020-11-28 14:44

Can I call a function nested inside another function from the global scope in python3.2?

def func1():
    def func2():
        print(\"Hello\")
        retur         


        
4条回答
  •  感动是毒
    2020-11-28 15:22

    def func1():
        def func2():
            global fudu
            fudu = func2
            print("Hello")
        func2()
    
    
    func1()
    
    fudu()
    
    print 'fudu' in dir()
    print 'func2' in dir()
    

    result

    Hello
    Hello
    True
    False
    

    Also:

    def func1():
        global func2
        def func2():
            print("Hello")
        func2()
    
    
    func1()
    
    print 'func2' in dir()
    func2()
    

    result

    Hello
    True
    Hello
    

    What's the interest?

提交回复
热议问题