Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
061cb3b0
Commit
061cb3b0
authored
Apr 14, 2014
by
R David Murray
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#15104: improve the discussion of __main__.
Patch by Sam Lucidi.
parent
1343b25f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
5 deletions
+11
-5
Doc/library/__main__.rst
Doc/library/__main__.rst
+11
-5
No files found.
Doc/library/__main__.rst
View file @
061cb3b0
...
...
@@ -5,13 +5,19 @@
.. module:: __main__
:synopsis: The environment where the top-level script is run.
'__main__' is the name of the scope in which top-level code executes.
A module's __name__ is set equal to '__main__' when read from
standard input, a script, or from an interactive prompt.
This module represents the (otherwise anonymous) scope in which the
interpreter's main program executes --- commands read either from standard
input, from a script file, or from an interactive prompt. It is this
environment in which the idiomatic "conditional script" stanza causes a script
to run::
A module can discover whether or not it is running in the main scope by
checking its own __name__, which allows a common idiom for conditionally
executing code in a module when it is run as a script or with `python
-m` but not when it is imported:
if __name__ == "__main__":
# execute only if run as a script
main()
For a package, the same effect can be achieved by including a
__main__.py module, the contents of which will be executed when the
module is run with -m.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment