On Fri, Nov 13, 2020 at 05:59:29AM -0400, André Roberge wrote:
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.
What if you import the `__main__` module? What does `__imported__` say now, and how do you check for "running as a script" if `__main__` has imported itself -- or some other module has imported it?
Is this `__imported__` variable *instead of* or *as well as* the proposed `__main__`?
Below, I'm going to assume this variable is addition to the proposed `__main__`. If that's not the case, and instead this is just a change of spelling, my comments below are probably irrelevant.
if not __imported__:
would behave the same as the proposed
So now we have three idioms for precisely the same thing. That's not merely three alternative calculation methods, like we might have here:
result = x**2 + 5*x result = x*x + 5*x result = x*(x + 5)
but three special purpose idioms which were intentionally created to mean the same thing. I hope you have a StackOverflow account so you will be around to answer the many questions asking what the difference between them are.
I don't know about you, but I never remember what are the differences between `type(obj)` and `obj.__class__`. So I'm sure people will have the same problem here.
Might I suggest this?
"The `__name__ = ...` idiom is the legacy method. It will never go away because of backwards compatibility, and also because it is useful for a module to check its own name for other purposes.
"The `__main__` variable is there as a shortcut.
"And the `__imported__` variable is there because the Python core developers think that there is a large number of Python users who can't work out how to use `not` to flip a boolean flag."