The ‘if __name__…’ idiom makes one function special. Hm... So if the module has that, will that main() still be called with your proposal? I.e., is __name__ still ‘__main__’?
On Tue, Oct 20, 2020 at 20:32 Michael Smith firstname.lastname@example.org wrote:
On Tue, Oct 20, 2020 at 23:12 Guido van Rossum email@example.com wrote:
On Tue, Oct 20, 2020 at 8:06 PM Michael Smith firstname.lastname@example.org wrote:
On Tue, Oct 20, 2020 at 10:19 PM Guido van Rossum email@example.com wrote:
I think it would be a tad more convincing if there was a way to pass
arguments too (even if just a list of strings). At the very least extra arguments should end up in sys.argv[1:].
Could python -m 'module:thunk' have exactly the same behavior with respect to arguments as `python3.8 -m module` does today?
$ cat bar.py import pprint, sys def thunk(): pprint.pprint(sys.argv) if __name__ == "__main__": thunk() $ python3.8 -m bar -- -1 --two --three=3 ['/Users/michael/bar.py', '--', '-1', '--two', '--three=3']
So then with the same bar.py, `python -m bar:thunk -- -2 --three --four=4` would print `['/Users/michael/bar.py', '--', '-1', '--two', '--three=3']`. I like this better than my previous suggestion to shorthand python -c.
Actually it should print the same except for sys.argv. (We could argue about what sys.argv should be.)
Then again, presumably the function must be specially crafted for this
usage. Why can't you just specially craft a module's main()?
I'm not sure I know what you mean by "specially crafted", other than the function only needs not require any formal parameters. It doesn't need to be special-er than that. It can handle args via sys.argv, as you suggested. Most of the `main` functions I write today are just like that.
Okay, so this is just a way to choose an alternative main() function.
Am I missing something important here? What's special about naming a function "main"? Don't you still have to do something else to invoke it from the cli, such as calling it expressly in the module or pointing console entry points to it?
$ echo 'def main(): print("hi")' > xyz.py $ python -m xyz <no output> $
I'm kind of meh at this point, I'll leave it to the usual crowd. :-)
I appreciate the engagement you've given this already. :)
--Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/