On Fri, May 29, 2020 at 5:54 AM email@example.com wrote:
-----Original Message----- From: Chris Angelico firstname.lastname@example.org
People can already put all their main logic into a function. If you want
test your main function, that's the best way to do it. The trouble is, how much goes into main() and how much into if __name__ == '__main__'? For example: Does your main function accept a list of arguments, or does it look in sys.argv? Neither answer is wrong. And that means the best way is to NOT force everyone across all of Python to choose the same way - let people write their code their way.
People write main entry points that are not exactly this?
If __name__ == '__main__': sys.exit(main(sys.argv[1:]))
Of course they do! Much more common in my code is this:
if __name__ == "__main__": main()
or some variation that fetches sys.argv but doesn't call sys.exit. And quite a number of my projects include a global try/except around main() to log errors to a file as well as to stderr (due to where they're used), or process their args individually (for arg in sys.argv[1:]: do_stuff(arg)), or some other variation. It's most certainly NOT always exactly what you show there.
And that's the problem. If it's part of the language definition, it will always have *exactly* the same semantics. As an idiom, it can be varied slightly to suit the situation, but as a language feature, it's rigid.