gooderp18绿色标准版
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1389 lines
137KB

  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="What’s New in Python 2.0" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/whatsnew/2.0.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Author, A.M. Kuchling and Moshe Zadka,. Introduction: A new release of Python, version 2.0, was released on October 16, 2000. This article covers the exciting new features in 2.0, highlights some o..." />
  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="Author, A.M. Kuchling and Moshe Zadka,. Introduction: A new release of Python, version 2.0, was released on October 16, 2000. This article covers the exciting new features in 2.0, highlights some o..." />
  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>What’s New in Python 2.0 &#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="Changelog" href="changelog.html" />
  33. <link rel="prev" title="What’s New in Python 2.1" href="2.1.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/whatsnew/2.0.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="#">What’s New in Python 2.0</a><ul>
  86. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  87. <li><a class="reference internal" href="#what-about-python-1-6">What About Python 1.6?</a></li>
  88. <li><a class="reference internal" href="#new-development-process">New Development Process</a></li>
  89. <li><a class="reference internal" href="#unicode">Unicode</a></li>
  90. <li><a class="reference internal" href="#list-comprehensions">List Comprehensions</a></li>
  91. <li><a class="reference internal" href="#augmented-assignment">Augmented Assignment</a></li>
  92. <li><a class="reference internal" href="#string-methods">String Methods</a></li>
  93. <li><a class="reference internal" href="#garbage-collection-of-cycles">Garbage Collection of Cycles</a></li>
  94. <li><a class="reference internal" href="#other-core-changes">Other Core Changes</a><ul>
  95. <li><a class="reference internal" href="#minor-language-changes">Minor Language Changes</a></li>
  96. <li><a class="reference internal" href="#changes-to-built-in-functions">Changes to Built-in Functions</a></li>
  97. </ul>
  98. </li>
  99. <li><a class="reference internal" href="#porting-to-2-0">Porting to 2.0</a></li>
  100. <li><a class="reference internal" href="#extending-embedding-changes">Extending/Embedding Changes</a></li>
  101. <li><a class="reference internal" href="#distutils-making-modules-easy-to-install">Distutils: Making Modules Easy to Install</a></li>
  102. <li><a class="reference internal" href="#xml-modules">XML Modules</a><ul>
  103. <li><a class="reference internal" href="#sax2-support">SAX2 Support</a></li>
  104. <li><a class="reference internal" href="#dom-support">DOM Support</a></li>
  105. <li><a class="reference internal" href="#relationship-to-pyxml">Relationship to PyXML</a></li>
  106. </ul>
  107. </li>
  108. <li><a class="reference internal" href="#module-changes">Module changes</a></li>
  109. <li><a class="reference internal" href="#new-modules">New modules</a></li>
  110. <li><a class="reference internal" href="#idle-improvements">IDLE Improvements</a></li>
  111. <li><a class="reference internal" href="#deleted-and-deprecated-modules">Deleted and Deprecated Modules</a></li>
  112. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  113. </ul>
  114. </li>
  115. </ul>
  116. </div>
  117. <div>
  118. <h4>Previous topic</h4>
  119. <p class="topless"><a href="2.1.html"
  120. title="previous chapter">What’s New in Python 2.1</a></p>
  121. </div>
  122. <div>
  123. <h4>Next topic</h4>
  124. <p class="topless"><a href="changelog.html"
  125. title="next chapter">Changelog</a></p>
  126. </div>
  127. <div role="note" aria-label="source link">
  128. <h3>This Page</h3>
  129. <ul class="this-page-menu">
  130. <li><a href="../bugs.html">Report a Bug</a></li>
  131. <li>
  132. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.0.rst"
  133. rel="nofollow">Show Source
  134. </a>
  135. </li>
  136. </ul>
  137. </div>
  138. </nav>
  139. </div>
  140. </div>
  141. <div class="related" role="navigation" aria-label="related navigation">
  142. <h3>Navigation</h3>
  143. <ul>
  144. <li class="right" style="margin-right: 10px">
  145. <a href="../genindex.html" title="General Index"
  146. accesskey="I">index</a></li>
  147. <li class="right" >
  148. <a href="../py-modindex.html" title="Python Module Index"
  149. >modules</a> |</li>
  150. <li class="right" >
  151. <a href="changelog.html" title="Changelog"
  152. accesskey="N">next</a> |</li>
  153. <li class="right" >
  154. <a href="2.1.html" title="What’s New in Python 2.1"
  155. accesskey="P">previous</a> |</li>
  156. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  157. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  158. <li class="switchers">
  159. <div class="language_switcher_placeholder"></div>
  160. <div class="version_switcher_placeholder"></div>
  161. </li>
  162. <li>
  163. </li>
  164. <li id="cpython-language-and-version">
  165. <a href="../index.html">3.12.3 Documentation</a> &#187;
  166. </li>
  167. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> &#187;</li>
  168. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.0</a></li>
  169. <li class="right">
  170. <div class="inline-search" role="search">
  171. <form class="inline-search" action="../search.html" method="get">
  172. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  173. <input type="submit" value="Go" />
  174. </form>
  175. </div>
  176. |
  177. </li>
  178. <li class="right">
  179. <label class="theme-selector-label">
  180. Theme
  181. <select class="theme-selector" oninput="activateTheme(this.value)">
  182. <option value="auto" selected>Auto</option>
  183. <option value="light">Light</option>
  184. <option value="dark">Dark</option>
  185. </select>
  186. </label> |</li>
  187. </ul>
  188. </div>
  189. <div class="document">
  190. <div class="documentwrapper">
  191. <div class="bodywrapper">
  192. <div class="body" role="main">
  193. <section id="what-s-new-in-python-2-0">
  194. <h1>What’s New in Python 2.0<a class="headerlink" href="#what-s-new-in-python-2-0" title="Link to this heading">¶</a></h1>
  195. <dl class="field-list simple">
  196. <dt class="field-odd">Author<span class="colon">:</span></dt>
  197. <dd class="field-odd"><p>A.M. Kuchling and Moshe Zadka</p>
  198. </dd>
  199. </dl>
  200. <section id="introduction">
  201. <h2>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h2>
  202. <p>A new release of Python, version 2.0, was released on October 16, 2000. This
  203. article covers the exciting new features in 2.0, highlights some other useful
  204. changes, and points out a few incompatible changes that may require rewriting
  205. code.</p>
  206. <p>Python’s development never completely stops between releases, and a steady flow
  207. of bug fixes and improvements are always being submitted. A host of minor fixes,
  208. a few optimizations, additional docstrings, and better error messages went into
  209. 2.0; to list them all would be impossible, but they’re certainly significant.
  210. Consult the publicly available CVS logs if you want to see the full list. This
  211. progress is due to the five developers working for PythonLabs are now getting
  212. paid to spend their days fixing bugs, and also due to the improved communication
  213. resulting from moving to SourceForge.</p>
  214. </section>
  215. <section id="what-about-python-1-6">
  216. <h2>What About Python 1.6?<a class="headerlink" href="#what-about-python-1-6" title="Link to this heading">¶</a></h2>
  217. <p>Python 1.6 can be thought of as the Contractual Obligations Python release.
  218. After the core development team left CNRI in May 2000, CNRI requested that a 1.6
  219. release be created, containing all the work on Python that had been performed at
  220. CNRI. Python 1.6 therefore represents the state of the CVS tree as of May 2000,
  221. with the most significant new feature being Unicode support. Development
  222. continued after May, of course, so the 1.6 tree received a few fixes to ensure
  223. that it’s forward-compatible with Python 2.0. 1.6 is therefore part of Python’s
  224. evolution, and not a side branch.</p>
  225. <p>So, should you take much interest in Python 1.6? Probably not. The 1.6final
  226. and 2.0beta1 releases were made on the same day (September 5, 2000), the plan
  227. being to finalize Python 2.0 within a month or so. If you have applications to
  228. maintain, there seems little point in breaking things by moving to 1.6, fixing
  229. them, and then having another round of breakage within a month by moving to 2.0;
  230. you’re better off just going straight to 2.0. Most of the really interesting
  231. features described in this document are only in 2.0, because a lot of work was
  232. done between May and September.</p>
  233. </section>
  234. <section id="new-development-process">
  235. <h2>New Development Process<a class="headerlink" href="#new-development-process" title="Link to this heading">¶</a></h2>
  236. <p>The most important change in Python 2.0 may not be to the code at all, but to
  237. how Python is developed: in May 2000 the Python developers began using the tools
  238. made available by SourceForge for storing source code, tracking bug reports,
  239. and managing the queue of patch submissions. To report bugs or submit patches
  240. for Python 2.0, use the bug tracking and patch manager tools available from
  241. Python’s project page, located at <a class="reference external" href="https://sourceforge.net/projects/python/">https://sourceforge.net/projects/python/</a>.</p>
  242. <p>The most important of the services now hosted at SourceForge is the Python CVS
  243. tree, the version-controlled repository containing the source code for Python.
  244. Previously, there were roughly 7 or so people who had write access to the CVS
  245. tree, and all patches had to be inspected and checked in by one of the people on
  246. this short list. Obviously, this wasn’t very scalable. By moving the CVS tree
  247. to SourceForge, it became possible to grant write access to more people; as of
  248. September 2000 there were 27 people able to check in changes, a fourfold
  249. increase. This makes possible large-scale changes that wouldn’t be attempted if
  250. they’d have to be filtered through the small group of core developers. For
  251. example, one day Peter Schneider-Kamp took it into his head to drop K&amp;R C
  252. compatibility and convert the C source for Python to ANSI C. After getting
  253. approval on the python-dev mailing list, he launched into a flurry of checkins
  254. that lasted about a week, other developers joined in to help, and the job was
  255. done. If there were only 5 people with write access, probably that task would
  256. have been viewed as “nice, but not worth the time and effort needed” and it
  257. would never have gotten done.</p>
  258. <p>The shift to using SourceForge’s services has resulted in a remarkable increase
  259. in the speed of development. Patches now get submitted, commented on, revised
  260. by people other than the original submitter, and bounced back and forth between
  261. people until the patch is deemed worth checking in. Bugs are tracked in one
  262. central location and can be assigned to a specific person for fixing, and we can
  263. count the number of open bugs to measure progress. This didn’t come without a
  264. cost: developers now have more e-mail to deal with, more mailing lists to
  265. follow, and special tools had to be written for the new environment. For
  266. example, SourceForge sends default patch and bug notification e-mail messages
  267. that are completely unhelpful, so Ka-Ping Yee wrote an HTML screen-scraper that
  268. sends more useful messages.</p>
  269. <p>The ease of adding code caused a few initial growing pains, such as code was
  270. checked in before it was ready or without getting clear agreement from the
  271. developer group. The approval process that has emerged is somewhat similar to
  272. that used by the Apache group. Developers can vote +1, +0, -0, or -1 on a patch;
  273. +1 and -1 denote acceptance or rejection, while +0 and -0 mean the developer is
  274. mostly indifferent to the change, though with a slight positive or negative
  275. slant. The most significant change from the Apache model is that the voting is
  276. essentially advisory, letting Guido van Rossum, who has Benevolent Dictator For
  277. Life status, know what the general opinion is. He can still ignore the result of
  278. a vote, and approve or reject a change even if the community disagrees with him.</p>
  279. <p>Producing an actual patch is the last step in adding a new feature, and is
  280. usually easy compared to the earlier task of coming up with a good design.
  281. Discussions of new features can often explode into lengthy mailing list threads,
  282. making the discussion hard to follow, and no one can read every posting to
  283. python-dev. Therefore, a relatively formal process has been set up to write
  284. Python Enhancement Proposals (PEPs), modelled on the internet RFC process. PEPs
  285. are draft documents that describe a proposed new feature, and are continually
  286. revised until the community reaches a consensus, either accepting or rejecting
  287. the proposal. Quoting from the introduction to <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0001/"><strong>PEP 1</strong></a>, “PEP Purpose and
  288. Guidelines”:</p>
  289. <blockquote class="epigraph">
  290. <div><p>PEP stands for Python Enhancement Proposal. A PEP is a design document
  291. providing information to the Python community, or describing a new feature for
  292. Python. The PEP should provide a concise technical specification of the feature
  293. and a rationale for the feature.</p>
  294. <p>We intend PEPs to be the primary mechanisms for proposing new features, for
  295. collecting community input on an issue, and for documenting the design decisions
  296. that have gone into Python. The PEP author is responsible for building
  297. consensus within the community and documenting dissenting opinions.</p>
  298. </div></blockquote>
  299. <p>Read the rest of <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0001/"><strong>PEP 1</strong></a> for the details of the PEP editorial process, style, and
  300. format. PEPs are kept in the Python CVS tree on SourceForge, though they’re not
  301. part of the Python 2.0 distribution, and are also available in HTML form from
  302. <a class="reference external" href="https://peps.python.org/">https://peps.python.org/</a>. As of September 2000, there are 25 PEPs, ranging
  303. from <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0201/"><strong>PEP 201</strong></a>, “Lockstep Iteration”, to PEP 225, “Elementwise/Objectwise
  304. Operators”.</p>
  305. </section>
  306. <section id="unicode">
  307. <h2>Unicode<a class="headerlink" href="#unicode" title="Link to this heading">¶</a></h2>
  308. <p>The largest new feature in Python 2.0 is a new fundamental data type: Unicode
  309. strings. Unicode uses 16-bit numbers to represent characters instead of the
  310. 8-bit number used by ASCII, meaning that 65,536 distinct characters can be
  311. supported.</p>
  312. <p>The final interface for Unicode support was arrived at through countless
  313. often-stormy discussions on the python-dev mailing list, and mostly implemented by
  314. Marc-André Lemburg, based on a Unicode string type implementation by Fredrik
  315. Lundh. A detailed explanation of the interface was written up as <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0100/"><strong>PEP 100</strong></a>,
  316. “Python Unicode Integration”. This article will simply cover the most
  317. significant points about the Unicode interfaces.</p>
  318. <p>In Python source code, Unicode strings are written as <code class="docutils literal notranslate"><span class="pre">u&quot;string&quot;</span></code>. Arbitrary
  319. Unicode characters can be written using a new escape sequence, <code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">HHHH</span></em></code>, where
  320. <em>HHHH</em> is a 4-digit hexadecimal number from 0000 to FFFF. The existing
  321. <code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">HH</span></em></code> escape sequence can also be used, and octal escapes can be used for
  322. characters up to U+01FF, which is represented by <code class="docutils literal notranslate"><span class="pre">\777</span></code>.</p>
  323. <p>Unicode strings, just like regular strings, are an immutable sequence type.
  324. They can be indexed and sliced, but not modified in place. Unicode strings have
  325. an <code class="docutils literal notranslate"><span class="pre">encode(</span> <span class="pre">[encoding]</span> <span class="pre">)</span></code> method that returns an 8-bit string in the desired
  326. encoding. Encodings are named by strings, such as <code class="docutils literal notranslate"><span class="pre">'ascii'</span></code>, <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code>,
  327. <code class="docutils literal notranslate"><span class="pre">'iso-8859-1'</span></code>, or whatever. A codec API is defined for implementing and
  328. registering new encodings that are then available throughout a Python program.
  329. If an encoding isn’t specified, the default encoding is usually 7-bit ASCII,
  330. though it can be changed for your Python installation by calling the
  331. <code class="docutils literal notranslate"><span class="pre">sys.setdefaultencoding(encoding)</span></code> function in a customized version of
  332. <code class="file docutils literal notranslate"><span class="pre">site.py</span></code>.</p>
  333. <p>Combining 8-bit and Unicode strings always coerces to Unicode, using the default
  334. ASCII encoding; the result of <code class="docutils literal notranslate"><span class="pre">'a'</span> <span class="pre">+</span> <span class="pre">u'bc'</span></code> is <code class="docutils literal notranslate"><span class="pre">u'abc'</span></code>.</p>
  335. <p>New built-in functions have been added, and existing built-ins modified to
  336. support Unicode:</p>
  337. <ul class="simple">
  338. <li><p><code class="docutils literal notranslate"><span class="pre">unichr(ch)</span></code> returns a Unicode string 1 character long, containing the
  339. character <em>ch</em>.</p></li>
  340. <li><p><code class="docutils literal notranslate"><span class="pre">ord(u)</span></code>, where <em>u</em> is a 1-character regular or Unicode string, returns the
  341. number of the character as an integer.</p></li>
  342. <li><p><code class="docutils literal notranslate"><span class="pre">unicode(string</span> <span class="pre">[,</span> <span class="pre">encoding]</span>&#160; <span class="pre">[,</span> <span class="pre">errors]</span> <span class="pre">)</span></code> creates a Unicode string
  343. from an 8-bit string. <code class="docutils literal notranslate"><span class="pre">encoding</span></code> is a string naming the encoding to use. The
  344. <code class="docutils literal notranslate"><span class="pre">errors</span></code> parameter specifies the treatment of characters that are invalid for
  345. the current encoding; passing <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> as the value causes an exception to
  346. be raised on any encoding error, while <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> causes errors to be silently
  347. ignored and <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> uses U+FFFD, the official replacement character, in
  348. case of any problems.</p></li>
  349. <li><p>The <code class="docutils literal notranslate"><span class="pre">exec</span></code> statement, and various built-ins such as <code class="docutils literal notranslate"><span class="pre">eval()</span></code>,
  350. <code class="docutils literal notranslate"><span class="pre">getattr()</span></code>, and <code class="docutils literal notranslate"><span class="pre">setattr()</span></code> will also accept Unicode strings as well as
  351. regular strings. (It’s possible that the process of fixing this missed some
  352. built-ins; if you find a built-in function that accepts strings but doesn’t
  353. accept Unicode strings at all, please report it as a bug.)</p></li>
  354. </ul>
  355. <p>A new module, <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a>, provides an interface to Unicode character
  356. properties. For example, <code class="docutils literal notranslate"><span class="pre">unicodedata.category(u'A')</span></code> returns the 2-character
  357. string ‘Lu’, the ‘L’ denoting it’s a letter, and ‘u’ meaning that it’s
  358. uppercase. <code class="docutils literal notranslate"><span class="pre">unicodedata.bidirectional(u'\u0660')</span></code> returns ‘AN’, meaning that
  359. U+0660 is an Arabic number.</p>
  360. <p>The <a class="reference internal" href="../library/codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> module contains functions to look up existing encodings and
  361. register new ones. Unless you want to implement a new encoding, you’ll most
  362. often use the <code class="docutils literal notranslate"><span class="pre">codecs.lookup(encoding)</span></code> function, which returns a
  363. 4-element tuple: <code class="docutils literal notranslate"><span class="pre">(encode_func,</span> <span class="pre">decode_func,</span> <span class="pre">stream_reader,</span> <span class="pre">stream_writer)</span></code>.</p>
  364. <ul class="simple">
  365. <li><p><em>encode_func</em> is a function that takes a Unicode string, and returns a 2-tuple
  366. <code class="docutils literal notranslate"><span class="pre">(string,</span> <span class="pre">length)</span></code>. <em>string</em> is an 8-bit string containing a portion (perhaps
  367. all) of the Unicode string converted into the given encoding, and <em>length</em> tells
  368. you how much of the Unicode string was converted.</p></li>
  369. <li><p><em>decode_func</em> is the opposite of <em>encode_func</em>, taking an 8-bit string and
  370. returning a 2-tuple <code class="docutils literal notranslate"><span class="pre">(ustring,</span> <span class="pre">length)</span></code>, consisting of the resulting Unicode
  371. string <em>ustring</em> and the integer <em>length</em> telling how much of the 8-bit string
  372. was consumed.</p></li>
  373. <li><p><em>stream_reader</em> is a class that supports decoding input from a stream.
  374. <em>stream_reader(file_obj)</em> returns an object that supports the <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code>,
  375. <code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">readlines()</span></code> methods. These methods will all
  376. translate from the given encoding and return Unicode strings.</p></li>
  377. <li><p><em>stream_writer</em>, similarly, is a class that supports encoding output to a
  378. stream. <em>stream_writer(file_obj)</em> returns an object that supports the
  379. <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">writelines()</span></code> methods. These methods expect Unicode
  380. strings, translating them to the given encoding on output.</p></li>
  381. </ul>
  382. <p>For example, the following code writes a Unicode string into a file, encoding
  383. it as UTF-8:</p>
  384. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">codecs</span>
  385. <span class="n">unistr</span> <span class="o">=</span> <span class="sa">u</span><span class="s1">&#39;</span><span class="se">\u0660\u2000</span><span class="s1">ab ...&#39;</span>
  386. <span class="p">(</span><span class="n">UTF8_encode</span><span class="p">,</span> <span class="n">UTF8_decode</span><span class="p">,</span>
  387. <span class="n">UTF8_streamreader</span><span class="p">,</span> <span class="n">UTF8_streamwriter</span><span class="p">)</span> <span class="o">=</span> <span class="n">codecs</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="s1">&#39;UTF-8&#39;</span><span class="p">)</span>
  388. <span class="n">output</span> <span class="o">=</span> <span class="n">UTF8_streamwriter</span><span class="p">(</span> <span class="nb">open</span><span class="p">(</span> <span class="s1">&#39;/tmp/output&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="p">)</span>
  389. <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span> <span class="n">unistr</span> <span class="p">)</span>
  390. <span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  391. </pre></div>
  392. </div>
  393. <p>The following code would then read UTF-8 input from the file:</p>
  394. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">input</span> <span class="o">=</span> <span class="n">UTF8_streamreader</span><span class="p">(</span> <span class="nb">open</span><span class="p">(</span> <span class="s1">&#39;/tmp/output&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="p">)</span>
  395. <span class="nb">print</span> <span class="nb">repr</span><span class="p">(</span><span class="nb">input</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
  396. <span class="nb">input</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  397. </pre></div>
  398. </div>
  399. <p>Unicode-aware regular expressions are available through the <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module,
  400. which has a new underlying implementation called SRE written by Fredrik Lundh of
  401. Secret Labs AB.</p>
  402. <p>A <code class="docutils literal notranslate"><span class="pre">-U</span></code> command line option was added which causes the Python compiler to
  403. interpret all string literals as Unicode string literals. This is intended to be
  404. used in testing and future-proofing your Python code, since some future version
  405. of Python may drop support for 8-bit strings and provide only Unicode strings.</p>
  406. </section>
  407. <section id="list-comprehensions">
  408. <h2>List Comprehensions<a class="headerlink" href="#list-comprehensions" title="Link to this heading">¶</a></h2>
  409. <p>Lists are a workhorse data type in Python, and many programs manipulate a list
  410. at some point. Two common operations on lists are to loop over them, and either
  411. pick out the elements that meet a certain criterion, or apply some function to
  412. each element. For example, given a list of strings, you might want to pull out
  413. all the strings containing a given substring, or strip off trailing whitespace
  414. from each line.</p>
  415. <p>The existing <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> and <a class="reference internal" href="../library/functions.html#filter" title="filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a> functions can be used for this
  416. purpose, but they require a function as one of their arguments. This is fine if
  417. there’s an existing built-in function that can be passed directly, but if there
  418. isn’t, you have to create a little function to do the required work, and
  419. Python’s scoping rules make the result ugly if the little function needs
  420. additional information. Take the first example in the previous paragraph,
  421. finding all the strings in the list containing a given substring. You could
  422. write the following to do it:</p>
  423. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Given the list L, make a list of all strings</span>
  424. <span class="c1"># containing the substring S.</span>
  425. <span class="n">sublist</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span> <span class="k">lambda</span> <span class="n">s</span><span class="p">,</span> <span class="n">substring</span><span class="o">=</span><span class="n">S</span><span class="p">:</span>
  426. <span class="n">string</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">substring</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
  427. <span class="n">L</span><span class="p">)</span>
  428. </pre></div>
  429. </div>
  430. <p>Because of Python’s scoping rules, a default argument is used so that the
  431. anonymous function created by the <a class="reference internal" href="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a> expression knows what
  432. substring is being searched for. List comprehensions make this cleaner:</p>
  433. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sublist</span> <span class="o">=</span> <span class="p">[</span> <span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">L</span> <span class="k">if</span> <span class="n">string</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">S</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="p">]</span>
  434. </pre></div>
  435. </div>
  436. <p>List comprehensions have the form:</p>
  437. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span> <span class="n">expression</span> <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">sequence1</span>
  438. <span class="k">for</span> <span class="n">expr2</span> <span class="ow">in</span> <span class="n">sequence2</span> <span class="o">...</span>
  439. <span class="k">for</span> <span class="n">exprN</span> <span class="ow">in</span> <span class="n">sequenceN</span>
  440. <span class="k">if</span> <span class="n">condition</span> <span class="p">]</span>
  441. </pre></div>
  442. </div>
  443. <p>The <code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code>…<code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code> clauses contain the sequences to be
  444. iterated over. The sequences do not have to be the same length, because they
  445. are <em>not</em> iterated over in parallel, but from left to right; this is explained
  446. more clearly in the following paragraphs. The elements of the generated list
  447. will be the successive values of <em>expression</em>. The final <code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code> clause
  448. is optional; if present, <em>expression</em> is only evaluated and added to the result
  449. if <em>condition</em> is true.</p>
  450. <p>To make the semantics very clear, a list comprehension is equivalent to the
  451. following Python code:</p>
  452. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">expr1</span> <span class="ow">in</span> <span class="n">sequence1</span><span class="p">:</span>
  453. <span class="k">for</span> <span class="n">expr2</span> <span class="ow">in</span> <span class="n">sequence2</span><span class="p">:</span>
  454. <span class="o">...</span>
  455. <span class="k">for</span> <span class="n">exprN</span> <span class="ow">in</span> <span class="n">sequenceN</span><span class="p">:</span>
  456. <span class="k">if</span> <span class="p">(</span><span class="n">condition</span><span class="p">):</span>
  457. <span class="c1"># Append the value of</span>
  458. <span class="c1"># the expression to the</span>
  459. <span class="c1"># resulting list.</span>
  460. </pre></div>
  461. </div>
  462. <p>This means that when there are multiple <code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code>…<code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code>
  463. clauses, the resulting list will be equal to the product of the lengths of all
  464. the sequences. If you have two lists of length 3, the output list is 9 elements
  465. long:</p>
  466. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">seq1</span> <span class="o">=</span> <span class="s1">&#39;abc&#39;</span>
  467. <span class="n">seq2</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
  468. <span class="o">&gt;&gt;&gt;</span> <span class="p">[</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq1</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">seq2</span><span class="p">]</span>
  469. <span class="p">[(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
  470. <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)]</span>
  471. </pre></div>
  472. </div>
  473. <p>To avoid introducing an ambiguity into Python’s grammar, if <em>expression</em> is
  474. creating a tuple, it must be surrounded with parentheses. The first list
  475. comprehension below is a syntax error, while the second one is correct:</p>
  476. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Syntax error</span>
  477. <span class="p">[</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq1</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">seq2</span><span class="p">]</span>
  478. <span class="c1"># Correct</span>
  479. <span class="p">[</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq1</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">seq2</span><span class="p">]</span>
  480. </pre></div>
  481. </div>
  482. <p>The idea of list comprehensions originally comes from the functional programming
  483. language Haskell (<a class="reference external" href="https://www.haskell.org">https://www.haskell.org</a>). Greg Ewing argued most effectively
  484. for adding them to Python and wrote the initial list comprehension patch, which
  485. was then discussed for a seemingly endless time on the python-dev mailing list
  486. and kept up-to-date by Skip Montanaro.</p>
  487. </section>
  488. <section id="augmented-assignment">
  489. <h2>Augmented Assignment<a class="headerlink" href="#augmented-assignment" title="Link to this heading">¶</a></h2>
  490. <p>Augmented assignment operators, another long-requested feature, have been added
  491. to Python 2.0. Augmented assignment operators include <code class="docutils literal notranslate"><span class="pre">+=</span></code>, <code class="docutils literal notranslate"><span class="pre">-=</span></code>, <code class="docutils literal notranslate"><span class="pre">*=</span></code>,
  492. and so forth. For example, the statement <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">+=</span> <span class="pre">2</span></code> increments the value of the
  493. variable <code class="docutils literal notranslate"><span class="pre">a</span></code> by 2, equivalent to the slightly lengthier <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">=</span> <span class="pre">a</span> <span class="pre">+</span> <span class="pre">2</span></code>.</p>
  494. <p>The full list of supported assignment operators is <code class="docutils literal notranslate"><span class="pre">+=</span></code>, <code class="docutils literal notranslate"><span class="pre">-=</span></code>, <code class="docutils literal notranslate"><span class="pre">*=</span></code>,
  495. <code class="docutils literal notranslate"><span class="pre">/=</span></code>, <code class="docutils literal notranslate"><span class="pre">%=</span></code>, <code class="docutils literal notranslate"><span class="pre">**=</span></code>, <code class="docutils literal notranslate"><span class="pre">&amp;=</span></code>, <code class="docutils literal notranslate"><span class="pre">|=</span></code>, <code class="docutils literal notranslate"><span class="pre">^=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;&gt;=</span></code>, and <code class="docutils literal notranslate"><span class="pre">&lt;&lt;=</span></code>. Python
  496. classes can override the augmented assignment operators by defining methods
  497. named <code class="xref py py-meth docutils literal notranslate"><span class="pre">__iadd__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__isub__()</span></code>, etc. For example, the following
  498. <code class="xref py py-class docutils literal notranslate"><span class="pre">Number</span></code> class stores a number and supports using += to create a new
  499. instance with an incremented value.</p>
  500. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Number</span><span class="p">:</span>
  501. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  502. <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
  503. <span class="k">def</span> <span class="fm">__iadd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">increment</span><span class="p">):</span>
  504. <span class="k">return</span> <span class="n">Number</span><span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="n">increment</span><span class="p">)</span>
  505. <span class="n">n</span> <span class="o">=</span> <span class="n">Number</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  506. <span class="n">n</span> <span class="o">+=</span> <span class="mi">3</span>
  507. <span class="nb">print</span> <span class="n">n</span><span class="o">.</span><span class="n">value</span>
  508. </pre></div>
  509. </div>
  510. <p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">__iadd__()</span></code> special method is called with the value of the increment,
  511. and should return a new instance with an appropriately modified value; this
  512. return value is bound as the new value of the variable on the left-hand side.</p>
  513. <p>Augmented assignment operators were first introduced in the C programming
  514. language, and most C-derived languages, such as <strong class="program">awk</strong>, C++, Java, Perl,
  515. and PHP also support them. The augmented assignment patch was implemented by
  516. Thomas Wouters.</p>
  517. </section>
  518. <section id="string-methods">
  519. <h2>String Methods<a class="headerlink" href="#string-methods" title="Link to this heading">¶</a></h2>
  520. <p>Until now string-manipulation functionality was in the <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module,
  521. which was usually a front-end for the <code class="xref py py-mod docutils literal notranslate"><span class="pre">strop</span></code> module written in C. The
  522. addition of Unicode posed a difficulty for the <code class="xref py py-mod docutils literal notranslate"><span class="pre">strop</span></code> module, because the
  523. functions would all need to be rewritten in order to accept either 8-bit or
  524. Unicode strings. For functions such as <code class="xref py py-func docutils literal notranslate"><span class="pre">string.replace()</span></code>, which takes 3
  525. string arguments, that means eight possible permutations, and correspondingly
  526. complicated code.</p>
  527. <p>Instead, Python 2.0 pushes the problem onto the string type, making string
  528. manipulation functionality available through methods on both 8-bit strings and
  529. Unicode strings.</p>
  530. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;andrew&#39;</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span>
  531. <span class="go">&#39;Andrew&#39;</span>
  532. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;hostname&#39;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;os&#39;</span><span class="p">,</span> <span class="s1">&#39;linux&#39;</span><span class="p">)</span>
  533. <span class="go">&#39;hlinuxtname&#39;</span>
  534. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;moshe&#39;</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;sh&#39;</span><span class="p">)</span>
  535. <span class="go">2</span>
  536. </pre></div>
  537. </div>
  538. <p>One thing that hasn’t changed, a noteworthy April Fools’ joke notwithstanding,
  539. is that Python strings are immutable. Thus, the string methods return new
  540. strings, and do not modify the string on which they operate.</p>
  541. <p>The old <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module is still around for backwards compatibility, but it
  542. mostly acts as a front-end to the new string methods.</p>
  543. <p>Two methods which have no parallel in pre-2.0 versions, although they did exist
  544. in JPython for quite some time, are <code class="xref py py-meth docutils literal notranslate"><span class="pre">startswith()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">endswith()</span></code>.
  545. <code class="docutils literal notranslate"><span class="pre">s.startswith(t)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">s[:len(t)]</span> <span class="pre">==</span> <span class="pre">t</span></code>, while
  546. <code class="docutils literal notranslate"><span class="pre">s.endswith(t)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">s[-len(t):]</span> <span class="pre">==</span> <span class="pre">t</span></code>.</p>
  547. <p>One other method which deserves special mention is <code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code>. The
  548. <code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code> method of a string receives one parameter, a sequence of strings,
  549. and is equivalent to the <code class="xref py py-func docutils literal notranslate"><span class="pre">string.join()</span></code> function from the old <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a>
  550. module, with the arguments reversed. In other words, <code class="docutils literal notranslate"><span class="pre">s.join(seq)</span></code> is
  551. equivalent to the old <code class="docutils literal notranslate"><span class="pre">string.join(seq,</span> <span class="pre">s)</span></code>.</p>
  552. </section>
  553. <section id="garbage-collection-of-cycles">
  554. <h2>Garbage Collection of Cycles<a class="headerlink" href="#garbage-collection-of-cycles" title="Link to this heading">¶</a></h2>
  555. <p>The C implementation of Python uses reference counting to implement garbage
  556. collection. Every Python object maintains a count of the number of references
  557. pointing to itself, and adjusts the count as references are created or
  558. destroyed. Once the reference count reaches zero, the object is no longer
  559. accessible, since you need to have a reference to an object to access it, and if
  560. the count is zero, no references exist any longer.</p>
  561. <p>Reference counting has some pleasant properties: it’s easy to understand and
  562. implement, and the resulting implementation is portable, fairly fast, and reacts
  563. well with other libraries that implement their own memory handling schemes. The
  564. major problem with reference counting is that it sometimes doesn’t realise that
  565. objects are no longer accessible, resulting in a memory leak. This happens when
  566. there are cycles of references.</p>
  567. <p>Consider the simplest possible cycle, a class instance which has a reference to
  568. itself:</p>
  569. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">instance</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
  570. <span class="n">instance</span><span class="o">.</span><span class="n">myself</span> <span class="o">=</span> <span class="n">instance</span>
  571. </pre></div>
  572. </div>
  573. <p>After the above two lines of code have been executed, the reference count of
  574. <code class="docutils literal notranslate"><span class="pre">instance</span></code> is 2; one reference is from the variable named <code class="docutils literal notranslate"><span class="pre">'instance'</span></code>, and
  575. the other is from the <code class="docutils literal notranslate"><span class="pre">myself</span></code> attribute of the instance.</p>
  576. <p>If the next line of code is <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">instance</span></code>, what happens? The reference count
  577. of <code class="docutils literal notranslate"><span class="pre">instance</span></code> is decreased by 1, so it has a reference count of 1; the
  578. reference in the <code class="docutils literal notranslate"><span class="pre">myself</span></code> attribute still exists. Yet the instance is no
  579. longer accessible through Python code, and it could be deleted. Several objects
  580. can participate in a cycle if they have references to each other, causing all of
  581. the objects to be leaked.</p>
  582. <p>Python 2.0 fixes this problem by periodically executing a cycle detection
  583. algorithm which looks for inaccessible cycles and deletes the objects involved.
  584. A new <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code></a> module provides functions to perform a garbage collection,
  585. obtain debugging statistics, and tuning the collector’s parameters.</p>
  586. <p>Running the cycle detection algorithm takes some time, and therefore will result
  587. in some additional overhead. It is hoped that after we’ve gotten experience
  588. with the cycle collection from using 2.0, Python 2.1 will be able to minimize
  589. the overhead with careful tuning. It’s not yet obvious how much performance is
  590. lost, because benchmarking this is tricky and depends crucially on how often the
  591. program creates and destroys objects. The detection of cycles can be disabled
  592. when Python is compiled, if you can’t afford even a tiny speed penalty or
  593. suspect that the cycle collection is buggy, by specifying the
  594. <code class="xref std std-option docutils literal notranslate"><span class="pre">--without-cycle-gc</span></code> switch when running the <strong class="program">configure</strong>
  595. script.</p>
  596. <p>Several people tackled this problem and contributed to a solution. An early
  597. implementation of the cycle detection approach was written by Toby Kelsey. The
  598. current algorithm was suggested by Eric Tiedemann during a visit to CNRI, and
  599. Guido van Rossum and Neil Schemenauer wrote two different implementations, which
  600. were later integrated by Neil. Lots of other people offered suggestions along
  601. the way; the March 2000 archives of the python-dev mailing list contain most of
  602. the relevant discussion, especially in the threads titled “Reference cycle
  603. collection for Python” and “Finalization again”.</p>
  604. </section>
  605. <section id="other-core-changes">
  606. <h2>Other Core Changes<a class="headerlink" href="#other-core-changes" title="Link to this heading">¶</a></h2>
  607. <p>Various minor changes have been made to Python’s syntax and built-in functions.
  608. None of the changes are very far-reaching, but they’re handy conveniences.</p>
  609. <section id="minor-language-changes">
  610. <h3>Minor Language Changes<a class="headerlink" href="#minor-language-changes" title="Link to this heading">¶</a></h3>
  611. <p>A new syntax makes it more convenient to call a given function with a tuple of
  612. arguments and/or a dictionary of keyword arguments. In Python 1.5 and earlier,
  613. you’d use the <code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code> built-in function: <code class="docutils literal notranslate"><span class="pre">apply(f,</span> <span class="pre">args,</span> <span class="pre">kw)</span></code> calls the
  614. function <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code> with the argument tuple <em>args</em> and the keyword arguments in
  615. the dictionary <em>kw</em>. <code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code> is the same in 2.0, but thanks to a patch
  616. from Greg Ewing, <code class="docutils literal notranslate"><span class="pre">f(*args,</span> <span class="pre">**kw)</span></code> is a shorter and clearer way to achieve the
  617. same effect. This syntax is symmetrical with the syntax for defining
  618. functions:</p>
  619. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
  620. <span class="c1"># args is a tuple of positional args,</span>
  621. <span class="c1"># kw is a dictionary of keyword args</span>
  622. <span class="o">...</span>
  623. </pre></div>
  624. </div>
  625. <p>The <code class="docutils literal notranslate"><span class="pre">print</span></code> statement can now have its output directed to a file-like
  626. object by following the <code class="docutils literal notranslate"><span class="pre">print</span></code> with <code class="docutils literal notranslate"><span class="pre">&gt;&gt;</span> <span class="pre">file</span></code>, similar to the
  627. redirection operator in Unix shells. Previously you’d either have to use the
  628. <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method of the file-like object, which lacks the convenience and
  629. simplicity of <code class="docutils literal notranslate"><span class="pre">print</span></code>, or you could assign a new value to
  630. <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and then restore the old value. For sending output to standard
  631. error, it’s much easier to write this:</p>
  632. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span> <span class="o">&gt;&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s2">&quot;Warning: action field not supplied&quot;</span>
  633. </pre></div>
  634. </div>
  635. <p>Modules can now be renamed on importing them, using the syntax <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">module</span>
  636. <span class="pre">as</span> <span class="pre">name</span></code> or <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">name</span> <span class="pre">as</span> <span class="pre">othername</span></code>. The patch was submitted
  637. by Thomas Wouters.</p>
  638. <p>A new format style is available when using the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator; ‘%r’ will insert
  639. the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of its argument. This was also added from symmetry
  640. considerations, this time for symmetry with the existing ‘%s’ format style,
  641. which inserts the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of its argument. For example, <code class="docutils literal notranslate"><span class="pre">'%r</span> <span class="pre">%s'</span> <span class="pre">%</span> <span class="pre">('abc',</span>
  642. <span class="pre">'abc')</span></code> returns a string containing <code class="docutils literal notranslate"><span class="pre">'abc'</span> <span class="pre">abc</span></code>.</p>
  643. <p>Previously there was no way to implement a class that overrode Python’s built-in
  644. <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> operator and implemented a custom version. <code class="docutils literal notranslate"><span class="pre">obj</span> <span class="pre">in</span> <span class="pre">seq</span></code> returns
  645. true if <em>obj</em> is present in the sequence <em>seq</em>; Python computes this by simply
  646. trying every index of the sequence until either <em>obj</em> is found or an
  647. <a class="reference internal" href="../library/exceptions.html#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a> is encountered. Moshe Zadka contributed a patch which adds a
  648. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code> magic method for providing a custom implementation for
  649. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code>. Additionally, new built-in objects written in C can define what
  650. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code> means for them via a new slot in the sequence protocol.</p>
  651. <p>Earlier versions of Python used a recursive algorithm for deleting objects.
  652. Deeply nested data structures could cause the interpreter to fill up the C stack
  653. and crash; Christian Tismer rewrote the deletion logic to fix this problem. On
  654. a related note, comparing recursive objects recursed infinitely and crashed;
  655. Jeremy Hylton rewrote the code to no longer crash, producing a useful result
  656. instead. For example, after this code:</p>
  657. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[]</span>
  658. <span class="n">b</span> <span class="o">=</span> <span class="p">[]</span>
  659. <span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
  660. <span class="n">b</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
  661. </pre></div>
  662. </div>
  663. <p>The comparison <code class="docutils literal notranslate"><span class="pre">a==b</span></code> returns true, because the two recursive data structures
  664. are isomorphic. See the thread “trashcan and PR#7” in the April 2000 archives of
  665. the python-dev mailing list for the discussion leading up to this
  666. implementation, and some useful relevant links. Note that comparisons can now
  667. also raise exceptions. In earlier versions of Python, a comparison operation
  668. such as <code class="docutils literal notranslate"><span class="pre">cmp(a,b)</span></code> would always produce an answer, even if a user-defined
  669. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code> method encountered an error, since the resulting exception would
  670. simply be silently swallowed.</p>
  671. <p>Work has been done on porting Python to 64-bit Windows on the Itanium processor,
  672. mostly by Trent Mick of ActiveState. (Confusingly, <code class="docutils literal notranslate"><span class="pre">sys.platform</span></code> is still
  673. <code class="docutils literal notranslate"><span class="pre">'win32'</span></code> on Win64 because it seems that for ease of porting, MS Visual C++
  674. treats code as 32 bit on Itanium.) PythonWin also supports Windows CE; see the
  675. Python CE page at <a class="reference external" href="https://pythonce.sourceforge.net/">https://pythonce.sourceforge.net/</a> for more information.</p>
  676. <p>Another new platform is Darwin/MacOS X; initial support for it is in Python 2.0.
  677. Dynamic loading works, if you specify “configure –with-dyld –with-suffix=.x”.
  678. Consult the README in the Python source distribution for more instructions.</p>
  679. <p>An attempt has been made to alleviate one of Python’s warts, the often-confusing
  680. <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> exception when code refers to a local variable before the
  681. variable has been assigned a value. For example, the following code raises an
  682. exception on the <code class="docutils literal notranslate"><span class="pre">print</span></code> statement in both 1.5.2 and 2.0; in 1.5.2 a
  683. <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> exception is raised, while 2.0 raises a new
  684. <a class="reference internal" href="../library/exceptions.html#UnboundLocalError" title="UnboundLocalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnboundLocalError</span></code></a> exception. <a class="reference internal" href="../library/exceptions.html#UnboundLocalError" title="UnboundLocalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnboundLocalError</span></code></a> is a subclass of
  685. <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a>, so any existing code that expects <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> to be
  686. raised should still work.</p>
  687. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
  688. <span class="nb">print</span> <span class="s2">&quot;i=&quot;</span><span class="p">,</span><span class="n">i</span>
  689. <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span>
  690. <span class="n">f</span><span class="p">()</span>
  691. </pre></div>
  692. </div>
  693. <p>Two new exceptions, <a class="reference internal" href="../library/exceptions.html#TabError" title="TabError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TabError</span></code></a> and <a class="reference internal" href="../library/exceptions.html#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a>, have been
  694. introduced. They’re both subclasses of <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>, and are raised when
  695. Python code is found to be improperly indented.</p>
  696. </section>
  697. <section id="changes-to-built-in-functions">
  698. <h3>Changes to Built-in Functions<a class="headerlink" href="#changes-to-built-in-functions" title="Link to this heading">¶</a></h3>
  699. <p>A new built-in, <code class="docutils literal notranslate"><span class="pre">zip(seq1,</span> <span class="pre">seq2,</span> <span class="pre">...)</span></code>, has been added. <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a>
  700. returns a list of tuples where each tuple contains the i-th element from each of
  701. the argument sequences. The difference between <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> and <code class="docutils literal notranslate"><span class="pre">map(None,</span>
  702. <span class="pre">seq1,</span> <span class="pre">seq2)</span></code> is that <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> pads the sequences with <code class="docutils literal notranslate"><span class="pre">None</span></code> if the
  703. sequences aren’t all of the same length, while <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> truncates the
  704. returned list to the length of the shortest argument sequence.</p>
  705. <p>The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> and <code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code> functions now accept an optional “base”
  706. parameter when the first argument is a string. <code class="docutils literal notranslate"><span class="pre">int('123',</span> <span class="pre">10)</span></code> returns 123,
  707. while <code class="docutils literal notranslate"><span class="pre">int('123',</span> <span class="pre">16)</span></code> returns 291. <code class="docutils literal notranslate"><span class="pre">int(123,</span> <span class="pre">16)</span></code> raises a
  708. <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception with the message “can’t convert non-string with
  709. explicit base”.</p>
  710. <p>A new variable holding more detailed version information has been added to the
  711. <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> module. <code class="docutils literal notranslate"><span class="pre">sys.version_info</span></code> is a tuple <code class="docutils literal notranslate"><span class="pre">(major,</span> <span class="pre">minor,</span> <span class="pre">micro,</span>
  712. <span class="pre">level,</span> <span class="pre">serial)</span></code> For example, in a hypothetical 2.0.1beta1, <code class="docutils literal notranslate"><span class="pre">sys.version_info</span></code>
  713. would be <code class="docutils literal notranslate"><span class="pre">(2,</span> <span class="pre">0,</span> <span class="pre">1,</span> <span class="pre">'beta',</span> <span class="pre">1)</span></code>. <em>level</em> is a string such as <code class="docutils literal notranslate"><span class="pre">&quot;alpha&quot;</span></code>,
  714. <code class="docutils literal notranslate"><span class="pre">&quot;beta&quot;</span></code>, or <code class="docutils literal notranslate"><span class="pre">&quot;final&quot;</span></code> for a final release.</p>
  715. <p>Dictionaries have an odd new method, <code class="docutils literal notranslate"><span class="pre">setdefault(key,</span> <span class="pre">default)</span></code>, which
  716. behaves similarly to the existing <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> method. However, if the key is
  717. missing, <code class="xref py py-meth docutils literal notranslate"><span class="pre">setdefault()</span></code> both returns the value of <em>default</em> as <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code>
  718. would do, and also inserts it into the dictionary as the value for <em>key</em>. Thus,
  719. the following lines of code:</p>
  720. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="nb">dict</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span> <span class="n">key</span> <span class="p">):</span> <span class="k">return</span> <span class="nb">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  721. <span class="k">else</span><span class="p">:</span>
  722. <span class="nb">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
  723. <span class="k">return</span> <span class="nb">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  724. </pre></div>
  725. </div>
  726. <p>can be reduced to a single <code class="docutils literal notranslate"><span class="pre">return</span> <span class="pre">dict.setdefault(key,</span> <span class="pre">[])</span></code> statement.</p>
  727. <p>The interpreter sets a maximum recursion depth in order to catch runaway
  728. recursion before filling the C stack and causing a core dump or GPF..
  729. Previously this limit was fixed when you compiled Python, but in 2.0 the maximum
  730. recursion depth can be read and modified using <a class="reference internal" href="../library/sys.html#sys.getrecursionlimit" title="sys.getrecursionlimit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getrecursionlimit()</span></code></a> and
  731. <a class="reference internal" href="../library/sys.html#sys.setrecursionlimit" title="sys.setrecursionlimit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setrecursionlimit()</span></code></a>. The default value is 1000, and a rough maximum
  732. value for a given platform can be found by running a new script,
  733. <code class="file docutils literal notranslate"><span class="pre">Misc/find_recursionlimit.py</span></code>.</p>
  734. </section>
  735. </section>
  736. <section id="porting-to-2-0">
  737. <h2>Porting to 2.0<a class="headerlink" href="#porting-to-2-0" title="Link to this heading">¶</a></h2>
  738. <p>New Python releases try hard to be compatible with previous releases, and the
  739. record has been pretty good. However, some changes are considered useful
  740. enough, usually because they fix initial design decisions that turned out to be
  741. actively mistaken, that breaking backward compatibility can’t always be avoided.
  742. This section lists the changes in Python 2.0 that may cause old Python code to
  743. break.</p>
  744. <p>The change which will probably break the most code is tightening up the
  745. arguments accepted by some methods. Some methods would take multiple arguments
  746. and treat them as a tuple, particularly various list methods such as
  747. <code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">insert()</span></code>. In earlier versions of Python, if <code class="docutils literal notranslate"><span class="pre">L</span></code> is
  748. a list, <code class="docutils literal notranslate"><span class="pre">L.append(</span> <span class="pre">1,2</span> <span class="pre">)</span></code> appends the tuple <code class="docutils literal notranslate"><span class="pre">(1,2)</span></code> to the list. In Python
  749. 2.0 this causes a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception to be raised, with the message:
  750. ‘append requires exactly 1 argument; 2 given’. The fix is to simply add an
  751. extra set of parentheses to pass both values as a tuple: <code class="docutils literal notranslate"><span class="pre">L.append(</span> <span class="pre">(1,2)</span> <span class="pre">)</span></code>.</p>
  752. <p>The earlier versions of these methods were more forgiving because they used an
  753. old function in Python’s C interface to parse their arguments; 2.0 modernizes
  754. them to use <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>, the current argument parsing function,
  755. which provides more helpful error messages and treats multi-argument calls as
  756. errors. If you absolutely must use 2.0 but can’t fix your code, you can edit
  757. <code class="file docutils literal notranslate"><span class="pre">Objects/listobject.c</span></code> and define the preprocessor symbol
  758. <code class="docutils literal notranslate"><span class="pre">NO_STRICT_LIST_APPEND</span></code> to preserve the old behaviour; this isn’t recommended.</p>
  759. <p>Some of the functions in the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module are still forgiving in this
  760. way. For example, <code class="docutils literal notranslate"><span class="pre">socket.connect(</span> <span class="pre">('hostname',</span> <span class="pre">25)</span> <span class="pre">)</span></code> is the correct
  761. form, passing a tuple representing an IP address, but <code class="docutils literal notranslate"><span class="pre">socket.connect('hostname',</span> <span class="pre">25)</span></code>
  762. also works. <a class="reference internal" href="../library/socket.html#socket.socket.connect_ex" title="socket.socket.connect_ex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.connect_ex</span></code></a> and <a class="reference internal" href="../library/socket.html#socket.socket.bind" title="socket.socket.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.bind</span></code></a>
  763. are similarly easy-going. 2.0alpha1 tightened these functions up, but because
  764. the documentation actually used the erroneous multiple argument form, many
  765. people wrote code which would break with the stricter checking. GvR backed out
  766. the changes in the face of public reaction, so for the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module, the
  767. documentation was fixed and the multiple argument form is simply marked as
  768. deprecated; it <em>will</em> be tightened up again in a future Python version.</p>
  769. <p>The <code class="docutils literal notranslate"><span class="pre">\x</span></code> escape in string literals now takes exactly 2 hex digits. Previously
  770. it would consume all the hex digits following the ‘x’ and take the lowest 8 bits
  771. of the result, so <code class="docutils literal notranslate"><span class="pre">\x123456</span></code> was equivalent to <code class="docutils literal notranslate"><span class="pre">\x56</span></code>.</p>
  772. <p>The <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> and <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> exceptions have a more friendly
  773. error message, whose text will be something like <code class="docutils literal notranslate"><span class="pre">'Spam'</span> <span class="pre">instance</span> <span class="pre">has</span> <span class="pre">no</span>
  774. <span class="pre">attribute</span> <span class="pre">'eggs'</span></code> or <code class="docutils literal notranslate"><span class="pre">name</span> <span class="pre">'eggs'</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">defined</span></code>. Previously the error
  775. message was just the missing attribute name <code class="docutils literal notranslate"><span class="pre">eggs</span></code>, and code written to take
  776. advantage of this fact will break in 2.0.</p>
  777. <p>Some work has been done to make integers and long integers a bit more
  778. interchangeable. In 1.5.2, large-file support was added for Solaris, to allow
  779. reading files larger than 2 GiB; this made the <code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code> method of file
  780. objects return a long integer instead of a regular integer. Some code would
  781. subtract two file offsets and attempt to use the result to multiply a sequence
  782. or slice a string, but this raised a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. In 2.0, long integers
  783. can be used to multiply or slice a sequence, and it’ll behave as you’d
  784. intuitively expect it to; <code class="docutils literal notranslate"><span class="pre">3L</span> <span class="pre">*</span> <span class="pre">'abc'</span></code> produces ‘abcabcabc’, and
  785. <code class="docutils literal notranslate"><span class="pre">(0,1,2,3)[2L:4L]</span></code> produces (2,3). Long integers can also be used in various
  786. contexts where previously only integers were accepted, such as in the
  787. <code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code> method of file objects, and in the formats supported by the <code class="docutils literal notranslate"><span class="pre">%</span></code>
  788. operator (<code class="docutils literal notranslate"><span class="pre">%d</span></code>, <code class="docutils literal notranslate"><span class="pre">%i</span></code>, <code class="docutils literal notranslate"><span class="pre">%x</span></code>, etc.). For example, <code class="docutils literal notranslate"><span class="pre">&quot;%d&quot;</span> <span class="pre">%</span> <span class="pre">2L**64</span></code> will
  789. produce the string <code class="docutils literal notranslate"><span class="pre">18446744073709551616</span></code>.</p>
  790. <p>The subtlest long integer change of all is that the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of a long
  791. integer no longer has a trailing ‘L’ character, though <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> still
  792. includes it. The ‘L’ annoyed many people who wanted to print long integers that
  793. looked just like regular integers, since they had to go out of their way to chop
  794. off the character. This is no longer a problem in 2.0, but code which does
  795. <code class="docutils literal notranslate"><span class="pre">str(longval)[:-1]</span></code> and assumes the ‘L’ is there, will now lose the final
  796. digit.</p>
  797. <p>Taking the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of a float now uses a different formatting precision
  798. than <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>. <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> uses <code class="docutils literal notranslate"><span class="pre">%.17g</span></code> format string for C’s
  799. <code class="xref py py-func docutils literal notranslate"><span class="pre">sprintf()</span></code>, while <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> uses <code class="docutils literal notranslate"><span class="pre">%.12g</span></code> as before. The effect is that
  800. <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> may occasionally show more decimal places than <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>, for
  801. certain numbers. For example, the number 8.1 can’t be represented exactly in
  802. binary, so <code class="docutils literal notranslate"><span class="pre">repr(8.1)</span></code> is <code class="docutils literal notranslate"><span class="pre">'8.0999999999999996'</span></code>, while str(8.1) is
  803. <code class="docutils literal notranslate"><span class="pre">'8.1'</span></code>.</p>
  804. <p>The <code class="docutils literal notranslate"><span class="pre">-X</span></code> command-line option, which turned all standard exceptions into
  805. strings instead of classes, has been removed; the standard exceptions will now
  806. always be classes. The <code class="xref py py-mod docutils literal notranslate"><span class="pre">exceptions</span></code> module containing the standard
  807. exceptions was translated from Python to a built-in C module, written by Barry
  808. Warsaw and Fredrik Lundh.</p>
  809. </section>
  810. <section id="extending-embedding-changes">
  811. <h2>Extending/Embedding Changes<a class="headerlink" href="#extending-embedding-changes" title="Link to this heading">¶</a></h2>
  812. <p>Some of the changes are under the covers, and will only be apparent to people
  813. writing C extension modules or embedding a Python interpreter in a larger
  814. application. If you aren’t dealing with Python’s C API, you can safely skip
  815. this section.</p>
  816. <p>The version number of the Python C API was incremented, so C extensions compiled
  817. for 1.5.2 must be recompiled in order to work with 2.0. On Windows, it’s not
  818. possible for Python 2.0 to import a third party extension built for Python 1.5.x
  819. due to how Windows DLLs work, so Python will raise an exception and the import
  820. will fail.</p>
  821. <p>Users of Jim Fulton’s ExtensionClass module will be pleased to find out that
  822. hooks have been added so that ExtensionClasses are now supported by
  823. <a class="reference internal" href="../library/functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> and <a class="reference internal" href="../library/functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a>. This means you no longer have to
  824. remember to write code such as <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">type(obj)</span> <span class="pre">==</span> <span class="pre">myExtensionClass</span></code>, but can use
  825. the more natural <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">isinstance(obj,</span> <span class="pre">myExtensionClass)</span></code>.</p>
  826. <p>The <code class="file docutils literal notranslate"><span class="pre">Python/importdl.c</span></code> file, which was a mass of #ifdefs to support
  827. dynamic loading on many different platforms, was cleaned up and reorganised by
  828. Greg Stein. <code class="file docutils literal notranslate"><span class="pre">importdl.c</span></code> is now quite small, and platform-specific code
  829. has been moved into a bunch of <code class="file docutils literal notranslate"><span class="pre">Python/dynload_*.c</span></code> files. Another
  830. cleanup: there were also a number of <code class="file docutils literal notranslate"><span class="pre">my*.h</span></code> files in the Include/
  831. directory that held various portability hacks; they’ve been merged into a single
  832. file, <code class="file docutils literal notranslate"><span class="pre">Include/pyport.h</span></code>.</p>
  833. <p>Vladimir Marangozov’s long-awaited malloc restructuring was completed, to make
  834. it easy to have the Python interpreter use a custom allocator instead of C’s
  835. standard <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code>. For documentation, read the comments in
  836. <code class="file docutils literal notranslate"><span class="pre">Include/pymem.h</span></code> and <code class="file docutils literal notranslate"><span class="pre">Include/objimpl.h</span></code>. For the lengthy
  837. discussions during which the interface was hammered out, see the web archives of
  838. the ‘patches’ and ‘python-dev’ lists at python.org.</p>
  839. <p>Recent versions of the GUSI development environment for MacOS support POSIX
  840. threads. Therefore, Python’s POSIX threading support now works on the
  841. Macintosh. Threading support using the user-space GNU <code class="docutils literal notranslate"><span class="pre">pth</span></code> library was also
  842. contributed.</p>
  843. <p>Threading support on Windows was enhanced, too. Windows supports thread locks
  844. that use kernel objects only in case of contention; in the common case when
  845. there’s no contention, they use simpler functions which are an order of
  846. magnitude faster. A threaded version of Python 1.5.2 on NT is twice as slow as
  847. an unthreaded version; with the 2.0 changes, the difference is only 10%. These
  848. improvements were contributed by Yakov Markovitch.</p>
  849. <p>Python 2.0’s source now uses only ANSI C prototypes, so compiling Python now
  850. requires an ANSI C compiler, and can no longer be done using a compiler that
  851. only supports K&amp;R C.</p>
  852. <p>Previously the Python virtual machine used 16-bit numbers in its bytecode,
  853. limiting the size of source files. In particular, this affected the maximum
  854. size of literal lists and dictionaries in Python source; occasionally people who
  855. are generating Python code would run into this limit. A patch by Charles G.
  856. Waldman raises the limit from <code class="docutils literal notranslate"><span class="pre">2**16</span></code> to <code class="docutils literal notranslate"><span class="pre">2**32</span></code>.</p>
  857. <p>Three new convenience functions intended for adding constants to a module’s
  858. dictionary at module initialization time were added: <a class="reference internal" href="../c-api/module.html#c.PyModule_AddObject" title="PyModule_AddObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_AddObject()</span></code></a>,
  859. <a class="reference internal" href="../c-api/module.html#c.PyModule_AddIntConstant" title="PyModule_AddIntConstant"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_AddIntConstant()</span></code></a>, and <a class="reference internal" href="../c-api/module.html#c.PyModule_AddStringConstant" title="PyModule_AddStringConstant"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_AddStringConstant()</span></code></a>. Each
  860. of these functions takes a module object, a null-terminated C string containing
  861. the name to be added, and a third argument for the value to be assigned to the
  862. name. This third argument is, respectively, a Python object, a C long, or a C
  863. string.</p>
  864. <p>A wrapper API was added for Unix-style signal handlers. <a class="reference internal" href="../c-api/sys.html#c.PyOS_getsig" title="PyOS_getsig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_getsig()</span></code></a> gets
  865. a signal handler and <a class="reference internal" href="../c-api/sys.html#c.PyOS_setsig" title="PyOS_setsig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_setsig()</span></code></a> will set a new handler.</p>
  866. </section>
  867. <section id="distutils-making-modules-easy-to-install">
  868. <h2>Distutils: Making Modules Easy to Install<a class="headerlink" href="#distutils-making-modules-easy-to-install" title="Link to this heading">¶</a></h2>
  869. <p>Before Python 2.0, installing modules was a tedious affair – there was no way
  870. to figure out automatically where Python is installed, or what compiler options
  871. to use for extension modules. Software authors had to go through an arduous
  872. ritual of editing Makefiles and configuration files, which only really work on
  873. Unix and leave Windows and MacOS unsupported. Python users faced wildly
  874. differing installation instructions which varied between different extension
  875. packages, which made administering a Python installation something of a chore.</p>
  876. <p>The SIG for distribution utilities, shepherded by Greg Ward, has created the
  877. Distutils, a system to make package installation much easier. They form the
  878. <code class="docutils literal notranslate"><span class="pre">distutils</span></code> package, a new part of Python’s standard library. In the best
  879. case, installing a Python module from source will require the same steps: first
  880. you simply mean unpack the tarball or zip archive, and the run “<code class="docutils literal notranslate"><span class="pre">python</span>
  881. <span class="pre">setup.py</span> <span class="pre">install</span></code>”. The platform will be automatically detected, the compiler
  882. will be recognized, C extension modules will be compiled, and the distribution
  883. installed into the proper directory. Optional command-line arguments provide
  884. more control over the installation process, the distutils package offers many
  885. places to override defaults – separating the build from the install, building
  886. or installing in non-default directories, and more.</p>
  887. <p>In order to use the Distutils, you need to write a <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> script. For
  888. the simple case, when the software contains only .py files, a minimal
  889. <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> can be just a few lines long:</p>
  890. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span>
  891. <span class="n">setup</span> <span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;foo&quot;</span><span class="p">,</span> <span class="n">version</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span><span class="p">,</span>
  892. <span class="n">py_modules</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;module1&quot;</span><span class="p">,</span> <span class="s2">&quot;module2&quot;</span><span class="p">])</span>
  893. </pre></div>
  894. </div>
  895. <p>The <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> file isn’t much more complicated if the software consists
  896. of a few packages:</p>
  897. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span>
  898. <span class="n">setup</span> <span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;foo&quot;</span><span class="p">,</span> <span class="n">version</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span><span class="p">,</span>
  899. <span class="n">packages</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;package&quot;</span><span class="p">,</span> <span class="s2">&quot;package.subpackage&quot;</span><span class="p">])</span>
  900. </pre></div>
  901. </div>
  902. <p>A C extension can be the most complicated case; here’s an example taken from
  903. the PyXML package:</p>
  904. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span><span class="p">,</span> <span class="n">Extension</span>
  905. <span class="n">expat_extension</span> <span class="o">=</span> <span class="n">Extension</span><span class="p">(</span><span class="s1">&#39;xml.parsers.pyexpat&#39;</span><span class="p">,</span>
  906. <span class="n">define_macros</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;XML_NS&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)],</span>
  907. <span class="n">include_dirs</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">&#39;extensions/expat/xmltok&#39;</span><span class="p">,</span>
  908. <span class="s1">&#39;extensions/expat/xmlparse&#39;</span> <span class="p">],</span>
  909. <span class="n">sources</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">&#39;extensions/pyexpat.c&#39;</span><span class="p">,</span>
  910. <span class="s1">&#39;extensions/expat/xmltok/xmltok.c&#39;</span><span class="p">,</span>
  911. <span class="s1">&#39;extensions/expat/xmltok/xmlrole.c&#39;</span><span class="p">,</span> <span class="p">]</span>
  912. <span class="p">)</span>
  913. <span class="n">setup</span> <span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;PyXML&quot;</span><span class="p">,</span> <span class="n">version</span> <span class="o">=</span> <span class="s2">&quot;0.5.4&quot;</span><span class="p">,</span>
  914. <span class="n">ext_modules</span> <span class="o">=</span><span class="p">[</span> <span class="n">expat_extension</span> <span class="p">]</span> <span class="p">)</span>
  915. </pre></div>
  916. </div>
  917. <p>The Distutils can also take care of creating source and binary distributions.
  918. The “sdist” command, run by “<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">sdist</span></code>’, builds a source
  919. distribution such as <code class="file docutils literal notranslate"><span class="pre">foo-1.0.tar.gz</span></code>. Adding new commands isn’t
  920. difficult, “bdist_rpm” and “bdist_wininst” commands have already been
  921. contributed to create an RPM distribution and a Windows installer for the
  922. software, respectively. Commands to create other distribution formats such as
  923. Debian packages and Solaris <code class="file docutils literal notranslate"><span class="pre">.pkg</span></code> files are in various stages of
  924. development.</p>
  925. <p>All this is documented in a new manual, <em>Distributing Python Modules</em>, that
  926. joins the basic set of Python documentation.</p>
  927. </section>
  928. <section id="xml-modules">
  929. <h2>XML Modules<a class="headerlink" href="#xml-modules" title="Link to this heading">¶</a></h2>
  930. <p>Python 1.5.2 included a simple XML parser in the form of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmllib</span></code>
  931. module, contributed by Sjoerd Mullender. Since 1.5.2’s release, two different
  932. interfaces for processing XML have become common: SAX2 (version 2 of the Simple
  933. API for XML) provides an event-driven interface with some similarities to
  934. <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmllib</span></code>, and the DOM (Document Object Model) provides a tree-based
  935. interface, transforming an XML document into a tree of nodes that can be
  936. traversed and modified. Python 2.0 includes a SAX2 interface and a stripped-down
  937. DOM interface as part of the <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package. Here we will give a brief
  938. overview of these new interfaces; consult the Python documentation or the source
  939. code for complete details. The Python XML SIG is also working on improved
  940. documentation.</p>
  941. <section id="sax2-support">
  942. <h3>SAX2 Support<a class="headerlink" href="#sax2-support" title="Link to this heading">¶</a></h3>
  943. <p>SAX defines an event-driven interface for parsing XML. To use SAX, you must
  944. write a SAX handler class. Handler classes inherit from various classes
  945. provided by SAX, and override various methods that will then be called by the
  946. XML parser. For example, the <a class="reference internal" href="../library/xml.sax.handler.html#xml.sax.handler.ContentHandler.startElement" title="xml.sax.handler.ContentHandler.startElement"><code class="xref py py-meth docutils literal notranslate"><span class="pre">startElement()</span></code></a> and <a class="reference internal" href="../library/xml.sax.handler.html#xml.sax.handler.ContentHandler.endElement" title="xml.sax.handler.ContentHandler.endElement"><code class="xref py py-meth docutils literal notranslate"><span class="pre">endElement()</span></code></a>
  947. methods are called for every starting and end tag encountered by the parser, the
  948. <a class="reference internal" href="../library/xml.sax.handler.html#xml.sax.handler.ContentHandler.characters" title="xml.sax.handler.ContentHandler.characters"><code class="xref py py-meth docutils literal notranslate"><span class="pre">characters()</span></code></a> method is called for every chunk of character data, and so
  949. forth.</p>
  950. <p>The advantage of the event-driven approach is that the whole document doesn’t
  951. have to be resident in memory at any one time, which matters if you are
  952. processing really huge documents. However, writing the SAX handler class can
  953. get very complicated if you’re trying to modify the document structure in some
  954. elaborate way.</p>
  955. <p>For example, this little example program defines a handler that prints a message
  956. for every starting and ending tag, and then parses the file <code class="file docutils literal notranslate"><span class="pre">hamlet.xml</span></code>
  957. using it:</p>
  958. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml</span> <span class="kn">import</span> <span class="n">sax</span>
  959. <span class="k">class</span> <span class="nc">SimpleHandler</span><span class="p">(</span><span class="n">sax</span><span class="o">.</span><span class="n">ContentHandler</span><span class="p">):</span>
  960. <span class="k">def</span> <span class="nf">startElement</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
  961. <span class="nb">print</span> <span class="s1">&#39;Start of element:&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">attrs</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
  962. <span class="k">def</span> <span class="nf">endElement</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
  963. <span class="nb">print</span> <span class="s1">&#39;End of element:&#39;</span><span class="p">,</span> <span class="n">name</span>
  964. <span class="c1"># Create a parser object</span>
  965. <span class="n">parser</span> <span class="o">=</span> <span class="n">sax</span><span class="o">.</span><span class="n">make_parser</span><span class="p">()</span>
  966. <span class="c1"># Tell it what handler to use</span>
  967. <span class="n">handler</span> <span class="o">=</span> <span class="n">SimpleHandler</span><span class="p">()</span>
  968. <span class="n">parser</span><span class="o">.</span><span class="n">setContentHandler</span><span class="p">(</span> <span class="n">handler</span> <span class="p">)</span>
  969. <span class="c1"># Parse a file!</span>
  970. <span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span> <span class="s1">&#39;hamlet.xml&#39;</span> <span class="p">)</span>
  971. </pre></div>
  972. </div>
  973. <p>For more information, consult the Python documentation, or the XML HOWTO at
  974. <a class="reference external" href="https://pyxml.sourceforge.net/topics/howto/xml-howto.html">https://pyxml.sourceforge.net/topics/howto/xml-howto.html</a>.</p>
  975. </section>
  976. <section id="dom-support">
  977. <h3>DOM Support<a class="headerlink" href="#dom-support" title="Link to this heading">¶</a></h3>
  978. <p>The Document Object Model is a tree-based representation for an XML document. A
  979. top-level <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code> instance is the root of the tree, and has a single
  980. child which is the top-level <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> instance. This <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code>
  981. has children nodes representing character data and any sub-elements, which may
  982. have further children of their own, and so forth. Using the DOM you can
  983. traverse the resulting tree any way you like, access element and attribute
  984. values, insert and delete nodes, and convert the tree back into XML.</p>
  985. <p>The DOM is useful for modifying XML documents, because you can create a DOM
  986. tree, modify it by adding new nodes or rearranging subtrees, and then produce a
  987. new XML document as output. You can also construct a DOM tree manually and
  988. convert it to XML, which can be a more flexible way of producing XML output than
  989. simply writing <code class="docutils literal notranslate"><span class="pre">&lt;tag1&gt;</span></code>…<code class="docutils literal notranslate"><span class="pre">&lt;/tag1&gt;</span></code> to a file.</p>
  990. <p>The DOM implementation included with Python lives in the <a class="reference internal" href="../library/xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.minidom</span></code></a>
  991. module. It’s a lightweight implementation of the Level 1 DOM with support for
  992. XML namespaces. The <code class="xref py py-func docutils literal notranslate"><span class="pre">parse()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">parseString()</span></code> convenience
  993. functions are provided for generating a DOM tree:</p>
  994. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.dom</span> <span class="kn">import</span> <span class="n">minidom</span>
  995. <span class="n">doc</span> <span class="o">=</span> <span class="n">minidom</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;hamlet.xml&#39;</span><span class="p">)</span>
  996. </pre></div>
  997. </div>
  998. <p><code class="docutils literal notranslate"><span class="pre">doc</span></code> is a <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code> instance. <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code>, like all the other
  999. DOM classes such as <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">Text</span></code>, is a subclass of the
  1000. <code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code> base class. All the nodes in a DOM tree therefore support certain
  1001. common methods, such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">toxml()</span></code> which returns a string containing the XML
  1002. representation of the node and its children. Each class also has special
  1003. methods of its own; for example, <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code>
  1004. instances have a method to find all child elements with a given tag name.
  1005. Continuing from the previous 2-line example:</p>
  1006. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">perslist</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span> <span class="s1">&#39;PERSONA&#39;</span> <span class="p">)</span>
  1007. <span class="nb">print</span> <span class="n">perslist</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
  1008. <span class="nb">print</span> <span class="n">perslist</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
  1009. </pre></div>
  1010. </div>
  1011. <p>For the <em>Hamlet</em> XML file, the above few lines output:</p>
  1012. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">PERSONA</span><span class="o">&gt;</span><span class="n">CLAUDIUS</span><span class="p">,</span> <span class="n">king</span> <span class="n">of</span> <span class="n">Denmark</span><span class="o">.</span> <span class="o">&lt;/</span><span class="n">PERSONA</span><span class="o">&gt;</span>
  1013. <span class="o">&lt;</span><span class="n">PERSONA</span><span class="o">&gt;</span><span class="n">HAMLET</span><span class="p">,</span> <span class="n">son</span> <span class="n">to</span> <span class="n">the</span> <span class="n">late</span><span class="p">,</span> <span class="ow">and</span> <span class="n">nephew</span> <span class="n">to</span> <span class="n">the</span> <span class="n">present</span> <span class="n">king</span><span class="o">.&lt;/</span><span class="n">PERSONA</span><span class="o">&gt;</span>
  1014. </pre></div>
  1015. </div>
  1016. <p>The root element of the document is available as <code class="docutils literal notranslate"><span class="pre">doc.documentElement</span></code>, and
  1017. its children can be easily modified by deleting, adding, or removing nodes:</p>
  1018. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">root</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">documentElement</span>
  1019. <span class="c1"># Remove the first child</span>
  1020. <span class="n">root</span><span class="o">.</span><span class="n">removeChild</span><span class="p">(</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">)</span>
  1021. <span class="c1"># Move the new first child to the end</span>
  1022. <span class="n">root</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">)</span>
  1023. <span class="c1"># Insert the new first child (originally,</span>
  1024. <span class="c1"># the third child) before the 20th child.</span>
  1025. <span class="n">root</span><span class="o">.</span><span class="n">insertBefore</span><span class="p">(</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">20</span><span class="p">]</span> <span class="p">)</span>
  1026. </pre></div>
  1027. </div>
  1028. <p>Again, I will refer you to the Python documentation for a complete listing of
  1029. the different <code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code> classes and their various methods.</p>
  1030. </section>
  1031. <section id="relationship-to-pyxml">
  1032. <h3>Relationship to PyXML<a class="headerlink" href="#relationship-to-pyxml" title="Link to this heading">¶</a></h3>
  1033. <p>The XML Special Interest Group has been working on XML-related Python code for a
  1034. while. Its code distribution, called PyXML, is available from the SIG’s web
  1035. pages at <a class="reference external" href="https://www.python.org/community/sigs/current/xml-sig">https://www.python.org/community/sigs/current/xml-sig</a>. The PyXML distribution also used
  1036. the package name <code class="docutils literal notranslate"><span class="pre">xml</span></code>. If you’ve written programs that used PyXML, you’re
  1037. probably wondering about its compatibility with the 2.0 <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package.</p>
  1038. <p>The answer is that Python 2.0’s <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package isn’t compatible with PyXML,
  1039. but can be made compatible by installing a recent version PyXML. Many
  1040. applications can get by with the XML support that is included with Python 2.0,
  1041. but more complicated applications will require that the full PyXML package will
  1042. be installed. When installed, PyXML versions 0.6.0 or greater will replace the
  1043. <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package shipped with Python, and will be a strict superset of the
  1044. standard package, adding a bunch of additional features. Some of the additional
  1045. features in PyXML include:</p>
  1046. <ul class="simple">
  1047. <li><p>4DOM, a full DOM implementation from FourThought, Inc.</p></li>
  1048. <li><p>The xmlproc validating parser, written by Lars Marius Garshol.</p></li>
  1049. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">sgmlop</span></code> parser accelerator module, written by Fredrik Lundh.</p></li>
  1050. </ul>
  1051. </section>
  1052. </section>
  1053. <section id="module-changes">
  1054. <h2>Module changes<a class="headerlink" href="#module-changes" title="Link to this heading">¶</a></h2>
  1055. <p>Lots of improvements and bugfixes were made to Python’s extensive standard
  1056. library; some of the affected modules include <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a>,
  1057. <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code></a>, <a class="reference internal" href="../library/cgi.html#module-cgi" title="cgi: Helpers for running Python scripts via the Common Gateway Interface. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cgi</span></code></a>, <a class="reference internal" href="../library/calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a>, <a class="reference internal" href="../library/posix.html#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">posix</span></code></a>, <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a>,
  1058. <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmllib</span></code>, <a class="reference internal" href="../library/aifc.html#module-aifc" title="aifc: Read and write audio files in AIFF or AIFC format. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">aifc</span></code></a>, <a class="reference internal" href="../library/chunk.html#module-chunk" title="chunk: Module to read IFF chunks. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">chunk</span></code></a> <a class="reference internal" href="../library/wave.html#module-wave" title="wave: Provide an interface to the WAV sound format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wave</span></code></a>, <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a>, <a class="reference internal" href="../library/shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a>,
  1059. and <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets). (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code></a>. Consult the CVS logs for the exact patch-by-patch details.</p>
  1060. <p>Brian Gallew contributed OpenSSL support for the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module. OpenSSL
  1061. is an implementation of the Secure Socket Layer, which encrypts the data being
  1062. sent over a socket. When compiling Python, you can edit <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code>
  1063. to include SSL support, which adds an additional function to the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a>
  1064. module: <code class="docutils literal notranslate"><span class="pre">socket.ssl(socket,</span> <span class="pre">keyfile,</span> <span class="pre">certfile)</span></code>, which takes a socket
  1065. object and returns an SSL socket. The <a class="reference internal" href="../library/http.html#module-http" title="http: HTTP status codes and messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code></a> and <a class="reference internal" href="../library/urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> modules
  1066. were also changed to support <code class="docutils literal notranslate"><span class="pre">https://</span></code> URLs, though no one has implemented
  1067. FTP or SMTP over SSL.</p>
  1068. <p>The <a class="reference internal" href="../library/http.html#module-http" title="http: HTTP status codes and messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code></a> module has been rewritten by Greg Stein to support HTTP/1.1.</p>
  1069. <p>Backward compatibility with the 1.5 version of <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> is provided,
  1070. though using HTTP/1.1 features such as pipelining will require rewriting code to
  1071. use a different set of interfaces.</p>
  1072. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">Tkinter</span></code> module now supports Tcl/Tk version 8.1, 8.2, or 8.3, and
  1073. support for the older 7.x versions has been dropped. The Tkinter module now
  1074. supports displaying Unicode strings in Tk widgets. Also, Fredrik Lundh
  1075. contributed an optimization which makes operations like <code class="docutils literal notranslate"><span class="pre">create_line</span></code> and
  1076. <code class="docutils literal notranslate"><span class="pre">create_polygon</span></code> much faster, especially when using lots of coordinates.</p>
  1077. <p>The <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> module has been greatly extended, starting from Oliver
  1078. Andrich’s enhanced version, to provide many additional functions from ncurses
  1079. and SYSV curses, such as colour, alternative character set support, pads, and
  1080. mouse support. This means the module is no longer compatible with operating
  1081. systems that only have BSD curses, but there don’t seem to be any currently
  1082. maintained OSes that fall into this category.</p>
  1083. <p>As mentioned in the earlier discussion of 2.0’s Unicode support, the underlying
  1084. implementation of the regular expressions provided by the <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module has
  1085. been changed. SRE, a new regular expression engine written by Fredrik Lundh and
  1086. partially funded by Hewlett Packard, supports matching against both 8-bit
  1087. strings and Unicode strings.</p>
  1088. </section>
  1089. <section id="new-modules">
  1090. <h2>New modules<a class="headerlink" href="#new-modules" title="Link to this heading">¶</a></h2>
  1091. <p>A number of new modules were added. We’ll simply list them with brief
  1092. descriptions; consult the 2.0 documentation for the details of a particular
  1093. module.</p>
  1094. <ul class="simple">
  1095. <li><p><a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a>: For registering functions to be called before the Python
  1096. interpreter exits. Code that currently sets <code class="docutils literal notranslate"><span class="pre">sys.exitfunc</span></code> directly should be
  1097. changed to use the <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> module instead, importing <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> and
  1098. calling <a class="reference internal" href="../library/atexit.html#atexit.register" title="atexit.register"><code class="xref py py-func docutils literal notranslate"><span class="pre">atexit.register()</span></code></a> with the function to be called on exit.
  1099. (Contributed by Skip Montanaro.)</p></li>
  1100. <li><p><a class="reference internal" href="../library/codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings</span></code>, <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a>: Added as part of the new
  1101. Unicode support.</p></li>
  1102. <li><p><a class="reference internal" href="../library/filecmp.html#module-filecmp" title="filecmp: Compare files efficiently."><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></code></a>: Supersedes the old <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmp</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmpcache</span></code> and
  1103. <code class="xref py py-mod docutils literal notranslate"><span class="pre">dircmp</span></code> modules, which have now become deprecated. (Contributed by Gordon
  1104. MacMillan and Moshe Zadka.)</p></li>
  1105. <li><p><a class="reference internal" href="../library/gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a>: This module provides internationalization (I18N) and
  1106. localization (L10N) support for Python programs by providing an interface to the
  1107. GNU gettext message catalog library. (Integrated by Barry Warsaw, from separate
  1108. contributions by Martin von Löwis, Peter Funk, and James Henstridge.)</p></li>
  1109. <li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">linuxaudiodev</span></code>: Support for the <code class="file docutils literal notranslate"><span class="pre">/dev/audio</span></code> device on Linux, a
  1110. twin to the existing <code class="xref py py-mod docutils literal notranslate"><span class="pre">sunaudiodev</span></code> module. (Contributed by Peter Bosch,
  1111. with fixes by Jeremy Hylton.)</p></li>
  1112. <li><p><a class="reference internal" href="../library/mmap.html#module-mmap" title="mmap: Interface to memory-mapped files for Unix and Windows."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code></a>: An interface to memory-mapped files on both Windows and Unix. A
  1113. file’s contents can be mapped directly into memory, at which point it behaves
  1114. like a mutable string, so its contents can be read and modified. They can even
  1115. be passed to functions that expect ordinary strings, such as the <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a>
  1116. module. (Contributed by Sam Rushing, with some extensions by A.M. Kuchling.)</p></li>
  1117. <li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code>: An interface to the Expat XML parser. (Contributed by Paul
  1118. Prescod.)</p></li>
  1119. <li><p><a class="reference internal" href="../library/urllib.robotparser.html#module-urllib.robotparser" title="urllib.robotparser: Load a robots.txt file and answer questions about fetchability of other URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">robotparser</span></code></a>: Parse a <code class="file docutils literal notranslate"><span class="pre">robots.txt</span></code> file, which is used for writing
  1120. web spiders that politely avoid certain areas of a web site. The parser accepts
  1121. the contents of a <code class="file docutils literal notranslate"><span class="pre">robots.txt</span></code> file, builds a set of rules from it, and
  1122. can then answer questions about the fetchability of a given URL. (Contributed
  1123. by Skip Montanaro.)</p></li>
  1124. <li><p><a class="reference internal" href="../library/tabnanny.html#module-tabnanny" title="tabnanny: Tool for detecting white space related problems in Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tabnanny</span></code></a>: A module/script to check Python source code for ambiguous
  1125. indentation. (Contributed by Tim Peters.)</p></li>
  1126. <li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">UserString</span></code>: A base class useful for deriving objects that behave like
  1127. strings.</p></li>
  1128. <li><p><a class="reference internal" href="../library/webbrowser.html#module-webbrowser" title="webbrowser: Easy-to-use controller for web browsers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code></a>: A module that provides a platform independent way to launch
  1129. a web browser on a specific URL. For each platform, various browsers are tried
  1130. in a specific order. The user can alter which browser is launched by setting the
  1131. <em>BROWSER</em> environment variable. (Originally inspired by Eric S. Raymond’s patch
  1132. to <a class="reference internal" href="../library/urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> which added similar functionality, but the final module comes
  1133. from code originally implemented by Fred Drake as
  1134. <code class="file docutils literal notranslate"><span class="pre">Tools/idle/BrowserControl.py</span></code>, and adapted for the standard library by
  1135. Fred.)</p></li>
  1136. <li><p><a class="reference internal" href="../library/winreg.html#module-winreg" title="winreg: Routines and objects for manipulating the Windows registry. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code></a>: An interface to the Windows registry. <code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code> is an
  1137. adaptation of functions that have been part of PythonWin since 1995, but has now
  1138. been added to the core distribution, and enhanced to support Unicode.
  1139. <code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code> was written by Bill Tutt and Mark Hammond.</p></li>
  1140. <li><p><a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a>: A module for reading and writing ZIP-format archives. These
  1141. are archives produced by <strong class="program">PKZIP</strong> on DOS/Windows or <strong class="program">zip</strong> on
  1142. Unix, not to be confused with <strong class="program">gzip</strong>-format files (which are
  1143. supported by the <a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> module) (Contributed by James C. Ahlstrom.)</p></li>
  1144. <li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">imputil</span></code>: A module that provides a simpler way for writing customized
  1145. import hooks, in comparison to the existing <code class="xref py py-mod docutils literal notranslate"><span class="pre">ihooks</span></code> module. (Implemented
  1146. by Greg Stein, with much discussion on python-dev along the way.)</p></li>
  1147. </ul>
  1148. </section>
  1149. <section id="idle-improvements">
  1150. <h2>IDLE Improvements<a class="headerlink" href="#idle-improvements" title="Link to this heading">¶</a></h2>
  1151. <p>IDLE is the official Python cross-platform IDE, written using Tkinter. Python
  1152. 2.0 includes IDLE 0.6, which adds a number of new features and improvements. A
  1153. partial list:</p>
  1154. <ul class="simple">
  1155. <li><p>UI improvements and optimizations, especially in the area of syntax
  1156. highlighting and auto-indentation.</p></li>
  1157. <li><p>The class browser now shows more information, such as the top level functions
  1158. in a module.</p></li>
  1159. <li><p>Tab width is now a user settable option. When opening an existing Python file,
  1160. IDLE automatically detects the indentation conventions, and adapts.</p></li>
  1161. <li><p>There is now support for calling browsers on various platforms, used to open
  1162. the Python documentation in a browser.</p></li>
  1163. <li><p>IDLE now has a command line, which is largely similar to the vanilla Python
  1164. interpreter.</p></li>
  1165. <li><p>Call tips were added in many places.</p></li>
  1166. <li><p>IDLE can now be installed as a package.</p></li>
  1167. <li><p>In the editor window, there is now a line/column bar at the bottom.</p></li>
  1168. <li><p>Three new keystroke commands: Check module (<kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Alt</kbd>-<kbd class="kbd docutils literal notranslate">F5</kbd></kbd>), Import module (<kbd class="kbd docutils literal notranslate">F5</kbd>) and
  1169. Run script (<kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">F5</kbd></kbd>).</p></li>
  1170. </ul>
  1171. </section>
  1172. <section id="deleted-and-deprecated-modules">
  1173. <h2>Deleted and Deprecated Modules<a class="headerlink" href="#deleted-and-deprecated-modules" title="Link to this heading">¶</a></h2>
  1174. <p>A few modules have been dropped because they’re obsolete, or because there are
  1175. now better ways to do the same thing. The <code class="xref py py-mod docutils literal notranslate"><span class="pre">stdwin</span></code> module is gone; it was
  1176. for a platform-independent windowing toolkit that’s no longer developed.</p>
  1177. <p>A number of modules have been moved to the <code class="file docutils literal notranslate"><span class="pre">lib-old</span></code> subdirectory:
  1178. <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmp</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmpcache</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">dircmp</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">dump</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">find</span></code>,
  1179. <code class="xref py py-mod docutils literal notranslate"><span class="pre">grep</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">packmail</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">poly</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">util</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">whatsound</span></code>,
  1180. <code class="xref py py-mod docutils literal notranslate"><span class="pre">zmod</span></code>. If you have code which relies on a module that’s been moved to
  1181. <code class="file docutils literal notranslate"><span class="pre">lib-old</span></code>, you can simply add that directory to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> to get them
  1182. back, but you’re encouraged to update any code that uses these modules.</p>
  1183. </section>
  1184. <section id="acknowledgements">
  1185. <h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Link to this heading">¶</a></h2>
  1186. <p>The authors would like to thank the following people for offering suggestions on
  1187. various drafts of this article: David Bolen, Mark Hammond, Gregg Hauser, Jeremy
  1188. Hylton, Fredrik Lundh, Detlef Lannert, Aahz Maruch, Skip Montanaro, Vladimir
  1189. Marangozov, Tobias Polzin, Guido van Rossum, Neil Schemenauer, and Russ Schmidt.</p>
  1190. </section>
  1191. </section>
  1192. <div class="clearer"></div>
  1193. </div>
  1194. </div>
  1195. </div>
  1196. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1197. <div class="sphinxsidebarwrapper">
  1198. <div>
  1199. <h3><a href="../contents.html">Table of Contents</a></h3>
  1200. <ul>
  1201. <li><a class="reference internal" href="#">What’s New in Python 2.0</a><ul>
  1202. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  1203. <li><a class="reference internal" href="#what-about-python-1-6">What About Python 1.6?</a></li>
  1204. <li><a class="reference internal" href="#new-development-process">New Development Process</a></li>
  1205. <li><a class="reference internal" href="#unicode">Unicode</a></li>
  1206. <li><a class="reference internal" href="#list-comprehensions">List Comprehensions</a></li>
  1207. <li><a class="reference internal" href="#augmented-assignment">Augmented Assignment</a></li>
  1208. <li><a class="reference internal" href="#string-methods">String Methods</a></li>
  1209. <li><a class="reference internal" href="#garbage-collection-of-cycles">Garbage Collection of Cycles</a></li>
  1210. <li><a class="reference internal" href="#other-core-changes">Other Core Changes</a><ul>
  1211. <li><a class="reference internal" href="#minor-language-changes">Minor Language Changes</a></li>
  1212. <li><a class="reference internal" href="#changes-to-built-in-functions">Changes to Built-in Functions</a></li>
  1213. </ul>
  1214. </li>
  1215. <li><a class="reference internal" href="#porting-to-2-0">Porting to 2.0</a></li>
  1216. <li><a class="reference internal" href="#extending-embedding-changes">Extending/Embedding Changes</a></li>
  1217. <li><a class="reference internal" href="#distutils-making-modules-easy-to-install">Distutils: Making Modules Easy to Install</a></li>
  1218. <li><a class="reference internal" href="#xml-modules">XML Modules</a><ul>
  1219. <li><a class="reference internal" href="#sax2-support">SAX2 Support</a></li>
  1220. <li><a class="reference internal" href="#dom-support">DOM Support</a></li>
  1221. <li><a class="reference internal" href="#relationship-to-pyxml">Relationship to PyXML</a></li>
  1222. </ul>
  1223. </li>
  1224. <li><a class="reference internal" href="#module-changes">Module changes</a></li>
  1225. <li><a class="reference internal" href="#new-modules">New modules</a></li>
  1226. <li><a class="reference internal" href="#idle-improvements">IDLE Improvements</a></li>
  1227. <li><a class="reference internal" href="#deleted-and-deprecated-modules">Deleted and Deprecated Modules</a></li>
  1228. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  1229. </ul>
  1230. </li>
  1231. </ul>
  1232. </div>
  1233. <div>
  1234. <h4>Previous topic</h4>
  1235. <p class="topless"><a href="2.1.html"
  1236. title="previous chapter">What’s New in Python 2.1</a></p>
  1237. </div>
  1238. <div>
  1239. <h4>Next topic</h4>
  1240. <p class="topless"><a href="changelog.html"
  1241. title="next chapter">Changelog</a></p>
  1242. </div>
  1243. <div role="note" aria-label="source link">
  1244. <h3>This Page</h3>
  1245. <ul class="this-page-menu">
  1246. <li><a href="../bugs.html">Report a Bug</a></li>
  1247. <li>
  1248. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.0.rst"
  1249. rel="nofollow">Show Source
  1250. </a>
  1251. </li>
  1252. </ul>
  1253. </div>
  1254. </div>
  1255. <div id="sidebarbutton" title="Collapse sidebar">
  1256. <span>«</span>
  1257. </div>
  1258. </div>
  1259. <div class="clearer"></div>
  1260. </div>
  1261. <div class="related" role="navigation" aria-label="related navigation">
  1262. <h3>Navigation</h3>
  1263. <ul>
  1264. <li class="right" style="margin-right: 10px">
  1265. <a href="../genindex.html" title="General Index"
  1266. >index</a></li>
  1267. <li class="right" >
  1268. <a href="../py-modindex.html" title="Python Module Index"
  1269. >modules</a> |</li>
  1270. <li class="right" >
  1271. <a href="changelog.html" title="Changelog"
  1272. >next</a> |</li>
  1273. <li class="right" >
  1274. <a href="2.1.html" title="What’s New in Python 2.1"
  1275. >previous</a> |</li>
  1276. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1277. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1278. <li class="switchers">
  1279. <div class="language_switcher_placeholder"></div>
  1280. <div class="version_switcher_placeholder"></div>
  1281. </li>
  1282. <li>
  1283. </li>
  1284. <li id="cpython-language-and-version">
  1285. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1286. </li>
  1287. <li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> &#187;</li>
  1288. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.0</a></li>
  1289. <li class="right">
  1290. <div class="inline-search" role="search">
  1291. <form class="inline-search" action="../search.html" method="get">
  1292. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1293. <input type="submit" value="Go" />
  1294. </form>
  1295. </div>
  1296. |
  1297. </li>
  1298. <li class="right">
  1299. <label class="theme-selector-label">
  1300. Theme
  1301. <select class="theme-selector" oninput="activateTheme(this.value)">
  1302. <option value="auto" selected>Auto</option>
  1303. <option value="light">Light</option>
  1304. <option value="dark">Dark</option>
  1305. </select>
  1306. </label> |</li>
  1307. </ul>
  1308. </div>
  1309. <div class="footer">
  1310. &copy;
  1311. <a href="../copyright.html">
  1312. Copyright
  1313. </a>
  1314. 2001-2024, Python Software Foundation.
  1315. <br />
  1316. This page is licensed under the Python Software Foundation License Version 2.
  1317. <br />
  1318. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1319. <br />
  1320. See <a href="/license.html">History and License</a> for more information.<br />
  1321. <br />
  1322. The Python Software Foundation is a non-profit corporation.
  1323. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1324. <br />
  1325. <br />
  1326. Last updated on Apr 09, 2024 (13:47 UTC).
  1327. <a href="/bugs.html">Found a bug</a>?
  1328. <br />
  1329. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1330. </div>
  1331. </body>
  1332. </html>
上海开阖软件有限公司 沪ICP备12045867号-1