gooderp18绿色标准版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

430 lines
21KB

  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="1. Introduction" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/reference/introduction.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="This reference manual describes the Python programming language. It is not intended as a tutorial. While I am trying to be as precise as possible, I chose to use English rather than formal specific..." />
  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="This reference manual describes the Python programming language. It is not intended as a tutorial. While I am trying to be as precise as possible, I chose to use English rather than formal specific..." />
  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>1. Introduction &#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="2. Lexical analysis" href="lexical_analysis.html" />
  33. <link rel="prev" title="The Python Language Reference" href="index.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/reference/introduction.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="#">1. Introduction</a><ul>
  86. <li><a class="reference internal" href="#alternate-implementations">1.1. Alternate Implementations</a></li>
  87. <li><a class="reference internal" href="#notation">1.2. Notation</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="index.html"
  95. title="previous chapter">The Python Language Reference</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="lexical_analysis.html"
  100. title="next chapter"><span class="section-number">2. </span>Lexical analysis</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/reference/introduction.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="lexical_analysis.html" title="2. Lexical analysis"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="index.html" title="The Python Language Reference"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Language Reference</a> &#187;</li>
  143. <li class="nav-item nav-item-this"><a href=""><span class="section-number">1. </span>Introduction</a></li>
  144. <li class="right">
  145. <div class="inline-search" role="search">
  146. <form class="inline-search" action="../search.html" method="get">
  147. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  148. <input type="submit" value="Go" />
  149. </form>
  150. </div>
  151. |
  152. </li>
  153. <li class="right">
  154. <label class="theme-selector-label">
  155. Theme
  156. <select class="theme-selector" oninput="activateTheme(this.value)">
  157. <option value="auto" selected>Auto</option>
  158. <option value="light">Light</option>
  159. <option value="dark">Dark</option>
  160. </select>
  161. </label> |</li>
  162. </ul>
  163. </div>
  164. <div class="document">
  165. <div class="documentwrapper">
  166. <div class="bodywrapper">
  167. <div class="body" role="main">
  168. <section id="introduction">
  169. <span id="id1"></span><h1><span class="section-number">1. </span>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h1>
  170. <p>This reference manual describes the Python programming language. It is not
  171. intended as a tutorial.</p>
  172. <p>While I am trying to be as precise as possible, I chose to use English rather
  173. than formal specifications for everything except syntax and lexical analysis.
  174. This should make the document more understandable to the average reader, but
  175. will leave room for ambiguities. Consequently, if you were coming from Mars and
  176. tried to re-implement Python from this document alone, you might have to guess
  177. things and in fact you would probably end up implementing quite a different
  178. language. On the other hand, if you are using Python and wonder what the precise
  179. rules about a particular area of the language are, you should definitely be able
  180. to find them here. If you would like to see a more formal definition of the
  181. language, maybe you could volunteer your time — or invent a cloning machine
  182. :-).</p>
  183. <p>It is dangerous to add too many implementation details to a language reference
  184. document — the implementation may change, and other implementations of the
  185. same language may work differently. On the other hand, CPython is the one
  186. Python implementation in widespread use (although alternate implementations
  187. continue to gain support), and its particular quirks are sometimes worth being
  188. mentioned, especially where the implementation imposes additional limitations.
  189. Therefore, you’ll find short “implementation notes” sprinkled throughout the
  190. text.</p>
  191. <p>Every Python implementation comes with a number of built-in and standard
  192. modules. These are documented in <a class="reference internal" href="../library/index.html#library-index"><span class="std std-ref">The Python Standard Library</span></a>. A few built-in modules
  193. are mentioned when they interact in a significant way with the language
  194. definition.</p>
  195. <section id="alternate-implementations">
  196. <span id="implementations"></span><h2><span class="section-number">1.1. </span>Alternate Implementations<a class="headerlink" href="#alternate-implementations" title="Link to this heading">¶</a></h2>
  197. <p>Though there is one Python implementation which is by far the most popular,
  198. there are some alternate implementations which are of particular interest to
  199. different audiences.</p>
  200. <p>Known implementations include:</p>
  201. <dl class="simple">
  202. <dt>CPython</dt><dd><p>This is the original and most-maintained implementation of Python, written in C.
  203. New language features generally appear here first.</p>
  204. </dd>
  205. <dt>Jython</dt><dd><p>Python implemented in Java. This implementation can be used as a scripting
  206. language for Java applications, or can be used to create applications using the
  207. Java class libraries. It is also often used to create tests for Java libraries.
  208. More information can be found at <a class="reference external" href="https://www.jython.org/">the Jython website</a>.</p>
  209. </dd>
  210. <dt>Python for .NET</dt><dd><p>This implementation actually uses the CPython implementation, but is a managed
  211. .NET application and makes .NET libraries available. It was created by Brian
  212. Lloyd. For more information, see the <a class="reference external" href="https://pythonnet.github.io/">Python for .NET home page</a>.</p>
  213. </dd>
  214. <dt>IronPython</dt><dd><p>An alternate Python for .NET. Unlike Python.NET, this is a complete Python
  215. implementation that generates IL, and compiles Python code directly to .NET
  216. assemblies. It was created by Jim Hugunin, the original creator of Jython. For
  217. more information, see <a class="reference external" href="https://ironpython.net/">the IronPython website</a>.</p>
  218. </dd>
  219. <dt>PyPy</dt><dd><p>An implementation of Python written completely in Python. It supports several
  220. advanced features not found in other implementations like stackless support
  221. and a Just in Time compiler. One of the goals of the project is to encourage
  222. experimentation with the language itself by making it easier to modify the
  223. interpreter (since it is written in Python). Additional information is
  224. available on <a class="reference external" href="https://www.pypy.org/">the PyPy project’s home page</a>.</p>
  225. </dd>
  226. </dl>
  227. <p>Each of these implementations varies in some way from the language as documented
  228. in this manual, or introduces specific information beyond what’s covered in the
  229. standard Python documentation. Please refer to the implementation-specific
  230. documentation to determine what else you need to know about the specific
  231. implementation you’re using.</p>
  232. </section>
  233. <section id="notation">
  234. <span id="id2"></span><h2><span class="section-number">1.2. </span>Notation<a class="headerlink" href="#notation" title="Link to this heading">¶</a></h2>
  235. <p id="index-0">The descriptions of lexical analysis and syntax use a modified
  236. <a class="reference external" href="https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form">Backus–Naur form (BNF)</a> grammar
  237. notation. This uses the following style of definition:</p>
  238. <pre>
  239. <strong id="grammar-token-notation-name">name </strong> ::= <a class="reference internal" href="#grammar-token-notation-lc_letter"><code class="xref docutils literal notranslate"><span class="pre">lc_letter</span></code></a> (<a class="reference internal" href="#grammar-token-notation-lc_letter"><code class="xref docutils literal notranslate"><span class="pre">lc_letter</span></code></a> | &quot;_&quot;)*
  240. <strong id="grammar-token-notation-lc_letter">lc_letter</strong> ::= &quot;a&quot;...&quot;z&quot;
  241. </pre>
  242. <p>The first line says that a <code class="docutils literal notranslate"><span class="pre">name</span></code> is an <code class="docutils literal notranslate"><span class="pre">lc_letter</span></code> followed by a sequence
  243. of zero or more <code class="docutils literal notranslate"><span class="pre">lc_letter</span></code>s and underscores. An <code class="docutils literal notranslate"><span class="pre">lc_letter</span></code> in turn is
  244. any of the single characters <code class="docutils literal notranslate"><span class="pre">'a'</span></code> through <code class="docutils literal notranslate"><span class="pre">'z'</span></code>. (This rule is actually
  245. adhered to for the names defined in lexical and grammar rules in this document.)</p>
  246. <p>Each rule begins with a name (which is the name defined by the rule) and
  247. <code class="docutils literal notranslate"><span class="pre">::=</span></code>. A vertical bar (<code class="docutils literal notranslate"><span class="pre">|</span></code>) is used to separate alternatives; it is the
  248. least binding operator in this notation. A star (<code class="docutils literal notranslate"><span class="pre">*</span></code>) means zero or more
  249. repetitions of the preceding item; likewise, a plus (<code class="docutils literal notranslate"><span class="pre">+</span></code>) means one or more
  250. repetitions, and a phrase enclosed in square brackets (<code class="docutils literal notranslate"><span class="pre">[</span> <span class="pre">]</span></code>) means zero or
  251. one occurrences (in other words, the enclosed phrase is optional). The <code class="docutils literal notranslate"><span class="pre">*</span></code>
  252. and <code class="docutils literal notranslate"><span class="pre">+</span></code> operators bind as tightly as possible; parentheses are used for
  253. grouping. Literal strings are enclosed in quotes. White space is only
  254. meaningful to separate tokens. Rules are normally contained on a single line;
  255. rules with many alternatives may be formatted alternatively with each line after
  256. the first beginning with a vertical bar.</p>
  257. <p id="index-1">In lexical definitions (as the example above), two more conventions are used:
  258. Two literal characters separated by three dots mean a choice of any single
  259. character in the given (inclusive) range of ASCII characters. A phrase between
  260. angular brackets (<code class="docutils literal notranslate"><span class="pre">&lt;...&gt;</span></code>) gives an informal description of the symbol
  261. defined; e.g., this could be used to describe the notion of ‘control character’
  262. if needed.</p>
  263. <p>Even though the notation used is almost the same, there is a big difference
  264. between the meaning of lexical and syntactic definitions: a lexical definition
  265. operates on the individual characters of the input source, while a syntax
  266. definition operates on the stream of tokens generated by the lexical analysis.
  267. All uses of BNF in the next chapter (“Lexical Analysis”) are lexical
  268. definitions; uses in subsequent chapters are syntactic definitions.</p>
  269. </section>
  270. </section>
  271. <div class="clearer"></div>
  272. </div>
  273. </div>
  274. </div>
  275. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  276. <div class="sphinxsidebarwrapper">
  277. <div>
  278. <h3><a href="../contents.html">Table of Contents</a></h3>
  279. <ul>
  280. <li><a class="reference internal" href="#">1. Introduction</a><ul>
  281. <li><a class="reference internal" href="#alternate-implementations">1.1. Alternate Implementations</a></li>
  282. <li><a class="reference internal" href="#notation">1.2. Notation</a></li>
  283. </ul>
  284. </li>
  285. </ul>
  286. </div>
  287. <div>
  288. <h4>Previous topic</h4>
  289. <p class="topless"><a href="index.html"
  290. title="previous chapter">The Python Language Reference</a></p>
  291. </div>
  292. <div>
  293. <h4>Next topic</h4>
  294. <p class="topless"><a href="lexical_analysis.html"
  295. title="next chapter"><span class="section-number">2. </span>Lexical analysis</a></p>
  296. </div>
  297. <div role="note" aria-label="source link">
  298. <h3>This Page</h3>
  299. <ul class="this-page-menu">
  300. <li><a href="../bugs.html">Report a Bug</a></li>
  301. <li>
  302. <a href="https://github.com/python/cpython/blob/main/Doc/reference/introduction.rst"
  303. rel="nofollow">Show Source
  304. </a>
  305. </li>
  306. </ul>
  307. </div>
  308. </div>
  309. <div id="sidebarbutton" title="Collapse sidebar">
  310. <span>«</span>
  311. </div>
  312. </div>
  313. <div class="clearer"></div>
  314. </div>
  315. <div class="related" role="navigation" aria-label="related navigation">
  316. <h3>Navigation</h3>
  317. <ul>
  318. <li class="right" style="margin-right: 10px">
  319. <a href="../genindex.html" title="General Index"
  320. >index</a></li>
  321. <li class="right" >
  322. <a href="../py-modindex.html" title="Python Module Index"
  323. >modules</a> |</li>
  324. <li class="right" >
  325. <a href="lexical_analysis.html" title="2. Lexical analysis"
  326. >next</a> |</li>
  327. <li class="right" >
  328. <a href="index.html" title="The Python Language Reference"
  329. >previous</a> |</li>
  330. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  331. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  332. <li class="switchers">
  333. <div class="language_switcher_placeholder"></div>
  334. <div class="version_switcher_placeholder"></div>
  335. </li>
  336. <li>
  337. </li>
  338. <li id="cpython-language-and-version">
  339. <a href="../index.html">3.12.3 Documentation</a> &#187;
  340. </li>
  341. <li class="nav-item nav-item-1"><a href="index.html" >The Python Language Reference</a> &#187;</li>
  342. <li class="nav-item nav-item-this"><a href=""><span class="section-number">1. </span>Introduction</a></li>
  343. <li class="right">
  344. <div class="inline-search" role="search">
  345. <form class="inline-search" action="../search.html" method="get">
  346. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  347. <input type="submit" value="Go" />
  348. </form>
  349. </div>
  350. |
  351. </li>
  352. <li class="right">
  353. <label class="theme-selector-label">
  354. Theme
  355. <select class="theme-selector" oninput="activateTheme(this.value)">
  356. <option value="auto" selected>Auto</option>
  357. <option value="light">Light</option>
  358. <option value="dark">Dark</option>
  359. </select>
  360. </label> |</li>
  361. </ul>
  362. </div>
  363. <div class="footer">
  364. &copy;
  365. <a href="../copyright.html">
  366. Copyright
  367. </a>
  368. 2001-2024, Python Software Foundation.
  369. <br />
  370. This page is licensed under the Python Software Foundation License Version 2.
  371. <br />
  372. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  373. <br />
  374. See <a href="/license.html">History and License</a> for more information.<br />
  375. <br />
  376. The Python Software Foundation is a non-profit corporation.
  377. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  378. <br />
  379. <br />
  380. Last updated on Apr 09, 2024 (13:47 UTC).
  381. <a href="/bugs.html">Found a bug</a>?
  382. <br />
  383. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  384. </div>
  385. </body>
  386. </html>
上海开阖软件有限公司 沪ICP备12045867号-1