Declare function at end of file in Python

前端 未结 5 1589
庸人自扰
庸人自扰 2020-11-27 15:23

Is it possible to call a function without first fully defining it? When attempting this I get the error: \"function_name is not defined\". I am coming from a C++ ba

5条回答
  •  粉色の甜心
    2020-11-27 15:32

    If you are willing to be like C++ and use everything inside a functions. you can call the first function from the bottom of the file, like this:

    def main():
        print("I'm in main")
        #calling a although it is in the bottom
        a()
    
    def b():
       print("I'm in b")
    
    def a():
       print("I'm in a")
       b()
    
    main()
    

    That way python is first 'reading' the whole file and just then starting the execution

提交回复
热议问题