I love the idea of __imported__ but would like to suggest taking it even 
further - why not, rather that a bool for __imported__, have the value of it be 
either None (if __name__ == "__main__") or the __name__ of the module that it 
is being imported from. This could potentially be useful in trying to resolve 
circular imports, imports via importlib or exec, etc. The code need would be 
slightly more complex needing to check the current call stack at the time of 
import but I don't think that it  would be  too much overhead.

Steve Barnes

From: Henshaw, Andy via Python-ideas <python-ideas@python.org>
Sent: 13 November 2020 16:06
To: André Roberge <andre.robe...@gmail.com>; Matt Wozniski <godlyg...@gmail.com>
Cc: Python-Ideas <python-ideas@python.org>
Subject: [Python-ideas] Re: Global flag for whether a module is __main__

Excellent redirect of original proposal: +10

From: André Roberge <andre.robe...@gmail.com<mailto:andre.robe...@gmail.com>>
[snip]

As a goal of making it even more obvious what the (new) idiom mans, I would 
suggest a variable named __imported__ with the opposite value to what is 
proposed.

Thus,

if not __imported__:

would behave the same as the proposed

if __main__:


André
_______________________________________________
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/XU4V4RFVV6LPE4CIEB6XPIAJVERIV7IZ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to