Hello, On Thu, 12 Nov 2020 07:56:17 -0000 "Matt Wozniski" <[email protected]> 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: > > 1. the script you ask Python to run is technically a module, > 2. every module has a unique name assigned to it, > 3. a module's `__name__` global stores this unique import name, > 4. 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. > if __main__: > > It would behave as though > > __main__ = (__name__ == "__main__") If anything, that conflicts with other proposed usages for __main__, e.g. https://mail.python.org/archives/list/[email protected]/thread/FBT5BT7KYFVQCZYVAY6HSSWNKAVCXA5T/ Having a function __main__() is definitely more useful than an alias for trivial comparison op. [] -- Best regards, Paul mailto:[email protected] _______________________________________________ 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/KJ2HQQSUU52NJCQRLJ5F4CY2DJXVGJWB/ Code of Conduct: http://python.org/psf/codeofconduct/
