I have a lot of simple scripts that calculate some stuff or so. They consist of just a single module.
Should I write main methods for them and call them with the if __name__
construct, or just dump it all right in there?
What are the advantages of either method?
I always write a main()
function (appropriately named), and put nothing but command-line parsing and a call to main()
in the if __name__ == '__main__'
block. That's because no matter how silly, trivial, or single-purpose I originally expect that script to be, I always end up wanting to call it from another module at some later date.
Either I take the time to make it an importable module today, or spend extra time to refactor it months later when I want to reuse it for something else.
Always.
Every time.
I've stopped fighting it and started writing my code with that expectation from the start.