gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
20KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="9. Top-level components" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/reference/toplevel_components.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="The Python interpreter can get its input from a number of sources: from a script passed to it as standard input or as program argument, typed in interactively, from a module source file, etc. This ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="The Python interpreter can get its input from a number of sources: from a script passed to it as standard input or as program argument, typed in interactively, from a module source file, etc. This ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>9. Top-level components &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="10. Full Grammar specification" href="grammar.html" />
  33. <link rel="prev" title="8. Compound statements" href="compound_stmts.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/reference/toplevel_components.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">9. Top-level components</a><ul>
  86. <li><a class="reference internal" href="#complete-python-programs">9.1. Complete Python programs</a></li>
  87. <li><a class="reference internal" href="#file-input">9.2. File input</a></li>
  88. <li><a class="reference internal" href="#interactive-input">9.3. Interactive input</a></li>
  89. <li><a class="reference internal" href="#expression-input">9.4. Expression input</a></li>
  90. </ul>
  91. </li>
  92. </ul>
  93. </div>
  94. <div>
  95. <h4>Previous topic</h4>
  96. <p class="topless"><a href="compound_stmts.html"
  97. title="previous chapter"><span class="section-number">8. </span>Compound statements</a></p>
  98. </div>
  99. <div>
  100. <h4>Next topic</h4>
  101. <p class="topless"><a href="grammar.html"
  102. title="next chapter"><span class="section-number">10. </span>Full Grammar specification</a></p>
  103. </div>
  104. <div role="note" aria-label="source link">
  105. <h3>This Page</h3>
  106. <ul class="this-page-menu">
  107. <li><a href="../bugs.html">Report a Bug</a></li>
  108. <li>
  109. <a href="https://github.com/python/cpython/blob/main/Doc/reference/toplevel_components.rst"
  110. rel="nofollow">Show Source
  111. </a>
  112. </li>
  113. </ul>
  114. </div>
  115. </nav>
  116. </div>
  117. </div>
  118. <div class="related" role="navigation" aria-label="related navigation">
  119. <h3>Navigation</h3>
  120. <ul>
  121. <li class="right" style="margin-right: 10px">
  122. <a href="../genindex.html" title="General Index"
  123. accesskey="I">index</a></li>
  124. <li class="right" >
  125. <a href="../py-modindex.html" title="Python Module Index"
  126. >modules</a> |</li>
  127. <li class="right" >
  128. <a href="grammar.html" title="10. Full Grammar specification"
  129. accesskey="N">next</a> |</li>
  130. <li class="right" >
  131. <a href="compound_stmts.html" title="8. Compound statements"
  132. accesskey="P">previous</a> |</li>
  133. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  134. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  135. <li class="switchers">
  136. <div class="language_switcher_placeholder"></div>
  137. <div class="version_switcher_placeholder"></div>
  138. </li>
  139. <li>
  140. </li>
  141. <li id="cpython-language-and-version">
  142. <a href="../index.html">3.12.3 Documentation</a> &#187;
  143. </li>
  144. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Language Reference</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><span class="section-number">9. </span>Top-level components</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="top-level-components">
  171. <span id="top-level"></span><h1><span class="section-number">9. </span>Top-level components<a class="headerlink" href="#top-level-components" title="Link to this heading">¶</a></h1>
  172. <p id="index-0">The Python interpreter can get its input from a number of sources: from a script
  173. passed to it as standard input or as program argument, typed in interactively,
  174. from a module source file, etc. This chapter gives the syntax used in these
  175. cases.</p>
  176. <section id="complete-python-programs">
  177. <span id="programs"></span><h2><span class="section-number">9.1. </span>Complete Python programs<a class="headerlink" href="#complete-python-programs" title="Link to this heading">¶</a></h2>
  178. <p id="index-2"><span id="index-1"></span>While a language specification need not prescribe how the language interpreter
  179. is invoked, it is useful to have a notion of a complete Python program. A
  180. complete Python program is executed in a minimally initialized environment: all
  181. built-in and standard modules are available, but none have been initialized,
  182. except for <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> (various system services), <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> (built-in
  183. functions, exceptions and <code class="docutils literal notranslate"><span class="pre">None</span></code>) and <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a>. The latter is used to
  184. provide the local and global namespace for execution of the complete program.</p>
  185. <p>The syntax for a complete Python program is that for file input, described in
  186. the next section.</p>
  187. <p id="index-3">The interpreter may also be invoked in interactive mode; in this case, it does
  188. not read and execute a complete program but reads and executes one statement
  189. (possibly compound) at a time. The initial environment is identical to that of
  190. a complete program; each statement is executed in the namespace of
  191. <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a>.</p>
  192. <p id="index-4">A complete program can be passed to the interpreter
  193. in three forms: with the <a class="reference internal" href="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> <em>string</em> command line option, as a file
  194. passed as the first command line argument, or as standard input. If the file
  195. or standard input is a tty device, the interpreter enters interactive mode;
  196. otherwise, it executes the file as a complete program.</p>
  197. </section>
  198. <section id="file-input">
  199. <span id="id1"></span><h2><span class="section-number">9.2. </span>File input<a class="headerlink" href="#file-input" title="Link to this heading">¶</a></h2>
  200. <p>All input read from non-interactive files has the same form:</p>
  201. <pre>
  202. <strong id="grammar-token-python-grammar-file_input">file_input</strong> ::= (NEWLINE | <a class="reference internal" href="compound_stmts.html#grammar-token-python-grammar-statement"><code class="xref docutils literal notranslate"><span class="pre">statement</span></code></a>)*
  203. </pre>
  204. <p>This syntax is used in the following situations:</p>
  205. <ul class="simple">
  206. <li><p>when parsing a complete Python program (from a file or from a string);</p></li>
  207. <li><p>when parsing a module;</p></li>
  208. <li><p>when parsing a string passed to the <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> function;</p></li>
  209. </ul>
  210. </section>
  211. <section id="interactive-input">
  212. <span id="interactive"></span><h2><span class="section-number">9.3. </span>Interactive input<a class="headerlink" href="#interactive-input" title="Link to this heading">¶</a></h2>
  213. <p>Input in interactive mode is parsed using the following grammar:</p>
  214. <pre>
  215. <strong id="grammar-token-python-grammar-interactive_input">interactive_input</strong> ::= [<a class="reference internal" href="compound_stmts.html#grammar-token-python-grammar-stmt_list"><code class="xref docutils literal notranslate"><span class="pre">stmt_list</span></code></a>] NEWLINE | <a class="reference internal" href="compound_stmts.html#grammar-token-python-grammar-compound_stmt"><code class="xref docutils literal notranslate"><span class="pre">compound_stmt</span></code></a> NEWLINE
  216. </pre>
  217. <p>Note that a (top-level) compound statement must be followed by a blank line in
  218. interactive mode; this is needed to help the parser detect the end of the input.</p>
  219. </section>
  220. <section id="expression-input">
  221. <span id="id2"></span><h2><span class="section-number">9.4. </span>Expression input<a class="headerlink" href="#expression-input" title="Link to this heading">¶</a></h2>
  222. <p id="index-6"><span id="index-5"></span><a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> is used for expression input. It ignores leading whitespace. The
  223. string argument to <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> must have the following form:</p>
  224. <pre>
  225. <strong id="grammar-token-python-grammar-eval_input">eval_input</strong> ::= <a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression_list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a> NEWLINE*
  226. </pre>
  227. </section>
  228. </section>
  229. <div class="clearer"></div>
  230. </div>
  231. </div>
  232. </div>
  233. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  234. <div class="sphinxsidebarwrapper">
  235. <div>
  236. <h3><a href="../contents.html">Table of Contents</a></h3>
  237. <ul>
  238. <li><a class="reference internal" href="#">9. Top-level components</a><ul>
  239. <li><a class="reference internal" href="#complete-python-programs">9.1. Complete Python programs</a></li>
  240. <li><a class="reference internal" href="#file-input">9.2. File input</a></li>
  241. <li><a class="reference internal" href="#interactive-input">9.3. Interactive input</a></li>
  242. <li><a class="reference internal" href="#expression-input">9.4. Expression input</a></li>
  243. </ul>
  244. </li>
  245. </ul>
  246. </div>
  247. <div>
  248. <h4>Previous topic</h4>
  249. <p class="topless"><a href="compound_stmts.html"
  250. title="previous chapter"><span class="section-number">8. </span>Compound statements</a></p>
  251. </div>
  252. <div>
  253. <h4>Next topic</h4>
  254. <p class="topless"><a href="grammar.html"
  255. title="next chapter"><span class="section-number">10. </span>Full Grammar specification</a></p>
  256. </div>
  257. <div role="note" aria-label="source link">
  258. <h3>This Page</h3>
  259. <ul class="this-page-menu">
  260. <li><a href="../bugs.html">Report a Bug</a></li>
  261. <li>
  262. <a href="https://github.com/python/cpython/blob/main/Doc/reference/toplevel_components.rst"
  263. rel="nofollow">Show Source
  264. </a>
  265. </li>
  266. </ul>
  267. </div>
  268. </div>
  269. <div id="sidebarbutton" title="Collapse sidebar">
  270. <span>«</span>
  271. </div>
  272. </div>
  273. <div class="clearer"></div>
  274. </div>
  275. <div class="related" role="navigation" aria-label="related navigation">
  276. <h3>Navigation</h3>
  277. <ul>
  278. <li class="right" style="margin-right: 10px">
  279. <a href="../genindex.html" title="General Index"
  280. >index</a></li>
  281. <li class="right" >
  282. <a href="../py-modindex.html" title="Python Module Index"
  283. >modules</a> |</li>
  284. <li class="right" >
  285. <a href="grammar.html" title="10. Full Grammar specification"
  286. >next</a> |</li>
  287. <li class="right" >
  288. <a href="compound_stmts.html" title="8. Compound statements"
  289. >previous</a> |</li>
  290. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  291. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  292. <li class="switchers">
  293. <div class="language_switcher_placeholder"></div>
  294. <div class="version_switcher_placeholder"></div>
  295. </li>
  296. <li>
  297. </li>
  298. <li id="cpython-language-and-version">
  299. <a href="../index.html">3.12.3 Documentation</a> &#187;
  300. </li>
  301. <li class="nav-item nav-item-1"><a href="index.html" >The Python Language Reference</a> &#187;</li>
  302. <li class="nav-item nav-item-this"><a href=""><span class="section-number">9. </span>Top-level components</a></li>
  303. <li class="right">
  304. <div class="inline-search" role="search">
  305. <form class="inline-search" action="../search.html" method="get">
  306. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  307. <input type="submit" value="Go" />
  308. </form>
  309. </div>
  310. |
  311. </li>
  312. <li class="right">
  313. <label class="theme-selector-label">
  314. Theme
  315. <select class="theme-selector" oninput="activateTheme(this.value)">
  316. <option value="auto" selected>Auto</option>
  317. <option value="light">Light</option>
  318. <option value="dark">Dark</option>
  319. </select>
  320. </label> |</li>
  321. </ul>
  322. </div>
  323. <div class="footer">
  324. &copy;
  325. <a href="../copyright.html">
  326. Copyright
  327. </a>
  328. 2001-2024, Python Software Foundation.
  329. <br />
  330. This page is licensed under the Python Software Foundation License Version 2.
  331. <br />
  332. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  333. <br />
  334. See <a href="/license.html">History and License</a> for more information.<br />
  335. <br />
  336. The Python Software Foundation is a non-profit corporation.
  337. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  338. <br />
  339. <br />
  340. Last updated on Apr 09, 2024 (13:47 UTC).
  341. <a href="/bugs.html">Found a bug</a>?
  342. <br />
  343. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  344. </div>
  345. </body>
  346. </html>
上海开阖软件有限公司 沪ICP备12045867号-1