Fwd: Re: Global flag for whether a module is __main__

Sorry, forgot to use "reply to all" ---------- Forwarded message --------- From: André Roberge <andre.roberge@gmail.com> Date: Sat, Nov 14, 2020 at 11:06 AM Subject: Re: [Python-ideas] Re: Global flag for whether a module is __main__ To: Steven D'Aprano <steve@pearwood.info> On Sat, Nov 14, 2020 at 10:45 AM Steven D'Aprano <steve@pearwood.info> wrote:
if __name__ == "__main__": ... The idea behind the name __imported__ (and, I gather, somewhat similar to the original suggestion of __main__ that started this thread) is to reduce such confusion. For what I had in mind, the semantic would be the same as though the following was inserted at the top of the module: __imported__ = True if __name__ == "__main__": __imported__ = False André

The module __main__ is somewhat special. It's there from the very beginning (so far as I can tell). $ python3 -c 'import __main__; print(__main__)' <module '__main__' (built-in)> And without importing __main__. $ python3 -c 'import sys; print(sys.modules["__main__"])' <module '__main__' (built-in)> In light of this, it seems to me that the technical implementation of the proposal amounts to 1. When creating a new module object, set __main__ = False 2. When initializing the built-in __main__ module, set __main__ = True I don't see a technical problem in the implementation of this idea. (I suspect the same rules will also work for C-coded modules.) Whether a good idea or not is another question. -- Jonathan

On Sat, Nov 14, 2020 at 05:22:41PM +0000, Jonathan Fine wrote:
The module __main__ is somewhat special. It's there from the very beginning (so far as I can tell).
https://docs.python.org/3/library/__main__.html [...]
2. When initializing the built-in __main__ module, set __main__ = True
The `__main__` module is not so much a special built-in module as a role given to any old module. The `__main__` module is whatever module gets executed as the main module, if any, otherwise whatever module object gets created to house the top level scope of the Python interpreter. -- Steve

The module __main__ is somewhat special. It's there from the very beginning (so far as I can tell). $ python3 -c 'import __main__; print(__main__)' <module '__main__' (built-in)> And without importing __main__. $ python3 -c 'import sys; print(sys.modules["__main__"])' <module '__main__' (built-in)> In light of this, it seems to me that the technical implementation of the proposal amounts to 1. When creating a new module object, set __main__ = False 2. When initializing the built-in __main__ module, set __main__ = True I don't see a technical problem in the implementation of this idea. (I suspect the same rules will also work for C-coded modules.) Whether a good idea or not is another question. -- Jonathan

On Sat, Nov 14, 2020 at 05:22:41PM +0000, Jonathan Fine wrote:
The module __main__ is somewhat special. It's there from the very beginning (so far as I can tell).
https://docs.python.org/3/library/__main__.html [...]
2. When initializing the built-in __main__ module, set __main__ = True
The `__main__` module is not so much a special built-in module as a role given to any old module. The `__main__` module is whatever module gets executed as the main module, if any, otherwise whatever module object gets created to house the top level scope of the Python interpreter. -- Steve
participants (3)
-
André Roberge
-
Jonathan Fine
-
Steven D'Aprano