Call a function defined in another function

前端 未结 4 1991
梦谈多话
梦谈多话 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:19

    This is based on eyquem's solution.

    def func1():
        global func2  # put it in global scope
        def func2():
            print("Hello")
    

    Now you can invoke func2 directly.

    But func1() would have to be called before you can call func2() otherwise it will not have been defined yet.

提交回复
热议问题