On 12/11/2020 16:37, Paul Sokolovsky wrote:
Hello,
On Thu, 12 Nov 2020 07:56:17 -0000
"Matt Wozniski" <godlyg...@gmail.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:
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.
I find this a curious argument. A bit like saying "Here are your new
shoes; they weigh 10Kg but that's absolutely great because you'll
develop stronger legs wearing them".
Rob Cliffe
_______________________________________________
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/HAUHWXKSKRFIVLKGZUL2BRDSMVZ5AW3V/
Code of Conduct: http://python.org/psf/codeofconduct/