Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 061cb3b

Browse files
committed
#15104: improve the discussion of __main__.
Patch by Sam Lucidi.
1 parent 1343b25 commit 061cb3b

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

Doc/library/__main__.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
.. module:: __main__
66
:synopsis: The environment where the top-level script is run.
77

8+
'__main__' is the name of the scope in which top-level code executes.
9+
A module's __name__ is set equal to '__main__' when read from
10+
standard input, a script, or from an interactive prompt.
811

9-
This module represents the (otherwise anonymous) scope in which the
10-
interpreter's main program executes --- commands read either from standard
11-
input, from a script file, or from an interactive prompt. It is this
12-
environment in which the idiomatic "conditional script" stanza causes a script
13-
to run::
12+
A module can discover whether or not it is running in the main scope by
13+
checking its own __name__, which allows a common idiom for conditionally
14+
executing code in a module when it is run as a script or with `python
15+
-m` but not when it is imported:
1416

1517
if __name__ == "__main__":
18+
# execute only if run as a script
1619
main()
1720

21+
For a package, the same effect can be achieved by including a
22+
__main__.py module, the contents of which will be executed when the
23+
module is run with -m.

0 commit comments

Comments
 (0)