Search code examples
pythonfunctionprototypedefinition

Python Functions AFTER code


Is there a way (without putting the function in a separate file) to define the content of a function after the bulk of my code? Sort of like C where you define prototypes and put the body later on in the file.

Ex;

blah blah blah
functionCall(arg)
blah blah blah

def functionCall(arg):
   blah blah blah

Solution

  • Yes. Instead of

    blah blah blah
    functionCall(arg)
    blah blah blah
    
    def functionCall(arg):
        blah blah blah
    

    Do

    def main():
        blah blah blah
        functionCall(arg)
        blah blah blah
    
    def functionCall(arg):
        blah blah blah
    
    if __name__ == '__main__':
        main()
    

    The if __name__ == '__main__': bit is mostly unrelated to the topic of the question; it just prevents the main from running if this file is imported as a module.