For sure. I think there can be room for improvement in modules though. On Fri, 2021-10-01 at 20:43 +0100, Thomas Grainger wrote: > FYI you can already use package/__main__.py which is runnable with > `python -m package` and you don't need the `if __name__ == > "__main__":` > https://docs.python.org/3.10/library/__main__.html#main-py-in-python-packages > > On Fri, 1 Oct 2021, 20:39 Paul Bryan, <pbr...@anode.ca> 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/4QQRV7NBGQK63OUXEQI7QZSE5L2VGUHY/ Code of Conduct: http://python.org/psf/codeofconduct/