Adding a Simple Main Function in Python



  • If you come from a background in Java you might wish that you could have a "main" function in your Python programs. Here is a simple pattern for doing so. You can then have main() anywhere that you want, top of your program listing, middle, last... does not matter. All that matters is that the final line of your program is the "kickoff" that sends you into main()...

    def main():
        pass
    
    main()


  • I do the same technique when I am working in BASH. It just makes coding cleaner and easier. I find that even for small programs and quick scripts that it is a good habit to get into.


Log in to reply