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
<mailto:python-ideas@python.org>
To unsubscribe send an email to python-ideas-le...@python.org
<mailto:python-ideas-le...@python.org>
https://mail.python.org/mailman3/lists/python-ideas.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/
<https://mail.python.org/archives/list/python-ideas@python.org/message/FKQS2NEI5RQMTX53N77KQQDFZ6HZONXU/>
Code of Conduct: http://python.org/psf/codeofconduct/
<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/