On 29/12/19 5:49 PM, Cameron Simpson wrote:
On 29Dec2019 09:49, Chris Angelico <ros...@gmail.com> wrote:
"Define before use" is a broad principle that I try to follow, even
when the code itself doesn't mandate this. This generally means that
"if name is main" is the very last thing in the file, and if there's a
main() function or equivalent, that's usually just before that. Any
metaprogramming goes right at the top; sometimes this is mandated (if
I write a decorator function, it has to be above the functions it's
decorating), but even if it's not, metaprogramming goes before the
mainline.

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!

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

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to