Re: Python __main__ function

While is possible to use `if __name__ == '__main__':` several times in the same script, your proposed magic function `def __main__()` cannot be redefined. Not to speak about lexical scope differences between one approach and the other. So your proposal is reducing features instead of expanding them. Jose Veiga

So your proposal is reducing features instead of expanding them. Surely, the `__name__` variable would remain, so if people needed a more powerful way of doing it they could use that? But then, we introduce multiple ways of doing the same thing...

Artemis wrote:
The OP is proposing as a possibility: "we could require user to have only one if __name__ == '__main__':". In that case, functionality will be reduced, won't it? In any case, the proposal has been discussed and rejected in the past (PEP 299 https://www.python.org/dev/peps/pep-0299/) as Paul Sokolovsky has pointed out.

On Thu, May 28, 2020, 12:17 PM <jdveiga@gmail.com> wrote:
I don't support the proposal. However, I've also never written a script with multiple 'if __name__ == __main__' lines, and I think if I saw one I'd complain in code review. It's hard for me to imagine how multiple entry point to a script can be a good thing.

On Fri, May 29, 2020 at 2:32 AM David Mertz <mertz@gnosis.cx> wrote:
There aren't multiple entry points, though. There would be multiple blocks of code that are skipped if the module is imported, but executed if it's run as a script. Remember, Python code is NOT declarative. That 'def' statement is an actual executable bit of code. ChrisA

On Thu, May 28, 2020, 3:06 PM Chris Angelico <rosuav@gmail.com> wrote:
I take back my comment. I think I may have used the multiple __main__ blocks occasionally, for reasons Chris notes. Like I want something configured BEFORE a function is defined (maybe a global that only makes sense when run as script). However, I haven't done it for a long while. I think when I was younger I didn't value encapsulation sufficiently. That said, I don't want the capability to change. There are lots of things I don't do stylistically, but I don't want striken from the language.

I'm going to note here that it is perfectly reasonable to use Python as a "scripting language" -- to, you know, write scripts. And when I'm writing scripts, I make heavy use of the global namespace :-) Granted, if it's really a quick and dirty script, I'll not bother with if __name__ == "__main__" at all, but there is a middle ground, where I do. There's no need to restrict Python to be more structured in this manner. -CHB On Thu, May 28, 2020 at 12:52 PM David Mertz <mertz@gnosis.cx> wrote:
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

So your proposal is reducing features instead of expanding them. Surely, the `__name__` variable would remain, so if people needed a more powerful way of doing it they could use that? But then, we introduce multiple ways of doing the same thing...

Artemis wrote:
The OP is proposing as a possibility: "we could require user to have only one if __name__ == '__main__':". In that case, functionality will be reduced, won't it? In any case, the proposal has been discussed and rejected in the past (PEP 299 https://www.python.org/dev/peps/pep-0299/) as Paul Sokolovsky has pointed out.

On Thu, May 28, 2020, 12:17 PM <jdveiga@gmail.com> wrote:
I don't support the proposal. However, I've also never written a script with multiple 'if __name__ == __main__' lines, and I think if I saw one I'd complain in code review. It's hard for me to imagine how multiple entry point to a script can be a good thing.

On Fri, May 29, 2020 at 2:32 AM David Mertz <mertz@gnosis.cx> wrote:
There aren't multiple entry points, though. There would be multiple blocks of code that are skipped if the module is imported, but executed if it's run as a script. Remember, Python code is NOT declarative. That 'def' statement is an actual executable bit of code. ChrisA

On Thu, May 28, 2020, 3:06 PM Chris Angelico <rosuav@gmail.com> wrote:
I take back my comment. I think I may have used the multiple __main__ blocks occasionally, for reasons Chris notes. Like I want something configured BEFORE a function is defined (maybe a global that only makes sense when run as script). However, I haven't done it for a long while. I think when I was younger I didn't value encapsulation sufficiently. That said, I don't want the capability to change. There are lots of things I don't do stylistically, but I don't want striken from the language.

I'm going to note here that it is perfectly reasonable to use Python as a "scripting language" -- to, you know, write scripts. And when I'm writing scripts, I make heavy use of the global namespace :-) Granted, if it's really a quick and dirty script, I'll not bother with if __name__ == "__main__" at all, but there is a middle ground, where I do. There's no need to restrict Python to be more structured in this manner. -CHB On Thu, May 28, 2020 at 12:52 PM David Mertz <mertz@gnosis.cx> wrote:
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
participants (6)
-
Artemis
-
Chris Angelico
-
Christopher Barker
-
David Mertz
-
jdveiga@gmail.com
-
Jose Veiga