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 <[email protected]>
Sent: 13 November 2020 16:06
To: André Roberge <[email protected]>; Matt Wozniski <[email protected]>
Cc: Python-Ideas <[email protected]>
Subject: [Python-ideas] Re: Global flag for whether a module is __main__

Excellent redirect of original proposal: +10

From: André Roberge <[email protected]<mailto:[email protected]>>
[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 -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/XU4V4RFVV6LPE4CIEB6XPIAJVERIV7IZ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to