2.4.html 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="What’s New in Python 2.4" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/whatsnew/2.4.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Author, A.M. Kuchling,. This article explains the new features in Python 2.4.1, released on March 30, 2005. Python 2.4 is a medium-sized release. It doesn’t introduce as many changes as the radical..." />
  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,. This article explains the new features in Python 2.4.1, released on March 30, 2005. Python 2.4 is a medium-sized release. It doesn’t introduce as many changes as the radical..." />
  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.4 &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="What’s New in Python 2.3" href="2.3.html" />
  34. <link rel="prev" title="What’s New in Python 2.5" href="2.5.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/whatsnew/2.4.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.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="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.4</a><ul>
  86. <li><a class="reference internal" href="#pep-218-built-in-set-objects">PEP 218: Built-In Set Objects</a></li>
  87. <li><a class="reference internal" href="#pep-237-unifying-long-integers-and-integers">PEP 237: Unifying Long Integers and Integers</a></li>
  88. <li><a class="reference internal" href="#pep-289-generator-expressions">PEP 289: Generator Expressions</a></li>
  89. <li><a class="reference internal" href="#pep-292-simpler-string-substitutions">PEP 292: Simpler String Substitutions</a></li>
  90. <li><a class="reference internal" href="#pep-318-decorators-for-functions-and-methods">PEP 318: Decorators for Functions and Methods</a></li>
  91. <li><a class="reference internal" href="#pep-322-reverse-iteration">PEP 322: Reverse Iteration</a></li>
  92. <li><a class="reference internal" href="#pep-324-new-subprocess-module">PEP 324: New subprocess Module</a></li>
  93. <li><a class="reference internal" href="#pep-327-decimal-data-type">PEP 327: Decimal Data Type</a><ul>
  94. <li><a class="reference internal" href="#why-is-decimal-needed">Why is Decimal needed?</a></li>
  95. <li><a class="reference internal" href="#the-decimal-type">The <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> type</a></li>
  96. <li><a class="reference internal" href="#the-context-type">The <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> type</a></li>
  97. </ul>
  98. </li>
  99. <li><a class="reference internal" href="#pep-328-multi-line-imports">PEP 328: Multi-line Imports</a></li>
  100. <li><a class="reference internal" href="#pep-331-locale-independent-float-string-conversions">PEP 331: Locale-Independent Float/String Conversions</a></li>
  101. <li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
  102. <li><a class="reference internal" href="#optimizations">Optimizations</a></li>
  103. </ul>
  104. </li>
  105. <li><a class="reference internal" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a><ul>
  106. <li><a class="reference internal" href="#cookielib">cookielib</a></li>
  107. <li><a class="reference internal" href="#doctest">doctest</a></li>
  108. </ul>
  109. </li>
  110. <li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
  111. <li><a class="reference internal" href="#port-specific-changes">Port-Specific Changes</a></li>
  112. </ul>
  113. </li>
  114. <li><a class="reference internal" href="#porting-to-python-2-4">Porting to Python 2.4</a></li>
  115. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  116. </ul>
  117. </li>
  118. </ul>
  119. </div>
  120. <div>
  121. <h4>Previous topic</h4>
  122. <p class="topless"><a href="2.5.html"
  123. title="previous chapter">What’s New in Python 2.5</a></p>
  124. </div>
  125. <div>
  126. <h4>Next topic</h4>
  127. <p class="topless"><a href="2.3.html"
  128. title="next chapter">What’s New in Python 2.3</a></p>
  129. </div>
  130. <div role="note" aria-label="source link">
  131. <h3>This Page</h3>
  132. <ul class="this-page-menu">
  133. <li><a href="../bugs.html">Report a Bug</a></li>
  134. <li>
  135. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.4.rst"
  136. rel="nofollow">Show Source
  137. </a>
  138. </li>
  139. </ul>
  140. </div>
  141. </nav>
  142. </div>
  143. </div>
  144. <div class="related" role="navigation" aria-label="related navigation">
  145. <h3>Navigation</h3>
  146. <ul>
  147. <li class="right" style="margin-right: 10px">
  148. <a href="../genindex.html" title="General Index"
  149. accesskey="I">index</a></li>
  150. <li class="right" >
  151. <a href="../py-modindex.html" title="Python Module Index"
  152. >modules</a> |</li>
  153. <li class="right" >
  154. <a href="2.3.html" title="What’s New in Python 2.3"
  155. accesskey="N">next</a> |</li>
  156. <li class="right" >
  157. <a href="2.5.html" title="What’s New in Python 2.5"
  158. accesskey="P">previous</a> |</li>
  159. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  160. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  161. <li class="switchers">
  162. <div class="language_switcher_placeholder"></div>
  163. <div class="version_switcher_placeholder"></div>
  164. </li>
  165. <li>
  166. </li>
  167. <li id="cpython-language-and-version">
  168. <a href="../index.html">3.12.0 Documentation</a> &#187;
  169. </li>
  170. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> &#187;</li>
  171. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.4</a></li>
  172. <li class="right">
  173. <div class="inline-search" role="search">
  174. <form class="inline-search" action="../search.html" method="get">
  175. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  176. <input type="submit" value="Go" />
  177. </form>
  178. </div>
  179. |
  180. </li>
  181. <li class="right">
  182. <label class="theme-selector-label">
  183. Theme
  184. <select class="theme-selector" oninput="activateTheme(this.value)">
  185. <option value="auto" selected>Auto</option>
  186. <option value="light">Light</option>
  187. <option value="dark">Dark</option>
  188. </select>
  189. </label> |</li>
  190. </ul>
  191. </div>
  192. <div class="document">
  193. <div class="documentwrapper">
  194. <div class="bodywrapper">
  195. <div class="body" role="main">
  196. <section id="what-s-new-in-python-2-4">
  197. <h1>What’s New in Python 2.4<a class="headerlink" href="#what-s-new-in-python-2-4" title="Permalink to this headline">¶</a></h1>
  198. <dl class="field-list simple">
  199. <dt class="field-odd">Author</dt>
  200. <dd class="field-odd"><p>A.M. Kuchling</p>
  201. </dd>
  202. </dl>
  203. <p>This article explains the new features in Python 2.4.1, released on March 30,
  204. 2005.</p>
  205. <p>Python 2.4 is a medium-sized release. It doesn’t introduce as many changes as
  206. the radical Python 2.2, but introduces more features than the conservative 2.3
  207. release. The most significant new language features are function decorators and
  208. generator expressions; most other changes are to the standard library.</p>
  209. <p>According to the CVS change logs, there were 481 patches applied and 502 bugs
  210. fixed between Python 2.3 and 2.4. Both figures are likely to be underestimates.</p>
  211. <p>This article doesn’t attempt to provide a complete specification of every single
  212. new feature, but instead provides a brief introduction to each feature. For
  213. full details, you should refer to the documentation for Python 2.4, such as the
  214. Python Library Reference and the Python Reference Manual. Often you will be
  215. referred to the PEP for a particular new feature for explanations of the
  216. implementation and design rationale.</p>
  217. <section id="pep-218-built-in-set-objects">
  218. <h2>PEP 218: Built-In Set Objects<a class="headerlink" href="#pep-218-built-in-set-objects" title="Permalink to this headline">¶</a></h2>
  219. <p>Python 2.3 introduced the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sets</span></code> module. C implementations of set data
  220. types have now been added to the Python core as two new built-in types,
  221. <code class="docutils literal notranslate"><span class="pre">set(iterable)</span></code> and <code class="docutils literal notranslate"><span class="pre">frozenset(iterable)</span></code>. They provide high speed
  222. operations for membership testing, for eliminating duplicates from sequences,
  223. and for mathematical operations like unions, intersections, differences, and
  224. symmetric differences.</p>
  225. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;abracadabra&#39;</span><span class="p">)</span> <span class="c1"># form a set from a string</span>
  226. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;z&#39;</span> <span class="ow">in</span> <span class="n">a</span> <span class="c1"># fast membership testing</span>
  227. <span class="go">False</span>
  228. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="c1"># unique letters in a</span>
  229. <span class="go">set([&#39;a&#39;, &#39;r&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;])</span>
  230. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1"># convert back into a string</span>
  231. <span class="go">&#39;arbcd&#39;</span>
  232. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;alacazam&#39;</span><span class="p">)</span> <span class="c1"># form a second set</span>
  233. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span> <span class="c1"># letters in a but not in b</span>
  234. <span class="go">set([&#39;r&#39;, &#39;d&#39;, &#39;b&#39;])</span>
  235. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">|</span> <span class="n">b</span> <span class="c1"># letters in either a or b</span>
  236. <span class="go">set([&#39;a&#39;, &#39;c&#39;, &#39;r&#39;, &#39;d&#39;, &#39;b&#39;, &#39;m&#39;, &#39;z&#39;, &#39;l&#39;])</span>
  237. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">&amp;</span> <span class="n">b</span> <span class="c1"># letters in both a and b</span>
  238. <span class="go">set([&#39;a&#39;, &#39;c&#39;])</span>
  239. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">^</span> <span class="n">b</span> <span class="c1"># letters in a or b but not both</span>
  240. <span class="go">set([&#39;r&#39;, &#39;d&#39;, &#39;b&#39;, &#39;m&#39;, &#39;z&#39;, &#39;l&#39;])</span>
  241. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">)</span> <span class="c1"># add a new element</span>
  242. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s1">&#39;wxy&#39;</span><span class="p">)</span> <span class="c1"># add multiple new elements</span>
  243. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
  244. <span class="go">set([&#39;a&#39;, &#39;c&#39;, &#39;b&#39;, &#39;d&#39;, &#39;r&#39;, &#39;w&#39;, &#39;y&#39;, &#39;x&#39;, &#39;z&#39;])</span>
  245. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span> <span class="c1"># take one element out</span>
  246. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
  247. <span class="go">set([&#39;a&#39;, &#39;c&#39;, &#39;b&#39;, &#39;d&#39;, &#39;r&#39;, &#39;w&#39;, &#39;y&#39;, &#39;z&#39;])</span>
  248. </pre></div>
  249. </div>
  250. <p>The <a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><code class="xref py py-func docutils literal notranslate"><span class="pre">frozenset()</span></code></a> type is an immutable version of <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a>. Since it is
  251. immutable and hashable, it may be used as a dictionary key or as a member of
  252. another set.</p>
  253. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">sets</span></code> module remains in the standard library, and may be useful if you
  254. wish to subclass the <code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">ImmutableSet</span></code> classes. There are
  255. currently no plans to deprecate the module.</p>
  256. <div class="admonition seealso">
  257. <p class="admonition-title">See also</p>
  258. <dl class="simple">
  259. <dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0218/"><strong>PEP 218</strong></a> - Adding a Built-In Set Object Type</dt><dd><p>Originally proposed by Greg Wilson and ultimately implemented by Raymond
  260. Hettinger.</p>
  261. </dd>
  262. </dl>
  263. </div>
  264. </section>
  265. <section id="pep-237-unifying-long-integers-and-integers">
  266. <h2>PEP 237: Unifying Long Integers and Integers<a class="headerlink" href="#pep-237-unifying-long-integers-and-integers" title="Permalink to this headline">¶</a></h2>
  267. <p>The lengthy transition process for this PEP, begun in Python 2.2, takes another
  268. step forward in Python 2.4. In 2.3, certain integer operations that would
  269. behave differently after int/long unification triggered <a class="reference internal" href="../library/exceptions.html#FutureWarning" title="FutureWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FutureWarning</span></code></a>
  270. warnings and returned values limited to 32 or 64 bits (depending on your
  271. platform). In 2.4, these expressions no longer produce a warning and instead
  272. produce a different result that’s usually a long integer.</p>
  273. <p>The problematic expressions are primarily left shifts and lengthy hexadecimal
  274. and octal constants. For example, <code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">&lt;&lt;</span> <span class="pre">32</span></code> results in a warning in 2.3,
  275. evaluating to 0 on 32-bit platforms. In Python 2.4, this expression now returns
  276. the correct answer, 8589934592.</p>
  277. <div class="admonition seealso">
  278. <p class="admonition-title">See also</p>
  279. <dl class="simple">
  280. <dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0237/"><strong>PEP 237</strong></a> - Unifying Long Integers and Integers</dt><dd><p>Original PEP written by Moshe Zadka and GvR. The changes for 2.4 were
  281. implemented by Kalle Svensson.</p>
  282. </dd>
  283. </dl>
  284. </div>
  285. </section>
  286. <section id="pep-289-generator-expressions">
  287. <h2>PEP 289: Generator Expressions<a class="headerlink" href="#pep-289-generator-expressions" title="Permalink to this headline">¶</a></h2>
  288. <p>The iterator feature introduced in Python 2.2 and the <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module
  289. make it easier to write programs that loop through large data sets without
  290. having the entire data set in memory at one time. List comprehensions don’t fit
  291. into this picture very well because they produce a Python list object containing
  292. all of the items. This unavoidably pulls all of the objects into memory, which
  293. can be a problem if your data set is very large. When trying to write a
  294. functionally styled program, it would be natural to write something like:</p>
  295. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">links</span> <span class="o">=</span> <span class="p">[</span><span class="n">link</span> <span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">get_all_links</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">link</span><span class="o">.</span><span class="n">followed</span><span class="p">]</span>
  296. <span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">links</span><span class="p">:</span>
  297. <span class="o">...</span>
  298. </pre></div>
  299. </div>
  300. <p>instead of</p>
  301. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">get_all_links</span><span class="p">():</span>
  302. <span class="k">if</span> <span class="n">link</span><span class="o">.</span><span class="n">followed</span><span class="p">:</span>
  303. <span class="k">continue</span>
  304. <span class="o">...</span>
  305. </pre></div>
  306. </div>
  307. <p>The first form is more concise and perhaps more readable, but if you’re dealing
  308. with a large number of link objects you’d have to write the second form to avoid
  309. having all link objects in memory at the same time.</p>
  310. <p>Generator expressions work similarly to list comprehensions but don’t
  311. materialize the entire list; instead they create a generator that will return
  312. elements one by one. The above example could be written as:</p>
  313. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">links</span> <span class="o">=</span> <span class="p">(</span><span class="n">link</span> <span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">get_all_links</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">link</span><span class="o">.</span><span class="n">followed</span><span class="p">)</span>
  314. <span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">links</span><span class="p">:</span>
  315. <span class="o">...</span>
  316. </pre></div>
  317. </div>
  318. <p>Generator expressions always have to be written inside parentheses, as in the
  319. above example. The parentheses signalling a function call also count, so if you
  320. want to create an iterator that will be immediately passed to a function you
  321. could write:</p>
  322. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span> <span class="nb">sum</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">count</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">list_all_objects</span><span class="p">())</span>
  323. </pre></div>
  324. </div>
  325. <p>Generator expressions differ from list comprehensions in various small ways.
  326. Most notably, the loop variable (<em>obj</em> in the above example) is not accessible
  327. outside of the generator expression. List comprehensions leave the variable
  328. assigned to its last value; future versions of Python will change this, making
  329. list comprehensions match generator expressions in this respect.</p>
  330. <div class="admonition seealso">
  331. <p class="admonition-title">See also</p>
  332. <dl class="simple">
  333. <dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0289/"><strong>PEP 289</strong></a> - Generator Expressions</dt><dd><p>Proposed by Raymond Hettinger and implemented by Jiwon Seo with early efforts
  334. steered by Hye-Shik Chang.</p>
  335. </dd>
  336. </dl>
  337. </div>
  338. </section>
  339. <section id="pep-292-simpler-string-substitutions">
  340. <h2>PEP 292: Simpler String Substitutions<a class="headerlink" href="#pep-292-simpler-string-substitutions" title="Permalink to this headline">¶</a></h2>
  341. <p>Some new classes in the standard library provide an alternative mechanism for
  342. substituting variables into strings; this style of substitution may be better
  343. for applications where untrained users need to edit templates.</p>
  344. <p>The usual way of substituting variables by name is the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator:</p>
  345. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">%(page)i</span><span class="s1">: </span><span class="si">%(title)s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;page&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="s1">&#39;The Best of Times&#39;</span><span class="p">}</span>
  346. <span class="go">&#39;2: The Best of Times&#39;</span>
  347. </pre></div>
  348. </div>
  349. <p>When writing the template string, it can be easy to forget the <code class="docutils literal notranslate"><span class="pre">i</span></code> or <code class="docutils literal notranslate"><span class="pre">s</span></code>
  350. after the closing parenthesis. This isn’t a big problem if the template is in a
  351. Python module, because you run the code, get an “Unsupported format character”
  352. <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, and fix the problem. However, consider an application such
  353. as Mailman where template strings or translations are being edited by users who
  354. aren’t aware of the Python language. The format string’s syntax is complicated
  355. to explain to such users, and if they make a mistake, it’s difficult to provide
  356. helpful feedback to them.</p>
  357. <p>PEP 292 adds a <code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code> class to 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 that uses
  358. <code class="docutils literal notranslate"><span class="pre">$</span></code> to indicate a substitution:</p>
  359. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">string</span>
  360. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$page: $title&#39;</span><span class="p">)</span>
  361. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">substitute</span><span class="p">({</span><span class="s1">&#39;page&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="s1">&#39;The Best of Times&#39;</span><span class="p">})</span>
  362. <span class="go">&#39;2: The Best of Times&#39;</span>
  363. </pre></div>
  364. </div>
  365. <p>If a key is missing from the dictionary, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code> method will
  366. raise a <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>. There’s also a <code class="xref py py-meth docutils literal notranslate"><span class="pre">safe_substitute()</span></code> method that
  367. ignores missing keys:</p>
  368. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$page: $title&#39;</span><span class="p">)</span>
  369. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">safe_substitute</span><span class="p">({</span><span class="s1">&#39;page&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">})</span>
  370. <span class="go">&#39;3: $title&#39;</span>
  371. </pre></div>
  372. </div>
  373. <div class="admonition seealso">
  374. <p class="admonition-title">See also</p>
  375. <dl class="simple">
  376. <dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0292/"><strong>PEP 292</strong></a> - Simpler String Substitutions</dt><dd><p>Written and implemented by Barry Warsaw.</p>
  377. </dd>
  378. </dl>
  379. </div>
  380. </section>
  381. <section id="pep-318-decorators-for-functions-and-methods">
  382. <h2>PEP 318: Decorators for Functions and Methods<a class="headerlink" href="#pep-318-decorators-for-functions-and-methods" title="Permalink to this headline">¶</a></h2>
  383. <p>Python 2.2 extended Python’s object model by adding static methods and class
  384. methods, but it didn’t extend Python’s syntax to provide any new way of defining
  385. static or class methods. Instead, you had to write a <a class="reference internal" href="../reference/compound_stmts.html#def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">def</span></code></a> statement
  386. in the usual way, and pass the resulting method to a <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> or
  387. <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> function that would wrap up the function as a method of the
  388. new type. Your code would look like this:</p>
  389. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  390. <span class="k">def</span> <span class="nf">meth</span> <span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
  391. <span class="o">...</span>
  392. <span class="n">meth</span> <span class="o">=</span> <span class="nb">classmethod</span><span class="p">(</span><span class="n">meth</span><span class="p">)</span> <span class="c1"># Rebind name to wrapped-up class method</span>
  393. </pre></div>
  394. </div>
  395. <p>If the method was very long, it would be easy to miss or forget the
  396. <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> invocation after the function body.</p>
  397. <p>The intention was always to add some syntax to make such definitions more
  398. readable, but at the time of 2.2’s release a good syntax was not obvious. Today
  399. a good syntax <em>still</em> isn’t obvious but users are asking for easier access to
  400. the feature; a new syntactic feature has been added to meet this need.</p>
  401. <p>The new feature is called “function decorators”. The name comes from the idea
  402. that <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a>, <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a>, and friends are storing
  403. additional information on a function object; they’re <em>decorating</em> functions with
  404. more details.</p>
  405. <p>The notation borrows from Java and uses the <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> character as an indicator.
  406. Using the new syntax, the example above would be written:</p>
  407. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  408. <span class="nd">@classmethod</span>
  409. <span class="k">def</span> <span class="nf">meth</span> <span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
  410. <span class="o">...</span>
  411. </pre></div>
  412. </div>
  413. <p>The <code class="docutils literal notranslate"><span class="pre">&#64;classmethod</span></code> is shorthand for the <code class="docutils literal notranslate"><span class="pre">meth=classmethod(meth)</span></code> assignment.
  414. More generally, if you have the following:</p>
  415. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@A</span>
  416. <span class="nd">@B</span>
  417. <span class="nd">@C</span>
  418. <span class="k">def</span> <span class="nf">f</span> <span class="p">():</span>
  419. <span class="o">...</span>
  420. </pre></div>
  421. </div>
  422. <p>It’s equivalent to the following pre-decorator code:</p>
  423. <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>
  424. <span class="n">f</span> <span class="o">=</span> <span class="n">A</span><span class="p">(</span><span class="n">B</span><span class="p">(</span><span class="n">C</span><span class="p">(</span><span class="n">f</span><span class="p">)))</span>
  425. </pre></div>
  426. </div>
  427. <p>Decorators must come on the line before a function definition, one decorator per
  428. line, and can’t be on the same line as the def statement, meaning that <code class="docutils literal notranslate"><span class="pre">&#64;A</span> <span class="pre">def</span>
  429. <span class="pre">f():</span> <span class="pre">...</span></code> is illegal. You can only decorate function definitions, either at
  430. the module level or inside a class; you can’t decorate class definitions.</p>
  431. <p>A decorator is just a function that takes the function to be decorated as an
  432. argument and returns either the same function or some new object. The return
  433. value of the decorator need not be callable (though it typically is), unless
  434. further decorators will be applied to the result. It’s easy to write your own
  435. decorators. The following simple example just sets an attribute on the function
  436. object:</p>
  437. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">deco</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
  438. <span class="gp">... </span> <span class="n">func</span><span class="o">.</span><span class="n">attr</span> <span class="o">=</span> <span class="s1">&#39;decorated&#39;</span>
  439. <span class="gp">... </span> <span class="k">return</span> <span class="n">func</span>
  440. <span class="gp">...</span>
  441. <span class="gp">&gt;&gt;&gt; </span><span class="nd">@deco</span>
  442. <span class="gp">... </span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="k">pass</span>
  443. <span class="gp">...</span>
  444. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span>
  445. <span class="go">&lt;function f at 0x402ef0d4&gt;</span>
  446. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">attr</span>
  447. <span class="go">&#39;decorated&#39;</span>
  448. <span class="gp">&gt;&gt;&gt;</span>
  449. </pre></div>
  450. </div>
  451. <p>As a slightly more realistic example, the following decorator checks that the
  452. supplied argument is an integer:</p>
  453. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">require_int</span> <span class="p">(</span><span class="n">func</span><span class="p">):</span>
  454. <span class="k">def</span> <span class="nf">wrapper</span> <span class="p">(</span><span class="n">arg</span><span class="p">):</span>
  455. <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
  456. <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
  457. <span class="k">return</span> <span class="n">wrapper</span>
  458. <span class="nd">@require_int</span>
  459. <span class="k">def</span> <span class="nf">p1</span> <span class="p">(</span><span class="n">arg</span><span class="p">):</span>
  460. <span class="nb">print</span> <span class="n">arg</span>
  461. <span class="nd">@require_int</span>
  462. <span class="k">def</span> <span class="nf">p2</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>
  463. <span class="nb">print</span> <span class="n">arg</span><span class="o">*</span><span class="mi">2</span>
  464. </pre></div>
  465. </div>
  466. <p>An example in <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0318/"><strong>PEP 318</strong></a> contains a fancier version of this idea that lets you
  467. both specify the required type and check the returned type.</p>
  468. <p>Decorator functions can take arguments. If arguments are supplied, your
  469. decorator function is called with only those arguments and must return a new
  470. decorator function; this function must take a single function and return a
  471. function, as previously described. In other words, <code class="docutils literal notranslate"><span class="pre">&#64;A</span> <span class="pre">&#64;B</span> <span class="pre">&#64;C(args)</span></code> becomes:</p>
  472. <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>
  473. <span class="n">_deco</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  474. <span class="n">f</span> <span class="o">=</span> <span class="n">A</span><span class="p">(</span><span class="n">B</span><span class="p">(</span><span class="n">_deco</span><span class="p">(</span><span class="n">f</span><span class="p">)))</span>
  475. </pre></div>
  476. </div>
  477. <p>Getting this right can be slightly brain-bending, but it’s not too difficult.</p>
  478. <p>A small related change makes the <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_name</span></code> attribute of functions
  479. writable. This attribute is used to display function names in tracebacks, so
  480. decorators should change the name of any new function that’s constructed and
  481. returned.</p>
  482. <div class="admonition seealso">
  483. <p class="admonition-title">See also</p>
  484. <dl class="simple">
  485. <dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0318/"><strong>PEP 318</strong></a> - Decorators for Functions, Methods and Classes</dt><dd><p>Written by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people
  486. wrote patches implementing function decorators, but the one that was actually
  487. checked in was patch #979728, written by Mark Russell.</p>
  488. </dd>
  489. <dt><a class="reference external" href="https://wiki.python.org/moin/PythonDecoratorLibrary">https://wiki.python.org/moin/PythonDecoratorLibrary</a></dt><dd><p>This Wiki page contains several examples of decorators.</p>
  490. </dd>
  491. </dl>
  492. </div>
  493. </section>
  494. <section id="pep-322-reverse-iteration">
  495. <h2>PEP 322: Reverse Iteration<a class="headerlink" href="#pep-322-reverse-iteration" title="Permalink to this headline">¶</a></h2>
  496. <p>A new built-in function, <code class="docutils literal notranslate"><span class="pre">reversed(seq)</span></code>, takes a sequence and returns an
  497. iterator that loops over the elements of the sequence in reverse order.</p>
  498. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">xrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">)):</span>
  499. <span class="gp">... </span> <span class="nb">print</span> <span class="n">i</span>
  500. <span class="gp">...</span>
  501. <span class="go">3</span>
  502. <span class="go">2</span>
  503. <span class="go">1</span>
  504. </pre></div>
  505. </div>
  506. <p>Compared to extended slicing, such as <code class="docutils literal notranslate"><span class="pre">range(1,4)[::-1]</span></code>, <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> is
  507. easier to read, runs faster, and uses substantially less memory.</p>
  508. <p>Note that <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> only accepts sequences, not arbitrary iterators. If
  509. you want to reverse an iterator, first convert it to a list with <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a>.</p>
  510. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">input</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/etc/passwd&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
  511. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">input</span><span class="p">)):</span>
  512. <span class="gp">... </span> <span class="nb">print</span> <span class="n">line</span>
  513. <span class="gp">...</span>
  514. <span class="go">root:*:0:0:System Administrator:/var/root:/bin/tcsh</span>
  515. <span class="go"> ...</span>
  516. </pre></div>
  517. </div>
  518. <div class="admonition seealso">
  519. <p class="admonition-title">See also</p>
  520. <dl class="simple">
  521. <dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0322/"><strong>PEP 322</strong></a> - Reverse Iteration</dt><dd><p>Written and implemented by Raymond Hettinger.</p>
  522. </dd>
  523. </dl>
  524. </div>
  525. </section>
  526. <section id="pep-324-new-subprocess-module">
  527. <h2>PEP 324: New subprocess Module<a class="headerlink" href="#pep-324-new-subprocess-module" title="Permalink to this headline">¶</a></h2>
  528. <p>The standard library provides a number of ways to execute a subprocess, offering
  529. different features and different levels of complexity.
  530. <code class="docutils literal notranslate"><span class="pre">os.system(command)</span></code> is easy to use, but slow (it runs a shell process
  531. which executes the command) and dangerous (you have to be careful about escaping
  532. the shell’s metacharacters). The <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> module offers classes that can
  533. capture standard output and standard error from the subprocess, but the naming
  534. is confusing. The <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module cleans this up, providing a unified
  535. interface that offers all the features you might need.</p>
  536. <p>Instead of <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code>’s collection of classes, <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> contains a
  537. single class called <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code> whose constructor supports a number of
  538. different keyword arguments.</p>
  539. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Popen</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">executable</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
  540. <span class="n">stdin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
  541. <span class="n">preexec_fn</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
  542. <span class="n">cwd</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">env</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">universal_newlines</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
  543. <span class="n">startupinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">creationflags</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  544. </pre></div>
  545. </div>
  546. <p><em>args</em> is commonly a sequence of strings that will be the arguments to the
  547. program executed as the subprocess. (If the <em>shell</em> argument is true, <em>args</em>
  548. can be a string which will then be passed on to the shell for interpretation,
  549. just as <a class="reference internal" href="../library/os.html#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a> does.)</p>
  550. <p><em>stdin</em>, <em>stdout</em>, and <em>stderr</em> specify what the subprocess’s input, output, and
  551. error streams will be. You can provide a file object or a file descriptor, or
  552. you can use the constant <code class="docutils literal notranslate"><span class="pre">subprocess.PIPE</span></code> to create a pipe between the
  553. subprocess and the parent.</p>
  554. <p id="index-7">The constructor has a number of handy options:</p>
  555. <ul class="simple">
  556. <li><p><em>close_fds</em> requests that all file descriptors be closed before running the
  557. subprocess.</p></li>
  558. <li><p><em>cwd</em> specifies the working directory in which the subprocess will be executed
  559. (defaulting to whatever the parent’s working directory is).</p></li>
  560. <li><p><em>env</em> is a dictionary specifying environment variables.</p></li>
  561. <li><p><em>preexec_fn</em> is a function that gets called before the child is started.</p></li>
  562. <li><p><em>universal_newlines</em> opens the child’s input and output using Python’s
  563. <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a> feature.</p></li>
  564. </ul>
  565. <p>Once you’ve created the <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code> instance, you can call its <code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code>
  566. method to pause until the subprocess has exited, <code class="xref py py-meth docutils literal notranslate"><span class="pre">poll()</span></code> to check if it’s
  567. exited without pausing, or <code class="docutils literal notranslate"><span class="pre">communicate(data)</span></code> to send the string <em>data</em>
  568. to the subprocess’s standard input. <code class="docutils literal notranslate"><span class="pre">communicate(data)</span></code> then reads any
  569. data that the subprocess has sent to its standard output or standard error,
  570. returning a tuple <code class="docutils literal notranslate"><span class="pre">(stdout_data,</span> <span class="pre">stderr_data)</span></code>.</p>
  571. <p><code class="xref py py-func docutils literal notranslate"><span class="pre">call()</span></code> is a shortcut that passes its arguments along to the <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code>
  572. constructor, waits for the command to complete, and returns the status code of
  573. the subprocess. It can serve as a safer analog to <a class="reference internal" href="../library/os.html#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a>:</p>
  574. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sts</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">([</span><span class="s1">&#39;dpkg&#39;</span><span class="p">,</span> <span class="s1">&#39;-i&#39;</span><span class="p">,</span> <span class="s1">&#39;/tmp/new-package.deb&#39;</span><span class="p">])</span>
  575. <span class="k">if</span> <span class="n">sts</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  576. <span class="c1"># Success</span>
  577. <span class="o">...</span>
  578. <span class="k">else</span><span class="p">:</span>
  579. <span class="c1"># dpkg returned an error</span>
  580. <span class="o">...</span>
  581. </pre></div>
  582. </div>
  583. <p>The command is invoked without use of the shell. If you really do want to use
  584. the shell, you can add <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> as a keyword argument and provide a string
  585. instead of a sequence:</p>
  586. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sts</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s1">&#39;dpkg -i /tmp/new-package.deb&#39;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  587. </pre></div>
  588. </div>
  589. <p>The PEP takes various examples of shell and Python code and shows how they’d be
  590. translated into Python code that uses <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a>. Reading this section
  591. of the PEP is highly recommended.</p>
  592. <div class="admonition seealso">
  593. <p class="admonition-title">See also</p>
  594. <dl class="simple">
  595. <dt><span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0324/"><strong>PEP 324</strong></a> - subprocess - New process module</dt><dd><p>Written and implemented by Peter Åstrand, with assistance from Fredrik Lundh and
  596. others.</p>
  597. </dd>
  598. </dl>
  599. </div>
  600. </section>
  601. <section id="pep-327-decimal-data-type">
  602. <h2>PEP 327: Decimal Data Type<a class="headerlink" href="#pep-327-decimal-data-type" title="Permalink to this headline">¶</a></h2>
  603. <p>Python has always supported floating-point (FP) numbers, based on the underlying
  604. C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span> type, as a data type. However, while most programming
  605. languages provide a floating-point type, many people (even programmers) are
  606. unaware that floating-point numbers don’t represent certain decimal fractions
  607. accurately. The new <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> type can represent these fractions
  608. accurately, up to a user-specified precision limit.</p>
  609. <section id="why-is-decimal-needed">
  610. <h3>Why is Decimal needed?<a class="headerlink" href="#why-is-decimal-needed" title="Permalink to this headline">¶</a></h3>
  611. <p>The limitations arise from the representation used for floating-point numbers.
  612. FP numbers are made up of three components:</p>
  613. <ul class="simple">
  614. <li><p>The sign, which is positive or negative.</p></li>
  615. <li><p>The mantissa, which is a single-digit binary number followed by a fractional
  616. part. For example, <code class="docutils literal notranslate"><span class="pre">1.01</span></code> in base-2 notation is <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">+</span> <span class="pre">0/2</span> <span class="pre">+</span> <span class="pre">1/4</span></code>, or 1.25 in
  617. decimal notation.</p></li>
  618. <li><p>The exponent, which tells where the decimal point is located in the number
  619. represented.</p></li>
  620. </ul>
  621. <p>For example, the number 1.25 has positive sign, a mantissa value of 1.01 (in
  622. binary), and an exponent of 0 (the decimal point doesn’t need to be shifted).
  623. The number 5 has the same sign and mantissa, but the exponent is 2 because the
  624. mantissa is multiplied by 4 (2 to the power of the exponent 2); 1.25 * 4 equals
  625. 5.</p>
  626. <p>Modern systems usually provide floating-point support that conforms to a
  627. standard called IEEE 754. C’s <span class="c-expr sig sig-inline c"><span class="kt">double</span></span> type is usually implemented as a
  628. 64-bit IEEE 754 number, which uses 52 bits of space for the mantissa. This
  629. means that numbers can only be specified to 52 bits of precision. If you’re
  630. trying to represent numbers whose expansion repeats endlessly, the expansion is
  631. cut off after 52 bits. Unfortunately, most software needs to produce output in
  632. base 10, and common fractions in base 10 are often repeating decimals in binary.
  633. For example, 1.1 decimal is binary <code class="docutils literal notranslate"><span class="pre">1.0001100110011</span> <span class="pre">...</span></code>; .1 = 1/16 + 1/32 +
  634. 1/256 plus an infinite number of additional terms. IEEE 754 has to chop off
  635. that infinitely repeated decimal after 52 digits, so the representation is
  636. slightly inaccurate.</p>
  637. <p>Sometimes you can see this inaccuracy when the number is printed:</p>
  638. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mf">1.1</span>
  639. <span class="go">1.1000000000000001</span>
  640. </pre></div>
  641. </div>
  642. <p>The inaccuracy isn’t always visible when you print the number because the
  643. FP-to-decimal-string conversion is provided by the C library, and most C libraries try
  644. to produce sensible output. Even if it’s not displayed, however, the inaccuracy
  645. is still there and subsequent operations can magnify the error.</p>
  646. <p>For many applications this doesn’t matter. If I’m plotting points and
  647. displaying them on my monitor, the difference between 1.1 and 1.1000000000000001
  648. is too small to be visible. Reports often limit output to a certain number of
  649. decimal places, and if you round the number to two or three or even eight
  650. decimal places, the error is never apparent. However, for applications where it
  651. does matter, it’s a lot of work to implement your own custom arithmetic
  652. routines.</p>
  653. <p>Hence, the <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> type was created.</p>
  654. </section>
  655. <section id="the-decimal-type">
  656. <h3>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> type<a class="headerlink" href="#the-decimal-type" title="Permalink to this headline">¶</a></h3>
  657. <p>A new module, <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a>, was added to Python’s standard library. It
  658. contains two classes, <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code>. <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>
  659. instances represent numbers, and <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> instances are used to wrap up
  660. various settings such as the precision and default rounding mode.</p>
  661. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances are immutable, like regular Python integers and FP
  662. numbers; once it’s been created, you can’t change the value an instance
  663. represents. <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances can be created from integers or
  664. strings:</p>
  665. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">decimal</span>
  666. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1972</span><span class="p">)</span>
  667. <span class="go">Decimal(&quot;1972&quot;)</span>
  668. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s2">&quot;1.1&quot;</span><span class="p">)</span>
  669. <span class="go">Decimal(&quot;1.1&quot;)</span>
  670. </pre></div>
  671. </div>
  672. <p>You can also provide tuples containing the sign, the mantissa represented as a
  673. tuple of decimal digits, and the exponent:</p>
  674. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="o">-</span><span class="mi">2</span><span class="p">))</span>
  675. <span class="go">Decimal(&quot;-14.75&quot;)</span>
  676. </pre></div>
  677. </div>
  678. <p>Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is
  679. negative.</p>
  680. <p>Converting from floating-point numbers poses a bit of a problem: should the FP
  681. number representing 1.1 turn into the decimal number for exactly 1.1, or for 1.1
  682. plus whatever inaccuracies are introduced? The decision was to dodge the issue
  683. and leave such a conversion out of the API. Instead, you should convert the
  684. floating-point number into a string using the desired precision and pass the
  685. string to the <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> constructor:</p>
  686. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="mf">1.1</span>
  687. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">))</span>
  688. <span class="go">Decimal(&quot;1.1&quot;)</span>
  689. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%.12f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">f</span><span class="p">)</span>
  690. <span class="go">Decimal(&quot;1.100000000000&quot;)</span>
  691. </pre></div>
  692. </div>
  693. <p>Once you have <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances, you can perform the usual mathematical
  694. operations on them. One limitation: exponentiation requires an integer
  695. exponent:</p>
  696. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;35.72&#39;</span><span class="p">)</span>
  697. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;1.73&#39;</span><span class="p">)</span>
  698. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">+</span><span class="n">b</span>
  699. <span class="go">Decimal(&quot;37.45&quot;)</span>
  700. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">-</span><span class="n">b</span>
  701. <span class="go">Decimal(&quot;33.99&quot;)</span>
  702. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">*</span><span class="n">b</span>
  703. <span class="go">Decimal(&quot;61.7956&quot;)</span>
  704. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">/</span><span class="n">b</span>
  705. <span class="go">Decimal(&quot;20.64739884393063583815028902&quot;)</span>
  706. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">**</span> <span class="mi">2</span>
  707. <span class="go">Decimal(&quot;1275.9184&quot;)</span>
  708. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">**</span><span class="n">b</span>
  709. <span class="gt">Traceback (most recent call last):</span>
  710. <span class="w"> </span><span class="c">...</span>
  711. <span class="gr">decimal.InvalidOperation</span>: <span class="n">x ** (non-integer)</span>
  712. </pre></div>
  713. </div>
  714. <p>You can combine <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances with integers, but not with
  715. floating-point numbers:</p>
  716. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">+</span> <span class="mi">4</span>
  717. <span class="go">Decimal(&quot;39.72&quot;)</span>
  718. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">+</span> <span class="mf">4.5</span>
  719. <span class="gt">Traceback (most recent call last):</span>
  720. <span class="w"> </span><span class="c">...</span>
  721. <span class="gr">TypeError</span>: <span class="n">You can interact Decimal only with int, long or Decimal data types.</span>
  722. <span class="gp">&gt;&gt;&gt;</span>
  723. </pre></div>
  724. </div>
  725. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> numbers can be used with the <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a> and <a class="reference internal" href="../library/cmath.html#module-cmath" title="cmath: Mathematical functions for complex numbers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code></a>
  726. modules, but note that they’ll be immediately converted to floating-point
  727. numbers before the operation is performed, resulting in a possible loss of
  728. precision and accuracy. You’ll also get back a regular floating-point number
  729. and not a <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>.</p>
  730. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span><span class="o">,</span> <span class="nn">cmath</span>
  731. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;123456789012.345&#39;</span><span class="p">)</span>
  732. <span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  733. <span class="go">351364.18288201344</span>
  734. <span class="gp">&gt;&gt;&gt; </span><span class="n">cmath</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="o">-</span><span class="n">d</span><span class="p">)</span>
  735. <span class="go">351364.18288201344j</span>
  736. </pre></div>
  737. </div>
  738. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances have a <code class="xref py py-meth docutils literal notranslate"><span class="pre">sqrt()</span></code> method that returns a
  739. <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>, but if you need other things such as trigonometric functions
  740. you’ll have to implement them.</p>
  741. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span>
  742. <span class="go">Decimal(&quot;351364.1828820134592177245001&quot;)</span>
  743. </pre></div>
  744. </div>
  745. </section>
  746. <section id="the-context-type">
  747. <h3>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> type<a class="headerlink" href="#the-context-type" title="Permalink to this headline">¶</a></h3>
  748. <p>Instances of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> class encapsulate several settings for
  749. decimal operations:</p>
  750. <ul class="simple">
  751. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">prec</span></code> is the precision, the number of decimal places.</p></li>
  752. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">rounding</span></code> specifies the rounding mode. The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module has
  753. constants for the various possibilities: <code class="xref py py-const docutils literal notranslate"><span class="pre">ROUND_DOWN</span></code>,
  754. <code class="xref py py-const docutils literal notranslate"><span class="pre">ROUND_CEILING</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ROUND_HALF_EVEN</span></code>, and various others.</p></li>
  755. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">traps</span></code> is a dictionary specifying what happens on encountering certain
  756. error conditions: either an exception is raised or a value is returned. Some
  757. examples of error conditions are division by zero, loss of precision, and
  758. overflow.</p></li>
  759. </ul>
  760. <p>There’s a thread-local default context available by calling <code class="xref py py-func docutils literal notranslate"><span class="pre">getcontext()</span></code>;
  761. you can change the properties of this context to alter the default precision,
  762. rounding, or trap handling. The following example shows the effect of changing
  763. the precision of the default context:</p>
  764. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span>
  765. <span class="go">28</span>
  766. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
  767. <span class="go">Decimal(&quot;0.1428571428571428571428571429&quot;)</span>
  768. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">9</span>
  769. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
  770. <span class="go">Decimal(&quot;0.142857143&quot;)</span>
  771. </pre></div>
  772. </div>
  773. <p>The default action for error conditions is selectable; the module can either
  774. return a special value such as infinity or not-a-number, or exceptions can be
  775. raised:</p>
  776. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  777. <span class="gt">Traceback (most recent call last):</span>
  778. <span class="w"> </span><span class="c">...</span>
  779. <span class="gr">decimal.DivisionByZero</span>: <span class="n">x / 0</span>
  780. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">traps</span><span class="p">[</span><span class="n">decimal</span><span class="o">.</span><span class="n">DivisionByZero</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
  781. <span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  782. <span class="go">Decimal(&quot;Infinity&quot;)</span>
  783. <span class="gp">&gt;&gt;&gt;</span>
  784. </pre></div>
  785. </div>
  786. <p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> instance also has various methods for formatting numbers
  787. such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">to_eng_string()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">to_sci_string()</span></code>.</p>
  788. <p>For more information, see the documentation for the <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module, which
  789. includes a quick-start tutorial and a reference.</p>
  790. <div class="admonition seealso">
  791. <p class="admonition-title">See also</p>
  792. <dl class="simple">
  793. <dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0327/"><strong>PEP 327</strong></a> - Decimal Data Type</dt><dd><p>Written by Facundo Batista and implemented by Facundo Batista, Eric Price,
  794. Raymond Hettinger, Aahz, and Tim Peters.</p>
  795. </dd>
  796. <dt><a class="reference external" href="http://www.lahey.com/float.htm">http://www.lahey.com/float.htm</a></dt><dd><p>The article uses Fortran code to illustrate many of the problems that
  797. floating-point inaccuracy can cause.</p>
  798. </dd>
  799. <dt><a class="reference external" href="http://speleotrove.com/decimal/">http://speleotrove.com/decimal/</a></dt><dd><p>A description of a decimal-based representation. This representation is being
  800. proposed as a standard, and underlies the new Python decimal type. Much of this
  801. material was written by Mike Cowlishaw, designer of the Rexx language.</p>
  802. </dd>
  803. </dl>
  804. </div>
  805. </section>
  806. </section>
  807. <section id="pep-328-multi-line-imports">
  808. <h2>PEP 328: Multi-line Imports<a class="headerlink" href="#pep-328-multi-line-imports" title="Permalink to this headline">¶</a></h2>
  809. <p>One language change is a small syntactic tweak aimed at making it easier to
  810. import many names from a module. In a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">names</span></code> statement,
  811. <em>names</em> is a sequence of names separated by commas. If the sequence is very
  812. long, you can either write multiple imports from the same module, or you can use
  813. backslashes to escape the line endings like this:</p>
  814. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">SimpleXMLRPCServer</span> <span class="kn">import</span> <span class="n">SimpleXMLRPCServer</span><span class="p">,</span>\
  815. <span class="n">SimpleXMLRPCRequestHandler</span><span class="p">,</span>\
  816. <span class="n">CGIXMLRPCRequestHandler</span><span class="p">,</span>\
  817. <span class="n">resolve_dotted_attribute</span>
  818. </pre></div>
  819. </div>
  820. <p>The syntactic change in Python 2.4 simply allows putting the names within
  821. parentheses. Python ignores newlines within a parenthesized expression, so the
  822. backslashes are no longer needed:</p>
  823. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">SimpleXMLRPCServer</span> <span class="kn">import</span> <span class="p">(</span><span class="n">SimpleXMLRPCServer</span><span class="p">,</span>
  824. <span class="n">SimpleXMLRPCRequestHandler</span><span class="p">,</span>
  825. <span class="n">CGIXMLRPCRequestHandler</span><span class="p">,</span>
  826. <span class="n">resolve_dotted_attribute</span><span class="p">)</span>
  827. </pre></div>
  828. </div>
  829. <p>The PEP also proposes that all <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statements be absolute imports,
  830. with a leading <code class="docutils literal notranslate"><span class="pre">.</span></code> character to indicate a relative import. This part of the
  831. PEP was not implemented for Python 2.4, but was completed for Python 2.5.</p>
  832. <div class="admonition seealso">
  833. <p class="admonition-title">See also</p>
  834. <dl class="simple">
  835. <dt><span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0328/"><strong>PEP 328</strong></a> - Imports: Multi-Line and Absolute/Relative</dt><dd><p>Written by Aahz. Multi-line imports were implemented by Dima Dorfman.</p>
  836. </dd>
  837. </dl>
  838. </div>
  839. </section>
  840. <section id="pep-331-locale-independent-float-string-conversions">
  841. <h2>PEP 331: Locale-Independent Float/String Conversions<a class="headerlink" href="#pep-331-locale-independent-float-string-conversions" title="Permalink to this headline">¶</a></h2>
  842. <p>The <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> modules lets Python software select various conversions and
  843. display conventions that are localized to a particular country or language.
  844. However, the module was careful to not change the numeric locale because various
  845. functions in Python’s implementation required that the numeric locale remain set
  846. to the <code class="docutils literal notranslate"><span class="pre">'C'</span></code> locale. Often this was because the code was using the C
  847. library’s <code class="xref c c-func docutils literal notranslate"><span class="pre">atof()</span></code> function.</p>
  848. <p>Not setting the numeric locale caused trouble for extensions that used third-party
  849. C libraries, however, because they wouldn’t have the correct locale set.
  850. The motivating example was GTK+, whose user interface widgets weren’t displaying
  851. numbers in the current locale.</p>
  852. <p>The solution described in the PEP is to add three new functions to the Python
  853. API that perform ASCII-only conversions, ignoring the locale setting:</p>
  854. <ul class="simple">
  855. <li><p><code class="docutils literal notranslate"><span class="pre">PyOS_ascii_strtod(str,</span> <span class="pre">ptr)</span></code> and <code class="docutils literal notranslate"><span class="pre">PyOS_ascii_atof(str,</span> <span class="pre">ptr)</span></code>
  856. both convert a string to a C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span>.</p></li>
  857. <li><p><code class="docutils literal notranslate"><span class="pre">PyOS_ascii_formatd(buffer,</span> <span class="pre">buf_len,</span> <span class="pre">format,</span> <span class="pre">d)</span></code> converts a
  858. <span class="c-expr sig sig-inline c"><span class="kt">double</span></span> to an ASCII string.</p></li>
  859. </ul>
  860. <p>The code for these functions came from the GLib library
  861. (<a class="reference external" href="https://developer-old.gnome.org/glib/2.26/">https://developer-old.gnome.org/glib/2.26/</a>), whose developers kindly
  862. relicensed the relevant functions and donated them to the Python Software
  863. Foundation. The <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module can now change the numeric locale,
  864. letting extensions such as GTK+ produce the correct results.</p>
  865. <div class="admonition seealso">
  866. <p class="admonition-title">See also</p>
  867. <dl class="simple">
  868. <dt><span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0331/"><strong>PEP 331</strong></a> - Locale-Independent Float/String Conversions</dt><dd><p>Written by Christian R. Reis, and implemented by Gustavo Carneiro.</p>
  869. </dd>
  870. </dl>
  871. </div>
  872. </section>
  873. <section id="other-language-changes">
  874. <h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Permalink to this headline">¶</a></h2>
  875. <p>Here are all of the changes that Python 2.4 makes to the core Python language.</p>
  876. <ul>
  877. <li><p>Decorators for functions and methods were added (<span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0318/"><strong>PEP 318</strong></a>).</p></li>
  878. <li><p>Built-in <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><code class="xref py py-func docutils literal notranslate"><span class="pre">frozenset()</span></code></a> types were added (<span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-0218/"><strong>PEP 218</strong></a>).
  879. Other new built-ins include the <code class="docutils literal notranslate"><span class="pre">reversed(seq)</span></code> function (<span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0322/"><strong>PEP 322</strong></a>).</p></li>
  880. <li><p>Generator expressions were added (<span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0289/"><strong>PEP 289</strong></a>).</p></li>
  881. <li><p>Certain numeric expressions no longer return values restricted to 32 or 64
  882. bits (<span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-0237/"><strong>PEP 237</strong></a>).</p></li>
  883. <li><p>You can now put parentheses around the list of names in a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span>
  884. <span class="pre">names</span></code> statement (<span class="target" id="index-17"></span><a class="pep reference external" href="https://peps.python.org/pep-0328/"><strong>PEP 328</strong></a>).</p></li>
  885. <li><p>The <a class="reference internal" href="../library/stdtypes.html#dict.update" title="dict.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.update()</span></code></a> method now accepts the same argument forms as the
  886. <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> constructor. This includes any mapping, any iterable of key/value
  887. pairs, and keyword arguments. (Contributed by Raymond Hettinger.)</p></li>
  888. <li><p>The string methods <code class="xref py py-meth docutils literal notranslate"><span class="pre">ljust()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">rjust()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">center()</span></code> now take
  889. an optional argument for specifying a fill character other than a space.
  890. (Contributed by Raymond Hettinger.)</p></li>
  891. <li><p>Strings also gained an <code class="xref py py-meth docutils literal notranslate"><span class="pre">rsplit()</span></code> method that works like the <code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code>
  892. method but splits from the end of the string. (Contributed by Sean
  893. Reifschneider.)</p>
  894. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;www.python.org&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
  895. <span class="go">[&#39;www&#39;, &#39;python.org&#39;]</span>
  896. <span class="go">&#39;www.python.org&#39;.rsplit(&#39;.&#39;, 1)</span>
  897. <span class="go">[&#39;www.python&#39;, &#39;org&#39;]</span>
  898. </pre></div>
  899. </div>
  900. </li>
  901. <li><p>Three keyword parameters, <em>cmp</em>, <em>key</em>, and <em>reverse</em>, were added to the
  902. <code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code> method of lists. These parameters make some common usages of
  903. <code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code> simpler. All of these parameters are optional.</p>
  904. <p>For the <em>cmp</em> parameter, the value should be a comparison function that takes
  905. two parameters and returns -1, 0, or +1 depending on how the parameters compare.
  906. This function will then be used to sort the list. Previously this was the only
  907. parameter that could be provided to <code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code>.</p>
  908. <p><em>key</em> should be a single-parameter function that takes a list element and
  909. returns a comparison key for the element. The list is then sorted using the
  910. comparison keys. The following example sorts a list case-insensitively:</p>
  911. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;D&#39;</span><span class="p">]</span>
  912. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span> <span class="c1"># Case-sensitive sort</span>
  913. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
  914. <span class="go">[&#39;A&#39;, &#39;D&#39;, &#39;b&#39;, &#39;c&#39;]</span>
  915. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Using &#39;key&#39; parameter to sort list</span>
  916. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
  917. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
  918. <span class="go">[&#39;A&#39;, &#39;b&#39;, &#39;c&#39;, &#39;D&#39;]</span>
  919. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Old-fashioned way</span>
  920. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">cmp</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span> <span class="n">cmp</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">y</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
  921. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
  922. <span class="go">[&#39;A&#39;, &#39;b&#39;, &#39;c&#39;, &#39;D&#39;]</span>
  923. </pre></div>
  924. </div>
  925. <p>The last example, which uses the <em>cmp</em> parameter, is the old way to perform a
  926. case-insensitive sort. It works but is slower than using a <em>key</em> parameter.
  927. Using <em>key</em> calls <code class="xref py py-meth docutils literal notranslate"><span class="pre">lower()</span></code> method once for each element in the list while
  928. using <em>cmp</em> will call it twice for each comparison, so using <em>key</em> saves on
  929. invocations of the <code class="xref py py-meth docutils literal notranslate"><span class="pre">lower()</span></code> method.</p>
  930. <p>For simple key functions and comparison functions, it is often possible to avoid
  931. a <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 by using an unbound method instead. For example,
  932. the above case-insensitive sort is best written as:</p>
  933. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="nb">str</span><span class="o">.</span><span class="n">lower</span><span class="p">)</span>
  934. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
  935. <span class="go">[&#39;A&#39;, &#39;b&#39;, &#39;c&#39;, &#39;D&#39;]</span>
  936. </pre></div>
  937. </div>
  938. <p>Finally, the <em>reverse</em> parameter takes a Boolean value. If the value is true,
  939. the list will be sorted into reverse order. Instead of <code class="docutils literal notranslate"><span class="pre">L.sort();</span>
  940. <span class="pre">L.reverse()</span></code>, you can now write <code class="docutils literal notranslate"><span class="pre">L.sort(reverse=True)</span></code>.</p>
  941. <p>The results of sorting are now guaranteed to be stable. This means that two
  942. entries with equal keys will be returned in the same order as they were input.
  943. For example, you can sort a list of people by name, and then sort the list by
  944. age, resulting in a list sorted by age where people with the same age are in
  945. name-sorted order.</p>
  946. <p>(All changes to <code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code> contributed by Raymond Hettinger.)</p>
  947. </li>
  948. <li><p>There is a new built-in function <code class="docutils literal notranslate"><span class="pre">sorted(iterable)</span></code> that works like the
  949. in-place <a class="reference internal" href="../library/stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a> method but can be used in expressions. The
  950. differences are:</p></li>
  951. <li><p>the input may be any iterable;</p></li>
  952. <li><p>a newly formed copy is sorted, leaving the original intact; and</p></li>
  953. <li><p>the expression returns the new sorted copy</p>
  954. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">9</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">]</span>
  955. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="mi">10</span><span class="o">+</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">L</span><span class="p">)]</span> <span class="c1"># usable in a list comprehension</span>
  956. <span class="go">[11, 12, 13, 14, 15, 16, 17, 18, 19]</span>
  957. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="c1"># original is left unchanged</span>
  958. <span class="go">[9,7,8,3,2,4,1,6,5]</span>
  959. <span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="s1">&#39;Monty Python&#39;</span><span class="p">)</span> <span class="c1"># any iterable may be an input</span>
  960. <span class="go">[&#39; &#39;, &#39;M&#39;, &#39;P&#39;, &#39;h&#39;, &#39;n&#39;, &#39;n&#39;, &#39;o&#39;, &#39;o&#39;, &#39;t&#39;, &#39;t&#39;, &#39;y&#39;, &#39;y&#39;]</span>
  961. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># List the contents of a dict sorted by key values</span>
  962. <span class="gp">&gt;&gt;&gt; </span><span class="n">colormap</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">red</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">blue</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">green</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">black</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">yellow</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
  963. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">colormap</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()):</span>
  964. <span class="gp">... </span> <span class="nb">print</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span>
  965. <span class="gp">...</span>
  966. <span class="go">black 4</span>
  967. <span class="go">blue 2</span>
  968. <span class="go">green 3</span>
  969. <span class="go">red 1</span>
  970. <span class="go">yellow 5</span>
  971. </pre></div>
  972. </div>
  973. <p>(Contributed by Raymond Hettinger.)</p>
  974. </li>
  975. <li><p>Integer operations will no longer trigger an <code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code>. The
  976. <code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code> warning will disappear in Python 2.5.</p></li>
  977. <li><p>The interpreter gained a new switch, <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a>, that takes a name, searches
  978. for the corresponding module on <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, and runs the module as a script.
  979. For example, you can now run the Python profiler with <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">profile</span></code>.
  980. (Contributed by Nick Coghlan.)</p></li>
  981. <li><p>The <code class="docutils literal notranslate"><span class="pre">eval(expr,</span> <span class="pre">globals,</span> <span class="pre">locals)</span></code> and <code class="docutils literal notranslate"><span class="pre">execfile(filename,</span> <span class="pre">globals,</span>
  982. <span class="pre">locals)</span></code> functions and the <code class="docutils literal notranslate"><span class="pre">exec</span></code> statement now accept any mapping type
  983. for the <em>locals</em> parameter. Previously this had to be a regular Python
  984. dictionary. (Contributed by Raymond Hettinger.)</p></li>
  985. <li><p>The <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> built-in function and <code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.izip()</span></code> now return an
  986. empty list if called with no arguments. Previously they raised a
  987. <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. This makes them more suitable for use with variable
  988. length argument lists:</p>
  989. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">transpose</span><span class="p">(</span><span class="n">array</span><span class="p">):</span>
  990. <span class="gp">... </span> <span class="k">return</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">array</span><span class="p">)</span>
  991. <span class="gp">...</span>
  992. <span class="gp">&gt;&gt;&gt; </span><span class="n">transpose</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> <span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">)])</span>
  993. <span class="go">[(1, 4), (2, 5), (3, 6)]</span>
  994. <span class="gp">&gt;&gt;&gt; </span><span class="n">transpose</span><span class="p">([])</span>
  995. <span class="go">[]</span>
  996. </pre></div>
  997. </div>
  998. <p>(Contributed by Raymond Hettinger.)</p>
  999. </li>
  1000. <li><p>Encountering a failure while importing a module no longer leaves a partially initialized
  1001. module object in <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>. The incomplete module object left
  1002. behind would fool further imports of the same module into succeeding, leading to
  1003. confusing errors. (Fixed by Tim Peters.)</p></li>
  1004. <li><p><a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> is now a constant; code that binds a new value to the name
  1005. <code class="docutils literal notranslate"><span class="pre">None</span></code> is now a syntax error. (Contributed by Raymond Hettinger.)</p></li>
  1006. </ul>
  1007. <section id="optimizations">
  1008. <h3>Optimizations<a class="headerlink" href="#optimizations" title="Permalink to this headline">¶</a></h3>
  1009. <ul class="simple">
  1010. <li><p>The inner loops for list and tuple slicing were optimized and now run about
  1011. one-third faster. The inner loops for dictionaries were also optimized,
  1012. resulting in performance boosts for <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code>,
  1013. <code class="xref py py-meth docutils literal notranslate"><span class="pre">iterkeys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">itervalues()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">iteritems()</span></code>. (Contributed by
  1014. Raymond Hettinger.)</p></li>
  1015. <li><p>The machinery for growing and shrinking lists was optimized for speed and for
  1016. space efficiency. Appending and popping from lists now runs faster due to more
  1017. efficient code paths and less frequent use of the underlying system
  1018. <code class="xref c c-func docutils literal notranslate"><span class="pre">realloc()</span></code>. List comprehensions also benefit. <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.extend()</span></code> was
  1019. also optimized and no longer converts its argument into a temporary list before
  1020. extending the base list. (Contributed by Raymond Hettinger.)</p></li>
  1021. <li><p><a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#tuple" title="tuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">tuple()</span></code></a>, <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>, <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>, and <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> now
  1022. run several times faster with non-sequence arguments that supply a
  1023. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code> method. (Contributed by Raymond Hettinger.)</p></li>
  1024. <li><p>The methods <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.__getitem__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.__getitem__()</span></code>, and
  1025. <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.__contains__()</span></code> are now implemented as <code class="xref py py-class docutils literal notranslate"><span class="pre">method_descriptor</span></code>
  1026. objects rather than <code class="xref py py-class docutils literal notranslate"><span class="pre">wrapper_descriptor</span></code> objects. This form of access
  1027. doubles their performance and makes them more suitable for use as arguments to
  1028. functionals: <code class="docutils literal notranslate"><span class="pre">map(mydict.__getitem__,</span> <span class="pre">keylist)</span></code>. (Contributed by Raymond
  1029. Hettinger.)</p></li>
  1030. <li><p>Added a new opcode, <code class="docutils literal notranslate"><span class="pre">LIST_APPEND</span></code>, that simplifies the generated bytecode
  1031. for list comprehensions and speeds them up by about a third. (Contributed by
  1032. Raymond Hettinger.)</p></li>
  1033. <li><p>The peephole bytecode optimizer has been improved to produce shorter, faster
  1034. bytecode; remarkably, the resulting bytecode is more readable. (Enhanced by
  1035. Raymond Hettinger.)</p></li>
  1036. <li><p>String concatenations in statements of the form <code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">=</span> <span class="pre">s</span> <span class="pre">+</span> <span class="pre">&quot;abc&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">+=</span>
  1037. <span class="pre">&quot;abc&quot;</span></code> are now performed more efficiently in certain circumstances. This
  1038. optimization won’t be present in other Python implementations such as Jython, so
  1039. you shouldn’t rely on it; using the <code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code> method of strings is still
  1040. recommended when you want to efficiently glue a large number of strings
  1041. together. (Contributed by Armin Rigo.)</p></li>
  1042. </ul>
  1043. <p>The net result of the 2.4 optimizations is that Python 2.4 runs the pystone
  1044. benchmark around 5% faster than Python 2.3 and 35% faster than Python 2.2.
  1045. (pystone is not a particularly good benchmark, but it’s the most commonly used
  1046. measurement of Python’s performance. Your own applications may show greater or
  1047. smaller benefits from Python 2.4.)</p>
  1048. </section>
  1049. </section>
  1050. <section id="new-improved-and-deprecated-modules">
  1051. <h2>New, Improved, and Deprecated Modules<a class="headerlink" href="#new-improved-and-deprecated-modules" title="Permalink to this headline">¶</a></h2>
  1052. <p>As usual, Python’s standard library received a number of enhancements and bug
  1053. fixes. Here’s a partial list of the most notable changes, sorted alphabetically
  1054. by module name. Consult the <code class="file docutils literal notranslate"><span class="pre">Misc/NEWS</span></code> file in the source tree for a more
  1055. complete list of changes, or look through the CVS logs for all the details.</p>
  1056. <ul>
  1057. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncore</span></code> module’s <code class="xref py py-func docutils literal notranslate"><span class="pre">loop()</span></code> function now has a <em>count</em> parameter
  1058. that lets you perform a limited number of passes through the polling loop. The
  1059. default is still to loop forever.</p></li>
  1060. <li><p>The <a class="reference internal" href="../library/base64.html#module-base64" title="base64: RFC 4648: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a> module now has more complete <span class="target" id="index-18"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3548.html"><strong>RFC 3548</strong></a> support for Base64,
  1061. Base32, and Base16 encoding and decoding, including optional case folding and
  1062. optional alternative alphabets. (Contributed by Barry Warsaw.)</p></li>
  1063. <li><p>The <a class="reference internal" href="../library/bisect.html#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code></a> module now has an underlying C implementation for improved
  1064. performance. (Contributed by Dmitry Vasiliev.)</p></li>
  1065. <li><p>The CJKCodecs collections of East Asian codecs, maintained by Hye-Shik Chang,
  1066. was integrated into 2.4. The new encodings are:</p></li>
  1067. <li><p>Chinese (PRC): gb2312, gbk, gb18030, big5hkscs, hz</p></li>
  1068. <li><p>Chinese (ROC): big5, cp950</p></li>
  1069. <li><dl class="simple">
  1070. <dt>Japanese: cp932, euc-jis-2004, euc-jp, euc-jisx0213, iso-2022-jp,</dt><dd><p>iso-2022-jp-1, iso-2022-jp-2, iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004,
  1071. shift-jis, shift-jisx0213, shift-jis-2004</p>
  1072. </dd>
  1073. </dl>
  1074. </li>
  1075. <li><p>Korean: cp949, euc-kr, johab, iso-2022-kr</p></li>
  1076. <li><p>Some other new encodings were added: HP Roman8, ISO_8859-11, ISO_8859-16,
  1077. PCTP-154, and TIS-620.</p></li>
  1078. <li><p>The UTF-8 and UTF-16 codecs now cope better with receiving partial input.
  1079. Previously the <code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code> class would try to read more data, making
  1080. it impossible to resume decoding from the stream. The <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> method will
  1081. now return as much data as it can and future calls will resume decoding where
  1082. previous ones left off. (Implemented by Walter Dörwald.)</p></li>
  1083. <li><p>There is a new <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module for various specialized collection
  1084. datatypes. Currently it contains just one type, <code class="xref py py-class docutils literal notranslate"><span class="pre">deque</span></code>, a double-ended
  1085. queue that supports efficiently adding and removing elements from either
  1086. end:</p>
  1087. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span>
  1088. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="s1">&#39;ghi&#39;</span><span class="p">)</span> <span class="c1"># make a new deque with three items</span>
  1089. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;j&#39;</span><span class="p">)</span> <span class="c1"># add a new entry to the right side</span>
  1090. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="s1">&#39;f&#39;</span><span class="p">)</span> <span class="c1"># add a new entry to the left side</span>
  1091. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="c1"># show the representation of the deque</span>
  1092. <span class="go">deque([&#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;])</span>
  1093. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="c1"># return and remove the rightmost item</span>
  1094. <span class="go">&#39;j&#39;</span>
  1095. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c1"># return and remove the leftmost item</span>
  1096. <span class="go">&#39;f&#39;</span>
  1097. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="c1"># list the contents of the deque</span>
  1098. <span class="go">[&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]</span>
  1099. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;h&#39;</span> <span class="ow">in</span> <span class="n">d</span> <span class="c1"># search the deque</span>
  1100. <span class="go">True</span>
  1101. </pre></div>
  1102. </div>
  1103. <p>Several modules, such as the <code class="xref py py-mod docutils literal notranslate"><span class="pre">Queue</span></code> and <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> modules, now take
  1104. advantage of <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.deque</span></code></a> for improved performance. (Contributed
  1105. by Raymond Hettinger.)</p>
  1106. </li>
  1107. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code> classes have been enhanced slightly. The <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code>
  1108. method now returns a list of the files that were successfully parsed, and the
  1109. <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> method raises <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> if passed a <em>value</em> argument that
  1110. isn’t a string. (Contributed by John Belmonte and David Goodger.)</p></li>
  1111. <li><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 now supports the ncurses extension
  1112. <code class="xref py py-func docutils literal notranslate"><span class="pre">use_default_colors()</span></code>. On platforms where the terminal supports
  1113. transparency, this makes it possible to use a transparent background.
  1114. (Contributed by Jörg Lehmann.)</p></li>
  1115. <li><p>The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code></a> module now includes an <code class="xref py py-class docutils literal notranslate"><span class="pre">HtmlDiff</span></code> class that creates
  1116. an HTML table showing a side by side comparison of two versions of a text.
  1117. (Contributed by Dan Gass.)</p></li>
  1118. <li><p>The <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package was updated to version 3.0, which dropped various
  1119. deprecated APIs and removes support for Python versions earlier than 2.3. The
  1120. 3.0 version of the package uses a new incremental parser for MIME messages,
  1121. available in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">email.FeedParser</span></code> module. The new parser doesn’t require
  1122. reading the entire message into memory, and doesn’t raise exceptions if a
  1123. message is malformed; instead it records any problems in the <code class="xref py py-attr docutils literal notranslate"><span class="pre">defect</span></code>
  1124. attribute of the message. (Developed by Anthony Baxter, Barry Warsaw, Thomas
  1125. Wouters, and others.)</p></li>
  1126. <li><p>The <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">heapq</span></code></a> module has been converted to C. The resulting tenfold
  1127. improvement in speed makes the module suitable for handling high volumes of
  1128. data. In addition, the module has two new functions <code class="xref py py-func docutils literal notranslate"><span class="pre">nlargest()</span></code> and
  1129. <code class="xref py py-func docutils literal notranslate"><span class="pre">nsmallest()</span></code> that use heaps to find the N largest or smallest values in a
  1130. dataset without the expense of a full sort. (Contributed by Raymond Hettinger.)</p></li>
  1131. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> module now contains constants for HTTP status codes defined
  1132. in various HTTP-related RFC documents. Constants have names such as
  1133. <code class="xref py py-const docutils literal notranslate"><span class="pre">OK</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">CREATED</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">CONTINUE</span></code>, and
  1134. <code class="xref py py-const docutils literal notranslate"><span class="pre">MOVED_PERMANENTLY</span></code>; use pydoc to get a full list. (Contributed by
  1135. Andrew Eland.)</p></li>
  1136. <li><p>The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> module now supports IMAP’s THREAD command (contributed by
  1137. Yves Dionne) and new <code class="xref py py-meth docutils literal notranslate"><span class="pre">deleteacl()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">myrights()</span></code> methods (contributed
  1138. by Arnaud Mazin).</p></li>
  1139. <li><p>The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module gained a <code class="docutils literal notranslate"><span class="pre">groupby(iterable[,</span> <span class="pre">*func*])</span></code>
  1140. function. <em>iterable</em> is something that can be iterated over to return a stream
  1141. of elements, and the optional <em>func</em> parameter is a function that takes an
  1142. element and returns a key value; if omitted, the key is simply the element
  1143. itself. <code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> then groups the elements into subsequences which have
  1144. matching values of the key, and returns a series of 2-tuples containing the key
  1145. value and an iterator over the subsequence.</p>
  1146. <p>Here’s an example to make this clearer. The <em>key</em> function simply returns
  1147. whether a number is even or odd, so the result of <code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> is to return
  1148. consecutive runs of odd or even numbers.</p>
  1149. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">itertools</span>
  1150. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">14</span><span class="p">]</span>
  1151. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">key_val</span><span class="p">,</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span><span class="p">):</span>
  1152. <span class="gp">... </span> <span class="nb">print</span> <span class="n">key_val</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
  1153. <span class="gp">...</span>
  1154. <span class="go">0 [2, 4, 6]</span>
  1155. <span class="go">1 [7]</span>
  1156. <span class="go">0 [8]</span>
  1157. <span class="go">1 [9, 11]</span>
  1158. <span class="go">0 [12, 14]</span>
  1159. <span class="gp">&gt;&gt;&gt;</span>
  1160. </pre></div>
  1161. </div>
  1162. <p><code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> is typically used with sorted input. The logic for
  1163. <code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> is similar to the Unix <code class="docutils literal notranslate"><span class="pre">uniq</span></code> filter which makes it handy for
  1164. eliminating, counting, or identifying duplicate elements:</p>
  1165. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span> <span class="o">=</span> <span class="s1">&#39;abracadabra&#39;</span>
  1166. <span class="gp">&gt;&gt;&gt; </span><span class="n">letters</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="c1"># Turn string into a sorted list of letters</span>
  1167. <span class="gp">&gt;&gt;&gt; </span><span class="n">letters</span>
  1168. <span class="go">[&#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;b&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;r&#39;, &#39;r&#39;]</span>
  1169. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">letters</span><span class="p">):</span>
  1170. <span class="gp">... </span> <span class="nb">print</span> <span class="n">k</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
  1171. <span class="gp">...</span>
  1172. <span class="go">a [&#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;]</span>
  1173. <span class="go">b [&#39;b&#39;, &#39;b&#39;]</span>
  1174. <span class="go">c [&#39;c&#39;]</span>
  1175. <span class="go">d [&#39;d&#39;]</span>
  1176. <span class="go">r [&#39;r&#39;, &#39;r&#39;]</span>
  1177. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># List unique letters</span>
  1178. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">letters</span><span class="p">)]</span>
  1179. <span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;r&#39;]</span>
  1180. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Count letter occurrences</span>
  1181. <span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">k</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">g</span><span class="p">)))</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">letters</span><span class="p">)]</span>
  1182. <span class="go">[(&#39;a&#39;, 5), (&#39;b&#39;, 2), (&#39;c&#39;, 1), (&#39;d&#39;, 1), (&#39;r&#39;, 2)]</span>
  1183. </pre></div>
  1184. </div>
  1185. <p>(Contributed by Hye-Shik Chang.)</p>
  1186. </li>
  1187. <li><p><a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> also gained a function named <code class="docutils literal notranslate"><span class="pre">tee(iterator,</span> <span class="pre">N)</span></code> that
  1188. returns <em>N</em> independent iterators that replicate <em>iterator</em>. If <em>N</em> is omitted,
  1189. the default is 2.</p>
  1190. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</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>
  1191. <span class="gp">&gt;&gt;&gt; </span><span class="n">i1</span><span class="p">,</span> <span class="n">i2</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">tee</span><span class="p">(</span><span class="n">L</span><span class="p">)</span>
  1192. <span class="gp">&gt;&gt;&gt; </span><span class="n">i1</span><span class="p">,</span><span class="n">i2</span>
  1193. <span class="go">(&lt;itertools.tee object at 0x402c2080&gt;, &lt;itertools.tee object at 0x402c2090&gt;)</span>
  1194. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">i1</span><span class="p">)</span> <span class="c1"># Run the first iterator to exhaustion</span>
  1195. <span class="go">[1, 2, 3]</span>
  1196. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">i2</span><span class="p">)</span> <span class="c1"># Run the second iterator to exhaustion</span>
  1197. <span class="go">[1, 2, 3]</span>
  1198. </pre></div>
  1199. </div>
  1200. <p>Note that <code class="xref py py-func docutils literal notranslate"><span class="pre">tee()</span></code> has to keep copies of the values returned by the
  1201. iterator; in the worst case, it may need to keep all of them. This should
  1202. therefore be used carefully if the leading iterator can run far ahead of the
  1203. trailing iterator in a long stream of inputs. If the separation is large, then
  1204. you might as well use <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a> instead. When the iterators track closely
  1205. with one another, <code class="xref py py-func docutils literal notranslate"><span class="pre">tee()</span></code> is ideal. Possible applications include
  1206. bookmarking, windowing, or lookahead iterators. (Contributed by Raymond
  1207. Hettinger.)</p>
  1208. </li>
  1209. <li><p>A number of functions were added to the <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module, such as
  1210. <code class="xref py py-func docutils literal notranslate"><span class="pre">bind_textdomain_codeset()</span></code> to specify a particular encoding and a family of
  1211. <code class="xref py py-func docutils literal notranslate"><span class="pre">l*gettext()</span></code> functions that return messages in the chosen encoding.
  1212. (Contributed by Gustavo Niemeyer.)</p></li>
  1213. <li><p>Some keyword arguments were added to the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> package’s
  1214. <code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code> function to simplify log configuration. The default
  1215. behavior is to log messages to standard error, but various keyword arguments can
  1216. be specified to log to a particular file, change the logging format, or set the
  1217. logging level. For example:</p>
  1218. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
  1219. <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s1">&#39;/var/log/application.log&#39;</span><span class="p">,</span>
  1220. <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="c1"># Log all messages</span>
  1221. <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;%(levelname):%(process):%(thread):%(message)&#39;</span><span class="p">)</span>
  1222. </pre></div>
  1223. </div>
  1224. <p>Other additions to the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> package include a <code class="docutils literal notranslate"><span class="pre">log(level,</span> <span class="pre">msg)</span></code>
  1225. convenience method, as well as a <code class="xref py py-class docutils literal notranslate"><span class="pre">TimedRotatingFileHandler</span></code> class that
  1226. rotates its log files at a timed interval. The module already had
  1227. <code class="xref py py-class docutils literal notranslate"><span class="pre">RotatingFileHandler</span></code>, which rotated logs once the file exceeded a
  1228. certain size. Both classes derive from a new <code class="xref py py-class docutils literal notranslate"><span class="pre">BaseRotatingHandler</span></code> class
  1229. that can be used to implement other rotating handlers.</p>
  1230. <p>(Changes implemented by Vinay Sajip.)</p>
  1231. </li>
  1232. <li><p>The <a class="reference internal" href="../library/marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code></a> module now shares interned strings on unpacking a data
  1233. structure. This may shrink the size of certain pickle strings, but the primary
  1234. effect is to make <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> files significantly smaller. (Contributed by
  1235. Martin von Löwis.)</p></li>
  1236. <li><p>The <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> module’s <code class="xref py py-class docutils literal notranslate"><span class="pre">NNTP</span></code> class gained <code class="xref py py-meth docutils literal notranslate"><span class="pre">description()</span></code> and
  1237. <code class="xref py py-meth docutils literal notranslate"><span class="pre">descriptions()</span></code> methods to retrieve newsgroup descriptions for a single
  1238. group or for a range of groups. (Contributed by Jürgen A. Erhard.)</p></li>
  1239. <li><p>Two new functions were added to the <a class="reference internal" href="../library/operator.html#module-operator" title="operator: Functions corresponding to the standard operators."><code class="xref py py-mod docutils literal notranslate"><span class="pre">operator</span></code></a> module,
  1240. <code class="docutils literal notranslate"><span class="pre">attrgetter(attr)</span></code> and <code class="docutils literal notranslate"><span class="pre">itemgetter(index)</span></code>. Both functions return
  1241. callables that take a single argument and return the corresponding attribute or
  1242. item; these callables make excellent data extractors when used with <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>
  1243. or <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a>. For example:</p>
  1244. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <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;d&#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">4</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>
  1245. <span class="gp">&gt;&gt;&gt; </span><span class="nb">map</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">L</span><span class="p">)</span>
  1246. <span class="go">[&#39;c&#39;, &#39;d&#39;, &#39;a&#39;, &#39;b&#39;]</span>
  1247. <span class="gp">&gt;&gt;&gt; </span><span class="nb">map</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">L</span><span class="p">)</span>
  1248. <span class="go">[2, 1, 4, 3]</span>
  1249. <span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># Sort list by second tuple item</span>
  1250. <span class="go">[(&#39;d&#39;, 1), (&#39;c&#39;, 2), (&#39;b&#39;, 3), (&#39;a&#39;, 4)]</span>
  1251. </pre></div>
  1252. </div>
  1253. <p>(Contributed by Raymond Hettinger.)</p>
  1254. </li>
  1255. <li><p>The <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> module was updated in various ways. The module now passes
  1256. its messages through <a class="reference internal" href="../library/gettext.html#gettext.gettext" title="gettext.gettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext.gettext()</span></code></a>, making it possible to
  1257. internationalize Optik’s help and error messages. Help messages for options can
  1258. now include the string <code class="docutils literal notranslate"><span class="pre">'%default'</span></code>, which will be replaced by the option’s
  1259. default value. (Contributed by Greg Ward.)</p></li>
  1260. <li><p>The long-term plan is to deprecate the <code class="xref py py-mod docutils literal notranslate"><span class="pre">rfc822</span></code> module in some future
  1261. Python release in favor of the <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package. To this end, the
  1262. <code class="xref py py-func docutils literal notranslate"><span class="pre">email.Utils.formatdate()</span></code> function has been changed to make it usable as a
  1263. replacement for <code class="xref py py-func docutils literal notranslate"><span class="pre">rfc822.formatdate()</span></code>. You may want to write new e-mail
  1264. processing code with this in mind. (Change implemented by Anthony Baxter.)</p></li>
  1265. <li><p>A new <code class="docutils literal notranslate"><span class="pre">urandom(n)</span></code> function was added to the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module, returning
  1266. a string containing <em>n</em> bytes of random data. This function provides access to
  1267. platform-specific sources of randomness such as <code class="file docutils literal notranslate"><span class="pre">/dev/urandom</span></code> on Linux or
  1268. the Windows CryptoAPI. (Contributed by Trevor Perrin.)</p></li>
  1269. <li><p>Another new function: <code class="docutils literal notranslate"><span class="pre">os.path.lexists(path)</span></code> returns true if the file
  1270. specified by <em>path</em> exists, whether or not it’s a symbolic link. This differs
  1271. from the existing <code class="docutils literal notranslate"><span class="pre">os.path.exists(path)</span></code> function, which returns false if
  1272. <em>path</em> is a symlink that points to a destination that doesn’t exist.
  1273. (Contributed by Beni Cherniavsky.)</p></li>
  1274. <li><p>A new <code class="xref py py-func docutils literal notranslate"><span class="pre">getsid()</span></code> function was added to the <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> module that
  1275. underlies the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module. (Contributed by J. Raynor.)</p></li>
  1276. <li><p>The <a class="reference internal" href="../library/poplib.html#module-poplib" title="poplib: POP3 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">poplib</span></code></a> module now supports POP over SSL. (Contributed by Hector
  1277. Urtubia.)</p></li>
  1278. <li><p>The <a class="reference internal" href="../library/profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">profile</span></code></a> module can now profile C extension functions. (Contributed
  1279. by Nick Bastin.)</p></li>
  1280. <li><p>The <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> module has a new method called <code class="docutils literal notranslate"><span class="pre">getrandbits(N)</span></code> that
  1281. returns a long integer <em>N</em> bits in length. The existing <code class="xref py py-meth docutils literal notranslate"><span class="pre">randrange()</span></code>
  1282. method now uses <code class="xref py py-meth docutils literal notranslate"><span class="pre">getrandbits()</span></code> where appropriate, making generation of
  1283. arbitrarily large random numbers more efficient. (Contributed by Raymond
  1284. Hettinger.)</p></li>
  1285. <li><p>The regular expression language accepted 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 was extended
  1286. with simple conditional expressions, written as <code class="docutils literal notranslate"><span class="pre">(?(group)A|B)</span></code>. <em>group</em> is
  1287. either a numeric group ID or a group name defined with <code class="docutils literal notranslate"><span class="pre">(?P&lt;group&gt;...)</span></code>
  1288. earlier in the expression. If the specified group matched, the regular
  1289. expression pattern <em>A</em> will be tested against the string; if the group didn’t
  1290. match, the pattern <em>B</em> will be used instead. (Contributed by Gustavo Niemeyer.)</p></li>
  1291. <li><p>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 is also no longer recursive, thanks to a massive amount
  1292. of work by Gustavo Niemeyer. In a recursive regular expression engine, certain
  1293. patterns result in a large amount of C stack space being consumed, and it was
  1294. possible to overflow the stack. For example, if you matched a 30000-byte string
  1295. of <code class="docutils literal notranslate"><span class="pre">a</span></code> characters against the expression <code class="docutils literal notranslate"><span class="pre">(a|b)+</span></code>, one stack frame was
  1296. consumed per character. Python 2.3 tried to check for stack overflow and raise
  1297. a <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> exception, but certain patterns could sidestep the
  1298. checking and if you were unlucky Python could segfault. Python 2.4’s regular
  1299. expression engine can match this pattern without problems.</p></li>
  1300. <li><p>The <a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code></a> module now performs tighter error-checking on the parameters
  1301. to the <a class="reference internal" href="../library/signal.html#signal.signal" title="signal.signal"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a> function. For example, you can’t set a handler on
  1302. the <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGKILL</span></code> signal; previous versions of Python would quietly accept
  1303. this, but 2.4 will raise a <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> exception.</p></li>
  1304. <li><p>Two new functions were added 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> module. <code class="xref py py-func docutils literal notranslate"><span class="pre">socketpair()</span></code>
  1305. returns a pair of connected sockets and <code class="docutils literal notranslate"><span class="pre">getservbyport(port)</span></code> looks up the
  1306. service name for a given port number. (Contributed by Dave Cole and Barry
  1307. Warsaw.)</p></li>
  1308. <li><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exitfunc()</span></code> function has been deprecated. Code should be using
  1309. the existing <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, which correctly handles calling multiple exit
  1310. functions. Eventually <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exitfunc()</span></code> will become a purely internal
  1311. interface, accessed only by <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>.</p></li>
  1312. <li><p>The <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module now generates GNU-format tar files by default.
  1313. (Contributed by Lars Gustäbel.)</p></li>
  1314. <li><p>The <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module now has an elegantly simple way to support
  1315. thread-local data. The module contains a <code class="xref py py-class docutils literal notranslate"><span class="pre">local</span></code> class whose attribute
  1316. values are local to different threads.</p>
  1317. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span>
  1318. <span class="n">data</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">local</span><span class="p">()</span>
  1319. <span class="n">data</span><span class="o">.</span><span class="n">number</span> <span class="o">=</span> <span class="mi">42</span>
  1320. <span class="n">data</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
  1321. </pre></div>
  1322. </div>
  1323. <p>Other threads can assign and retrieve their own values for the <code class="xref py py-attr docutils literal notranslate"><span class="pre">number</span></code>
  1324. and <code class="xref py py-attr docutils literal notranslate"><span class="pre">url</span></code> attributes. You can subclass <code class="xref py py-class docutils literal notranslate"><span class="pre">local</span></code> to initialize
  1325. attributes or to add methods. (Contributed by Jim Fulton.)</p>
  1326. </li>
  1327. <li><p>The <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code></a> module now automatically disables periodic garbage
  1328. collection during the timing loop. This change makes consecutive timings more
  1329. comparable. (Contributed by Raymond Hettinger.)</p></li>
  1330. <li><p>The <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module now supports a wider variety of objects including
  1331. Python functions, class instances, sets, frozensets, deques, arrays, files,
  1332. sockets, and regular expression pattern objects. (Contributed by Raymond
  1333. Hettinger.)</p></li>
  1334. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code> module now supports a multi-call extension for
  1335. transmitting multiple XML-RPC calls in a single HTTP operation. (Contributed by
  1336. Brian Quinlan.)</p></li>
  1337. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">mpz</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">rotor</span></code>, and <code class="xref py py-mod docutils literal notranslate"><span class="pre">xreadlines</span></code> modules have been
  1338. removed.</p></li>
  1339. </ul>
  1340. <section id="cookielib">
  1341. <h3>cookielib<a class="headerlink" href="#cookielib" title="Permalink to this headline">¶</a></h3>
  1342. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code> library supports client-side handling for HTTP cookies,
  1343. mirroring the <code class="xref py py-mod docutils literal notranslate"><span class="pre">Cookie</span></code> module’s server-side cookie support. Cookies are
  1344. stored in cookie jars; the library transparently stores cookies offered by the
  1345. web server in the cookie jar, and fetches the cookie from the jar when
  1346. connecting to the server. As in web browsers, policy objects control whether
  1347. cookies are accepted or not.</p>
  1348. <p>In order to store cookies across sessions, two implementations of cookie jars
  1349. are provided: one that stores cookies in the Netscape format so applications can
  1350. use the Mozilla or Lynx cookie files, and one that stores cookies in the same
  1351. format as the Perl libwww library.</p>
  1352. <p><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib2</span></code> has been changed to interact with <code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code>:
  1353. <code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPCookieProcessor</span></code> manages a cookie jar that is used when accessing
  1354. URLs.</p>
  1355. <p>This module was contributed by John J. Lee.</p>
  1356. </section>
  1357. <section id="doctest">
  1358. <h3>doctest<a class="headerlink" href="#doctest" title="Permalink to this headline">¶</a></h3>
  1359. <p>The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module underwent considerable refactoring thanks to Edward
  1360. Loper and Tim Peters. Testing can still be as simple as running
  1361. <a class="reference internal" href="../library/doctest.html#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">doctest.testmod()</span></code></a>, but the refactorings allow customizing the module’s
  1362. operation in various ways</p>
  1363. <p>The new <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code> class extracts the tests from a given object’s
  1364. docstrings:</p>
  1365. <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="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  1366. <span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; f(2,2)</span>
  1367. <span class="sd">4</span>
  1368. <span class="sd">&gt;&gt;&gt; f(3,2)</span>
  1369. <span class="sd">6</span>
  1370. <span class="sd"> &quot;&quot;&quot;</span>
  1371. <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span>
  1372. <span class="n">finder</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">DocTestFinder</span><span class="p">()</span>
  1373. <span class="c1"># Get list of DocTest instances</span>
  1374. <span class="n">tests</span> <span class="o">=</span> <span class="n">finder</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  1375. </pre></div>
  1376. </div>
  1377. <p>The new <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code> class then runs individual tests and can produce
  1378. a summary of the results:</p>
  1379. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">runner</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">DocTestRunner</span><span class="p">()</span>
  1380. <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">tests</span><span class="p">:</span>
  1381. <span class="n">tried</span><span class="p">,</span> <span class="n">failed</span> <span class="o">=</span> <span class="n">runner</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
  1382. <span class="n">runner</span><span class="o">.</span><span class="n">summarize</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
  1383. </pre></div>
  1384. </div>
  1385. <p>The above example produces the following output:</p>
  1386. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">1</span> <span class="n">items</span> <span class="n">passed</span> <span class="nb">all</span> <span class="n">tests</span><span class="p">:</span>
  1387. <span class="mi">2</span> <span class="n">tests</span> <span class="ow">in</span> <span class="n">f</span>
  1388. <span class="mi">2</span> <span class="n">tests</span> <span class="ow">in</span> <span class="mi">1</span> <span class="n">items</span><span class="o">.</span>
  1389. <span class="mi">2</span> <span class="n">passed</span> <span class="ow">and</span> <span class="mi">0</span> <span class="n">failed</span><span class="o">.</span>
  1390. <span class="n">Test</span> <span class="n">passed</span><span class="o">.</span>
  1391. </pre></div>
  1392. </div>
  1393. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code> uses an instance of the <code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code> class to
  1394. compare the expected output with the actual output. This class takes a number
  1395. of different flags that customize its behaviour; ambitious users can also write
  1396. a completely new subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code>.</p>
  1397. <p>The default output checker provides a number of handy features. For example,
  1398. with the <a class="reference internal" href="../library/doctest.html#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.ELLIPSIS</span></code></a> option flag, an ellipsis (<code class="docutils literal notranslate"><span class="pre">...</span></code>) in the
  1399. expected output matches any substring, making it easier to accommodate outputs
  1400. that vary in minor ways:</p>
  1401. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">o</span> <span class="p">(</span><span class="n">n</span><span class="p">):</span>
  1402. <span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; o(1)</span>
  1403. <span class="sd">&lt;__main__.C instance at 0x...&gt;</span>
  1404. <span class="sd">&gt;&gt;&gt;</span>
  1405. <span class="sd">&quot;&quot;&quot;</span>
  1406. </pre></div>
  1407. </div>
  1408. <p>Another special string, <code class="docutils literal notranslate"><span class="pre">&lt;BLANKLINE&gt;</span></code>, matches a blank line:</p>
  1409. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">p</span> <span class="p">(</span><span class="n">n</span><span class="p">):</span>
  1410. <span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; p(1)</span>
  1411. <span class="sd">&lt;BLANKLINE&gt;</span>
  1412. <span class="sd">&gt;&gt;&gt;</span>
  1413. <span class="sd">&quot;&quot;&quot;</span>
  1414. </pre></div>
  1415. </div>
  1416. <p>Another new capability is producing a diff-style display of the output by
  1417. specifying the <a class="reference internal" href="../library/doctest.html#doctest.REPORT_UDIFF" title="doctest.REPORT_UDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_UDIFF</span></code></a> (unified diffs),
  1418. <a class="reference internal" href="../library/doctest.html#doctest.REPORT_CDIFF" title="doctest.REPORT_CDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_CDIFF</span></code></a> (context diffs), or <a class="reference internal" href="../library/doctest.html#doctest.REPORT_NDIFF" title="doctest.REPORT_NDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_NDIFF</span></code></a>
  1419. (delta-style) option flags. For example:</p>
  1420. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">g</span> <span class="p">(</span><span class="n">n</span><span class="p">):</span>
  1421. <span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; g(4)</span>
  1422. <span class="sd">here</span>
  1423. <span class="sd">is</span>
  1424. <span class="sd">a</span>
  1425. <span class="sd">lengthy</span>
  1426. <span class="sd">&gt;&gt;&gt;&quot;&quot;&quot;</span>
  1427. <span class="n">L</span> <span class="o">=</span> <span class="s1">&#39;here is a rather lengthy list of words&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
  1428. <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">L</span><span class="p">[:</span><span class="n">n</span><span class="p">]:</span>
  1429. <span class="nb">print</span> <span class="n">word</span>
  1430. </pre></div>
  1431. </div>
  1432. <p>Running the above function’s tests with <a class="reference internal" href="../library/doctest.html#doctest.REPORT_UDIFF" title="doctest.REPORT_UDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_UDIFF</span></code></a> specified,
  1433. you get the following output:</p>
  1434. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>**********************************************************************
  1435. File &quot;t.py&quot;, line 15, in g
  1436. Failed example:
  1437. g(4)
  1438. Differences (unified diff with -expected +actual):
  1439. @@ -2,3 +2,3 @@
  1440. is
  1441. a
  1442. -lengthy
  1443. +rather
  1444. **********************************************************************
  1445. </pre></div>
  1446. </div>
  1447. </section>
  1448. </section>
  1449. <section id="build-and-c-api-changes">
  1450. <h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Permalink to this headline">¶</a></h2>
  1451. <p>Some of the changes to Python’s build process and to the C API are:</p>
  1452. <ul class="simple">
  1453. <li><p>Three new convenience macros were added for common return values from
  1454. extension functions: <a class="reference internal" href="../c-api/none.html#c.Py_RETURN_NONE" title="Py_RETURN_NONE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_NONE</span></code></a>, <a class="reference internal" href="../c-api/bool.html#c.Py_RETURN_TRUE" title="Py_RETURN_TRUE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_TRUE</span></code></a>, and
  1455. <a class="reference internal" href="../c-api/bool.html#c.Py_RETURN_FALSE" title="Py_RETURN_FALSE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_FALSE</span></code></a>. (Contributed by Brett Cannon.)</p></li>
  1456. <li><p>Another new macro, <a class="reference internal" href="../c-api/refcounting.html#c.Py_CLEAR" title="Py_CLEAR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_CLEAR</span></code></a>, decreases the reference count of
  1457. <em>obj</em> and sets <em>obj</em> to the null pointer. (Contributed by Jim Fulton.)</p></li>
  1458. <li><p>A new function, <code class="docutils literal notranslate"><span class="pre">PyTuple_Pack(N,</span> <span class="pre">obj1,</span> <span class="pre">obj2,</span> <span class="pre">...,</span> <span class="pre">objN)</span></code>, constructs
  1459. tuples from a variable length argument list of Python objects. (Contributed by
  1460. Raymond Hettinger.)</p></li>
  1461. <li><p>A new function, <code class="docutils literal notranslate"><span class="pre">PyDict_Contains(d,</span> <span class="pre">k)</span></code>, implements fast dictionary
  1462. lookups without masking exceptions raised during the look-up process.
  1463. (Contributed by Raymond Hettinger.)</p></li>
  1464. <li><p>The <span class="c-expr sig sig-inline c"><span class="n">Py_IS_NAN</span><span class="p">(</span><span class="n">X</span><span class="p">)</span></span> macro returns 1 if its float or double argument
  1465. <em>X</em> is a NaN. (Contributed by Tim Peters.)</p></li>
  1466. <li><p>C code can avoid unnecessary locking by using the new
  1467. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_ThreadsInitialized()</span></code> function to tell if any thread operations
  1468. have been performed. If this function returns false, no lock operations are
  1469. needed. (Contributed by Nick Coghlan.)</p></li>
  1470. <li><p>A new function, <a class="reference internal" href="../c-api/arg.html#c.PyArg_VaParseTupleAndKeywords" title="PyArg_VaParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_VaParseTupleAndKeywords()</span></code></a>, is the same as
  1471. <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> but takes a <code class="xref c c-type docutils literal notranslate"><span class="pre">va_list</span></code> instead of a
  1472. number of arguments. (Contributed by Greg Chapman.)</p></li>
  1473. <li><p>A new method flag, <a class="reference internal" href="../c-api/structures.html#c.METH_COEXIST" title="METH_COEXIST"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_COEXIST</span></code></a>, allows a function defined in slots
  1474. to co-exist with a <a class="reference internal" href="../c-api/structures.html#c.PyCFunction" title="PyCFunction"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunction</span></code></a> having the same name. This can halve
  1475. the access time for a method such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">set.__contains__()</span></code>. (Contributed by
  1476. Raymond Hettinger.)</p></li>
  1477. <li><p>Python can now be built with additional profiling for the interpreter itself,
  1478. intended as an aid to people developing the Python core. Providing
  1479. <code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-profiling</span></code> to the <strong class="program">configure</strong> script will let you
  1480. profile the interpreter with <strong class="program">gprof</strong>, and providing the
  1481. <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-tsc</span></code> switch enables profiling using the Pentium’s
  1482. Time-Stamp-Counter register. Note that the <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-tsc</span></code> switch is slightly
  1483. misnamed, because the profiling feature also works on the PowerPC platform,
  1484. though that processor architecture doesn’t call that register “the TSC
  1485. register”. (Contributed by Jeremy Hylton.)</p></li>
  1486. <li><p>The <code class="xref c c-type docutils literal notranslate"><span class="pre">tracebackobject</span></code> type has been renamed to
  1487. <code class="xref c c-type docutils literal notranslate"><span class="pre">PyTracebackObject</span></code>.</p></li>
  1488. </ul>
  1489. <section id="port-specific-changes">
  1490. <h3>Port-Specific Changes<a class="headerlink" href="#port-specific-changes" title="Permalink to this headline">¶</a></h3>
  1491. <ul class="simple">
  1492. <li><p>The Windows port now builds under MSVC++ 7.1 as well as version 6.
  1493. (Contributed by Martin von Löwis.)</p></li>
  1494. </ul>
  1495. </section>
  1496. </section>
  1497. <section id="porting-to-python-2-4">
  1498. <h2>Porting to Python 2.4<a class="headerlink" href="#porting-to-python-2-4" title="Permalink to this headline">¶</a></h2>
  1499. <p>This section lists previously described changes that may require changes to your
  1500. code:</p>
  1501. <ul class="simple">
  1502. <li><p>Left shifts and hexadecimal/octal constants that are too large no longer
  1503. trigger a <a class="reference internal" href="../library/exceptions.html#FutureWarning" title="FutureWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FutureWarning</span></code></a> and return a value limited to 32 or 64 bits;
  1504. instead they return a long integer.</p></li>
  1505. <li><p>Integer operations will no longer trigger an <code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code>. The
  1506. <code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code> warning will disappear in Python 2.5.</p></li>
  1507. <li><p>The <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> built-in function and <code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.izip()</span></code> now return an
  1508. empty list instead of raising 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 if called with no
  1509. arguments.</p></li>
  1510. <li><p>You can no longer compare the <code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code> and <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instances
  1511. provided by the <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module. Two instances of different classes
  1512. will now always be unequal, and relative comparisons (<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>) will raise
  1513. 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>.</p></li>
  1514. <li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">dircache.listdir()</span></code> now passes exceptions to the caller instead of
  1515. returning empty lists.</p></li>
  1516. <li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">LexicalHandler.startDTD()</span></code> used to receive the public and system IDs in
  1517. the wrong order. This has been corrected; applications relying on the wrong
  1518. order need to be fixed.</p></li>
  1519. <li><p><a class="reference internal" href="../library/fcntl.html#fcntl.ioctl" title="fcntl.ioctl"><code class="xref py py-func docutils literal notranslate"><span class="pre">fcntl.ioctl()</span></code></a> now warns if the <em>mutate</em> argument is omitted and
  1520. relevant.</p></li>
  1521. <li><p>The <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module now generates GNU-format tar files by default.</p></li>
  1522. <li><p>Encountering a failure while importing a module no longer leaves a
  1523. partially initialized module object in <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>.</p></li>
  1524. <li><p><a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> is now a constant; code that binds a new value to the name
  1525. <code class="docutils literal notranslate"><span class="pre">None</span></code> is now a syntax error.</p></li>
  1526. <li><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">signals.signal()</span></code> function now raises a <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> exception
  1527. for certain illegal values; previously these errors would pass silently. For
  1528. example, you can no longer set a handler on the <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGKILL</span></code> signal.</p></li>
  1529. </ul>
  1530. </section>
  1531. <section id="acknowledgements">
  1532. <span id="acks"></span><h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Permalink to this headline">¶</a></h2>
  1533. <p>The author would like to thank the following people for offering suggestions,
  1534. corrections and assistance with various drafts of this article: Koray Can,
  1535. Hye-Shik Chang, Michael Dyck, Raymond Hettinger, Brian Hurt, Hamish Lawson,
  1536. Fredrik Lundh, Sean Reifschneider, Sadruddin Rejeb.</p>
  1537. </section>
  1538. </section>
  1539. <div class="clearer"></div>
  1540. </div>
  1541. </div>
  1542. </div>
  1543. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1544. <div class="sphinxsidebarwrapper">
  1545. <div>
  1546. <h3><a href="../contents.html">Table of Contents</a></h3>
  1547. <ul>
  1548. <li><a class="reference internal" href="#">What’s New in Python 2.4</a><ul>
  1549. <li><a class="reference internal" href="#pep-218-built-in-set-objects">PEP 218: Built-In Set Objects</a></li>
  1550. <li><a class="reference internal" href="#pep-237-unifying-long-integers-and-integers">PEP 237: Unifying Long Integers and Integers</a></li>
  1551. <li><a class="reference internal" href="#pep-289-generator-expressions">PEP 289: Generator Expressions</a></li>
  1552. <li><a class="reference internal" href="#pep-292-simpler-string-substitutions">PEP 292: Simpler String Substitutions</a></li>
  1553. <li><a class="reference internal" href="#pep-318-decorators-for-functions-and-methods">PEP 318: Decorators for Functions and Methods</a></li>
  1554. <li><a class="reference internal" href="#pep-322-reverse-iteration">PEP 322: Reverse Iteration</a></li>
  1555. <li><a class="reference internal" href="#pep-324-new-subprocess-module">PEP 324: New subprocess Module</a></li>
  1556. <li><a class="reference internal" href="#pep-327-decimal-data-type">PEP 327: Decimal Data Type</a><ul>
  1557. <li><a class="reference internal" href="#why-is-decimal-needed">Why is Decimal needed?</a></li>
  1558. <li><a class="reference internal" href="#the-decimal-type">The <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> type</a></li>
  1559. <li><a class="reference internal" href="#the-context-type">The <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> type</a></li>
  1560. </ul>
  1561. </li>
  1562. <li><a class="reference internal" href="#pep-328-multi-line-imports">PEP 328: Multi-line Imports</a></li>
  1563. <li><a class="reference internal" href="#pep-331-locale-independent-float-string-conversions">PEP 331: Locale-Independent Float/String Conversions</a></li>
  1564. <li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
  1565. <li><a class="reference internal" href="#optimizations">Optimizations</a></li>
  1566. </ul>
  1567. </li>
  1568. <li><a class="reference internal" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a><ul>
  1569. <li><a class="reference internal" href="#cookielib">cookielib</a></li>
  1570. <li><a class="reference internal" href="#doctest">doctest</a></li>
  1571. </ul>
  1572. </li>
  1573. <li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
  1574. <li><a class="reference internal" href="#port-specific-changes">Port-Specific Changes</a></li>
  1575. </ul>
  1576. </li>
  1577. <li><a class="reference internal" href="#porting-to-python-2-4">Porting to Python 2.4</a></li>
  1578. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  1579. </ul>
  1580. </li>
  1581. </ul>
  1582. </div>
  1583. <div>
  1584. <h4>Previous topic</h4>
  1585. <p class="topless"><a href="2.5.html"
  1586. title="previous chapter">What’s New in Python 2.5</a></p>
  1587. </div>
  1588. <div>
  1589. <h4>Next topic</h4>
  1590. <p class="topless"><a href="2.3.html"
  1591. title="next chapter">What’s New in Python 2.3</a></p>
  1592. </div>
  1593. <div role="note" aria-label="source link">
  1594. <h3>This Page</h3>
  1595. <ul class="this-page-menu">
  1596. <li><a href="../bugs.html">Report a Bug</a></li>
  1597. <li>
  1598. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.4.rst"
  1599. rel="nofollow">Show Source
  1600. </a>
  1601. </li>
  1602. </ul>
  1603. </div>
  1604. </div>
  1605. </div>
  1606. <div class="clearer"></div>
  1607. </div>
  1608. <div class="related" role="navigation" aria-label="related navigation">
  1609. <h3>Navigation</h3>
  1610. <ul>
  1611. <li class="right" style="margin-right: 10px">
  1612. <a href="../genindex.html" title="General Index"
  1613. >index</a></li>
  1614. <li class="right" >
  1615. <a href="../py-modindex.html" title="Python Module Index"
  1616. >modules</a> |</li>
  1617. <li class="right" >
  1618. <a href="2.3.html" title="What’s New in Python 2.3"
  1619. >next</a> |</li>
  1620. <li class="right" >
  1621. <a href="2.5.html" title="What’s New in Python 2.5"
  1622. >previous</a> |</li>
  1623. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1624. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1625. <li class="switchers">
  1626. <div class="language_switcher_placeholder"></div>
  1627. <div class="version_switcher_placeholder"></div>
  1628. </li>
  1629. <li>
  1630. </li>
  1631. <li id="cpython-language-and-version">
  1632. <a href="../index.html">3.12.0 Documentation</a> &#187;
  1633. </li>
  1634. <li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> &#187;</li>
  1635. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.4</a></li>
  1636. <li class="right">
  1637. <div class="inline-search" role="search">
  1638. <form class="inline-search" action="../search.html" method="get">
  1639. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  1640. <input type="submit" value="Go" />
  1641. </form>
  1642. </div>
  1643. |
  1644. </li>
  1645. <li class="right">
  1646. <label class="theme-selector-label">
  1647. Theme
  1648. <select class="theme-selector" oninput="activateTheme(this.value)">
  1649. <option value="auto" selected>Auto</option>
  1650. <option value="light">Light</option>
  1651. <option value="dark">Dark</option>
  1652. </select>
  1653. </label> |</li>
  1654. </ul>
  1655. </div>
  1656. <div class="footer">
  1657. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  1658. <br />
  1659. This page is licensed under the Python Software Foundation License Version 2.
  1660. <br />
  1661. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1662. <br />
  1663. See <a href="/license.html">History and License</a> for more information.<br />
  1664. <br />
  1665. The Python Software Foundation is a non-profit corporation.
  1666. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1667. <br />
  1668. <br />
  1669. Last updated on Oct 02, 2023.
  1670. <a href="/bugs.html">Found a bug</a>?
  1671. <br />
  1672. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  1673. </div>
  1674. </body>
  1675. </html>