Python file structure

Chris Angelico rosuav at gmail.com
Tue May 12 16:07:31 EDT 2015


On Wed, May 13, 2015 at 5:54 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> Also, I like to put command-line parsing inside the main function and
> make that its *only* responsibility. The main function then calls the
> real entry point of my script, which will be something more
> specifically named. This also has the advantage that if some other
> module needs to invoke my script, all it has to do is call the entry
> point function which will be named something more suitable than
> "main".

That often makes sense, but sometimes doesn't. When it doesn't, you
can usually tell because your main function looks something like this:

def main():
    do_real_work(*sys.argv)
if __name__=="__main__": main()

A one-line function that's called from one place? In-line it.

if __name__ == "__main__":
    do_real_work(*sys.argv)

ChrisA



More information about the Python-list mailing list