Thanks for finding that. While I don't feel strongly one way or the other, I do think the discussion is worthwhile.
As I understand, the arguments for: - let's get rid of boilerplate, that many (esp. beginners) may not understand - you could add command line arguments and return values in a more natural way As I understand, the arguments against: - it's just 2 lines of code; this isn't a big problem being solved - boilerplate serves to teach a fundamental concept about the loading and execution of Python modules - there may not be a clean way to maintain compatibility with previous versions of Python Paul On Sat, 2021-10-02 at 12:21 -0400, Eric V. Smith wrote: > See the rejected PEP 299 for a previous version of this idea: > https://www.python.org/dev/peps/pep-0299/ > > And a discussion on python-dev: > https://mail.python.org/pipermail/python-dev/2006-March/062951.html > > Eric > > On 10/1/2021 3:38 PM, Paul Bryan wrote: > > > > > How about the following? > > > > def __main__(): > > ... > > Behavior: > > > > 1. Load module as normal. > > 2. If __name__ is "__main__" or module is named in python -m, call > > __main__ function. > > > > Paul > > > > > > On Fri, 2021-10-01 at 15:35 -0400, Jonathan Crall wrote: > > > > > I was curious if / what sort of proposals have been considered > > > for simplifying the pattern: > > > > > > ``` > > > def main(): > > > ... > > > > > > if __name__ == "__main__": > > > main() > > > ``` > > > > > > I imagine this topic must have come up before, so I'd be > > > interested in any relevant history. > > > > > > But unless I'm missing something, it seems like adding some > > > easier alternative to this cumbersome entrypoint syntax would be > > > worth considering. > > > > > > My motivation for writing this suggestion is in an attempt to > > > stop a common anti-pattern, where instead of defining a `main` > > > function (or a function by any other name) an simply calling that > > > by adding the above two lines, a lot of Python users I work with > > > will just start dumping their logic into the global scope of the > > > module. > > > > > > Needless to say, this can have consequences. If there was some > > > default builtin, let's call it `__main__` for now (open to > > > suggestions), that took a function as an argument and > > > conditionally executed it if `__name__ == "__main__"` in the > > > caller's scope, that would allow us to simplify the above > > > boilerplate to a single line with no extra indentation: > > > > > > ``` > > > def main(): > > > ... > > > > > > __main__(main) > > > ``` > > > > > > In addition to being simpler, it would allow users to avoid the > > > trap of adding logic that impacts the global scope. It would also > > > save me some keystrokes, which I'm always grateful for. > > > > > > Furthermore, it could be used as a decorator (and the use-case > > > wouldn't be unreasonable!), and we all know how much new Python > > > users love decorators when they find out about them. > > > > > > ``` > > > @__main__ > > > def main(): > > > ... > > > ``` > > > > > > Maybe having such a builtin would discourage globals and help new > > > users get the use-decorators-everywhere bug out of their system. > > > > > > -- > > > -Dr. Jon Crall (him) > > > _______________________________________________ > > > Python-ideas mailing list -- python-ideas@python.org > > > To unsubscribe send an email to python-ideas-le...@python.org > > > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > > > Message archived at > > > https://mail.python.org/archives/list/python-ideas@python.org/message/FKQS2NEI5RQMTX53N77KQQDFZ6HZONXU/ > > > Code of Conduct: http://python.org/psf/codeofconduct/ > > > > > > > > _______________________________________________ > > Python-ideas mailing list -- python-ideas@python.org > > To unsubscribe send an email to python-ideas-le...@python.org > > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > > Message archived at > > https://mail.python.org/archives/list/python-ideas@python.org/message/RNB5YYTGQIV4CRPUZEZTADKG7WJ4YY3B/ > > Code of Conduct: http://python.org/psf/codeofconduct/ > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/3X4X65F4UA2SZP5VMK75DGSLAAT3JJ2C/ > Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/BLH6XX5M4RC25VPREEVTMRKNPTBNHXJ5/ Code of Conduct: http://python.org/psf/codeofconduct/