On Thu, 12 Nov 2020 07:56:17 -0000 "Matt Wozniski" email@example.com wrote:
Currently, the simplest and most idiomatic way to check whether a module was run as a script rather than imported is:
if __name__ == "__main__":
People generally learn this by rote memorization, because users often want the ability to add testing code or command line interfaces to their modules before they understand enough about Python's data model to have any idea why this works. Understanding what's actually happening requires you to know that:
- the script you ask Python to run is technically a module,
- every module has a unique name assigned to it,
- a module's `__name__` global stores this unique import name,
- and "__main__" is a magic name for the initial script's module.
But that's absolutely great! A novice can first memorize the 'if __name__ == "__main__":' idiom, but it will plant seed for them to explore Python module and module loading systems. Such ways of learning is what makes Python great.
It would behave as though
__main__ = (__name__ == "__main__")
If anything, that conflicts with other proposed usages for __main__, e.g. https://firstname.lastname@example.org/thread/FBT5BT7...
Having a function __main__() is definitely more useful than an alias for trivial comparison op.