On 02Jan2020 18:01, DL Neil <pythonl...@danceswithmice.info> wrote:
On 29/12/19 5:49 PM, Cameron Simpson wrote:
For main, i have the opposite habit. If a module has a main() function for command line use I usually want that right up the front:

 #!/usr/bin/env python3
 ....
 import...

 def main(argv=None):
   ... main command line ...

 classes, functions, etc

 if __name__ == '__main__':
   sys.exit(main(sys.argv))

My reasoning here is that I want the main programme obvious up front.

But then I loosely follow "define before use" after that.

OK, I'll bite:

1 the reader (one assumes) starts at the top, then scrolls all the way to the bottom to find if...__main___, but is then directed to search for the def main... which is all the way back up to the top!

If they scrolled, they'd have seen the main() function. I consider it akin to the opening comment/docstring: I want the reader to know pretty immediately that this has a meaningful command line mode.

2 if instead of a main(), that code was under if ... __main__ would that be an equally reasonable and "obvious" place to find it?

Well...

Inline code under the if...__main__ stuff cannot be called as a function; I usually consider the main() function a reusable component. The boilerplate under if...__main__ at the bottom is just the call to it for when the module is used with "python -m". But main itself is standalone. So it won't be under the if...__main__; the only real discussion is where to put it.

Cheers,
Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to