ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Adding a Simple Main Function in Python

    Developer Discussion
    python python 3
    1
    2
    1.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      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()
      
      1 Reply Last reply Reply Quote 0
      • scottalanmillerS
        scottalanmiller
        last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post