I have also often wondered if there could be something more turn-key for
that bit at the bottom.

Here is a suggestion I'll throw into the ring:

sys.run_main()

It goes at the bottom of the file, and basically just lets the programmer
express what they want to happen. Easy to explain. The appropriate logic is
buried in run_main(), checking if we're in the __main__ situation, and
looking for a main() in the file and running it and probably some corner
cases I haven't thought of. I figure sys is the right module, having argv
and all that. I would prefer to not have the programmer need to resort to
underbar __main()__ type naming to accomplish a thing that is so ordinary.
If there were a syntax for this, visualize it showing up in the first day
of Python class, at the bottom of hello.py or whatever. You want something
easy to explain, not requiring a ton of attention for solving something
that is actually quite routine.

I don't mind making the name 'main' special, but you could have an option
to specify a different function or make it more agnostic about the name in
some other way.

Best,

Nick


On Fri, Oct 1, 2021 at 12:36 PM Jonathan Crall <erote...@gmail.com> 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/KXR6ABMG3CYLSDDDPW6WPZTHTMKCDJTG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to