2.1.html 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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.1" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/whatsnew/2.1.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Author, A.M. Kuchling,. Introduction: This article explains the new features in Python 2.1. While there aren’t as many changes in 2.1 as there were in Python 2.0, there are still some pleasant surp..." />
  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,. Introduction: This article explains the new features in Python 2.1. While there aren’t as many changes in 2.1 as there were in Python 2.0, there are still some pleasant surp..." />
  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.1 &#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.0" href="2.0.html" />
  34. <link rel="prev" title="What’s New in Python 2.2" href="2.2.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/whatsnew/2.1.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.1</a><ul>
  86. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  87. <li><a class="reference internal" href="#pep-227-nested-scopes">PEP 227: Nested Scopes</a></li>
  88. <li><a class="reference internal" href="#pep-236-future-directives">PEP 236: __future__ Directives</a></li>
  89. <li><a class="reference internal" href="#pep-207-rich-comparisons">PEP 207: Rich Comparisons</a></li>
  90. <li><a class="reference internal" href="#pep-230-warning-framework">PEP 230: Warning Framework</a></li>
  91. <li><a class="reference internal" href="#pep-229-new-build-system">PEP 229: New Build System</a></li>
  92. <li><a class="reference internal" href="#pep-205-weak-references">PEP 205: Weak References</a></li>
  93. <li><a class="reference internal" href="#pep-232-function-attributes">PEP 232: Function Attributes</a></li>
  94. <li><a class="reference internal" href="#pep-235-importing-modules-on-case-insensitive-platforms">PEP 235: Importing Modules on Case-Insensitive Platforms</a></li>
  95. <li><a class="reference internal" href="#pep-217-interactive-display-hook">PEP 217: Interactive Display Hook</a></li>
  96. <li><a class="reference internal" href="#pep-208-new-coercion-model">PEP 208: New Coercion Model</a></li>
  97. <li><a class="reference internal" href="#pep-241-metadata-in-python-packages">PEP 241: Metadata in Python Packages</a></li>
  98. <li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a></li>
  99. <li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
  100. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  101. </ul>
  102. </li>
  103. </ul>
  104. </div>
  105. <div>
  106. <h4>Previous topic</h4>
  107. <p class="topless"><a href="2.2.html"
  108. title="previous chapter">What’s New in Python 2.2</a></p>
  109. </div>
  110. <div>
  111. <h4>Next topic</h4>
  112. <p class="topless"><a href="2.0.html"
  113. title="next chapter">What’s New in Python 2.0</a></p>
  114. </div>
  115. <div role="note" aria-label="source link">
  116. <h3>This Page</h3>
  117. <ul class="this-page-menu">
  118. <li><a href="../bugs.html">Report a Bug</a></li>
  119. <li>
  120. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.1.rst"
  121. rel="nofollow">Show Source
  122. </a>
  123. </li>
  124. </ul>
  125. </div>
  126. </nav>
  127. </div>
  128. </div>
  129. <div class="related" role="navigation" aria-label="related navigation">
  130. <h3>Navigation</h3>
  131. <ul>
  132. <li class="right" style="margin-right: 10px">
  133. <a href="../genindex.html" title="General Index"
  134. accesskey="I">index</a></li>
  135. <li class="right" >
  136. <a href="../py-modindex.html" title="Python Module Index"
  137. >modules</a> |</li>
  138. <li class="right" >
  139. <a href="2.0.html" title="What’s New in Python 2.0"
  140. accesskey="N">next</a> |</li>
  141. <li class="right" >
  142. <a href="2.2.html" title="What’s New in Python 2.2"
  143. accesskey="P">previous</a> |</li>
  144. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  145. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  146. <li class="switchers">
  147. <div class="language_switcher_placeholder"></div>
  148. <div class="version_switcher_placeholder"></div>
  149. </li>
  150. <li>
  151. </li>
  152. <li id="cpython-language-and-version">
  153. <a href="../index.html">3.12.0 Documentation</a> &#187;
  154. </li>
  155. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> &#187;</li>
  156. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.1</a></li>
  157. <li class="right">
  158. <div class="inline-search" role="search">
  159. <form class="inline-search" action="../search.html" method="get">
  160. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  161. <input type="submit" value="Go" />
  162. </form>
  163. </div>
  164. |
  165. </li>
  166. <li class="right">
  167. <label class="theme-selector-label">
  168. Theme
  169. <select class="theme-selector" oninput="activateTheme(this.value)">
  170. <option value="auto" selected>Auto</option>
  171. <option value="light">Light</option>
  172. <option value="dark">Dark</option>
  173. </select>
  174. </label> |</li>
  175. </ul>
  176. </div>
  177. <div class="document">
  178. <div class="documentwrapper">
  179. <div class="bodywrapper">
  180. <div class="body" role="main">
  181. <section id="what-s-new-in-python-2-1">
  182. <h1>What’s New in Python 2.1<a class="headerlink" href="#what-s-new-in-python-2-1" title="Permalink to this headline">¶</a></h1>
  183. <dl class="field-list simple">
  184. <dt class="field-odd">Author</dt>
  185. <dd class="field-odd"><p>A.M. Kuchling</p>
  186. </dd>
  187. </dl>
  188. <section id="introduction">
  189. <h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
  190. <p>This article explains the new features in Python 2.1. While there aren’t as
  191. many changes in 2.1 as there were in Python 2.0, there are still some pleasant
  192. surprises in store. 2.1 is the first release to be steered through the use of
  193. Python Enhancement Proposals, or PEPs, so most of the sizable changes have
  194. accompanying PEPs that provide more complete documentation and a design
  195. rationale for the change. This article doesn’t attempt to document the new
  196. features completely, but simply provides an overview of the new features for
  197. Python programmers. Refer to the Python 2.1 documentation, or to the specific
  198. PEP, for more details about any new feature that particularly interests you.</p>
  199. <p>One recent goal of the Python development team has been to accelerate the pace
  200. of new releases, with a new release coming every 6 to 9 months. 2.1 is the first
  201. release to come out at this faster pace, with the first alpha appearing in
  202. January, 3 months after the final version of 2.0 was released.</p>
  203. <p>The final release of Python 2.1 was made on April 17, 2001.</p>
  204. </section>
  205. <section id="pep-227-nested-scopes">
  206. <h2>PEP 227: Nested Scopes<a class="headerlink" href="#pep-227-nested-scopes" title="Permalink to this headline">¶</a></h2>
  207. <p>The largest change in Python 2.1 is to Python’s scoping rules. In Python 2.0,
  208. at any given time there are at most three namespaces used to look up variable
  209. names: local, module-level, and the built-in namespace. This often surprised
  210. people because it didn’t match their intuitive expectations. For example, a
  211. nested recursive function definition doesn’t work:</p>
  212. <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>
  213. <span class="o">...</span>
  214. <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
  215. <span class="o">...</span>
  216. <span class="k">return</span> <span class="n">g</span><span class="p">(</span><span class="n">value</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
  217. <span class="o">...</span>
  218. </pre></div>
  219. </div>
  220. <p>The function <code class="xref py py-func docutils literal notranslate"><span class="pre">g()</span></code> will always raise a <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> exception, because
  221. the binding of the name <code class="docutils literal notranslate"><span class="pre">g</span></code> isn’t in either its local namespace or in the
  222. module-level namespace. This isn’t much of a problem in practice (how often do
  223. you recursively define interior functions like this?), but this also made using
  224. the <a class="reference internal" href="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a> expression clumsier, and this was a problem in practice.
  225. In code which uses <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> you can often find local variables being
  226. copied by passing them as the default values of arguments.</p>
  227. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">find</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
  228. <span class="s2">&quot;Return list of any entries equal to &#39;name&#39;&quot;</span>
  229. <span class="n">L</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">:</span> <span class="n">x</span> <span class="o">==</span> <span class="n">name</span><span class="p">,</span>
  230. <span class="bp">self</span><span class="o">.</span><span class="n">list_attribute</span><span class="p">)</span>
  231. <span class="k">return</span> <span class="n">L</span>
  232. </pre></div>
  233. </div>
  234. <p>The readability of Python code written in a strongly functional style suffers
  235. greatly as a result.</p>
  236. <p>The most significant change to Python 2.1 is that static scoping has been added
  237. to the language to fix this problem. As a first effect, the <code class="docutils literal notranslate"><span class="pre">name=name</span></code>
  238. default argument is now unnecessary in the above example. Put simply, when a
  239. given variable name is not assigned a value within a function (by an assignment,
  240. or the <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>, <a class="reference internal" href="../reference/compound_stmts.html#class"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">class</span></code></a>, or <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),
  241. references to the variable will be looked up in the local namespace of the
  242. enclosing scope. A more detailed explanation of the rules, and a dissection of
  243. the implementation, can be found in the PEP.</p>
  244. <p>This change may cause some compatibility problems for code where the same
  245. variable name is used both at the module level and as a local variable within a
  246. function that contains further function definitions. This seems rather unlikely
  247. though, since such code would have been pretty confusing to read in the first
  248. place.</p>
  249. <p>One side effect of the change is that the <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> and
  250. <code class="docutils literal notranslate"><span class="pre">exec</span></code> statements have been made illegal inside a function scope under
  251. certain conditions. The Python reference manual has said all along that <code class="docutils literal notranslate"><span class="pre">from</span>
  252. <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> is only legal at the top level of a module, but the CPython
  253. interpreter has never enforced this before. As part of the implementation of
  254. nested scopes, the compiler which turns Python source into bytecodes has to
  255. generate different code to access variables in a containing scope. <code class="docutils literal notranslate"><span class="pre">from</span>
  256. <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> and <code class="docutils literal notranslate"><span class="pre">exec</span></code> make it impossible for the compiler to
  257. figure this out, because they add names to the local namespace that are
  258. unknowable at compile time. Therefore, if a function contains function
  259. definitions or <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> expressions with free variables, the compiler
  260. will flag this by raising a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> exception.</p>
  261. <p>To make the preceding explanation a bit clearer, here’s an example:</p>
  262. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
  263. <span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
  264. <span class="c1"># The next line is a syntax error</span>
  265. <span class="n">exec</span> <span class="s1">&#39;x=2&#39;</span>
  266. <span class="k">def</span> <span class="nf">g</span><span class="p">():</span>
  267. <span class="k">return</span> <span class="n">x</span>
  268. </pre></div>
  269. </div>
  270. <p>Line 4 containing the <code class="docutils literal notranslate"><span class="pre">exec</span></code> statement is a syntax error, since
  271. <code class="docutils literal notranslate"><span class="pre">exec</span></code> would define a new local variable named <code class="docutils literal notranslate"><span class="pre">x</span></code> whose value should
  272. be accessed by <code class="xref py py-func docutils literal notranslate"><span class="pre">g()</span></code>.</p>
  273. <p>This shouldn’t be much of a limitation, since <code class="docutils literal notranslate"><span class="pre">exec</span></code> is rarely used in
  274. most Python code (and when it is used, it’s often a sign of a poor design
  275. anyway).</p>
  276. <p>Compatibility concerns have led to nested scopes being introduced gradually; in
  277. Python 2.1, they aren’t enabled by default, but can be turned on within a module
  278. by using a future statement as described in <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0236/"><strong>PEP 236</strong></a>. (See the following section
  279. for further discussion of <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0236/"><strong>PEP 236</strong></a>.) In Python 2.2, nested scopes will become
  280. the default and there will be no way to turn them off, but users will have had
  281. all of 2.1’s lifetime to fix any breakage resulting from their introduction.</p>
  282. <div class="admonition seealso">
  283. <p class="admonition-title">See also</p>
  284. <dl class="simple">
  285. <dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0227/"><strong>PEP 227</strong></a> - Statically Nested Scopes</dt><dd><p>Written and implemented by Jeremy Hylton.</p>
  286. </dd>
  287. </dl>
  288. </div>
  289. </section>
  290. <section id="pep-236-future-directives">
  291. <h2>PEP 236: __future__ Directives<a class="headerlink" href="#pep-236-future-directives" title="Permalink to this headline">¶</a></h2>
  292. <p>The reaction to nested scopes was widespread concern about the dangers of
  293. breaking code with the 2.1 release, and it was strong enough to make the
  294. Pythoneers take a more conservative approach. This approach consists of
  295. introducing a convention for enabling optional functionality in release N that
  296. will become compulsory in release N+1.</p>
  297. <p>The syntax uses a <code class="docutils literal notranslate"><span class="pre">from...import</span></code> statement using the reserved module name
  298. <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a>. Nested scopes can be enabled by the following statement:</p>
  299. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">nested_scopes</span>
  300. </pre></div>
  301. </div>
  302. <p>While it looks like a normal <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> statement, it’s not; there are
  303. strict rules on where such a future statement can be put. They can only be at
  304. the top of a module, and must precede any Python code or regular
  305. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> statements. This is because such statements can affect how
  306. the Python bytecode compiler parses code and generates bytecode, so they must
  307. precede any statement that will result in bytecodes being produced.</p>
  308. <div class="admonition seealso">
  309. <p class="admonition-title">See also</p>
  310. <dl class="simple">
  311. <dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0236/"><strong>PEP 236</strong></a> - Back to the <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a></dt><dd><p>Written by Tim Peters, and primarily implemented by Jeremy Hylton.</p>
  312. </dd>
  313. </dl>
  314. </div>
  315. </section>
  316. <section id="pep-207-rich-comparisons">
  317. <h2>PEP 207: Rich Comparisons<a class="headerlink" href="#pep-207-rich-comparisons" title="Permalink to this headline">¶</a></h2>
  318. <p>In earlier versions, Python’s support for implementing comparisons on user-defined
  319. classes and extension types was quite simple. Classes could implement a
  320. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code> method that was given two instances of a class, and could only
  321. return 0 if they were equal or +1 or -1 if they weren’t; the method couldn’t
  322. raise an exception or return anything other than a Boolean value. Users of
  323. Numeric Python often found this model too weak and restrictive, because in the
  324. number-crunching programs that numeric Python is used for, it would be more
  325. useful to be able to perform elementwise comparisons of two matrices, returning
  326. a matrix containing the results of a given comparison for each element. If the
  327. two matrices are of different sizes, then the compare has to be able to raise an
  328. exception to signal the error.</p>
  329. <p>In Python 2.1, rich comparisons were added in order to support this need.
  330. Python classes can now individually overload each of the <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>,
  331. <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">==</span></code>, and <code class="docutils literal notranslate"><span class="pre">!=</span></code> operations. The new magic method names are:</p>
  332. <table class="docutils align-default">
  333. <colgroup>
  334. <col style="width: 41%" />
  335. <col style="width: 59%" />
  336. </colgroup>
  337. <thead>
  338. <tr class="row-odd"><th class="head"><p>Operation</p></th>
  339. <th class="head"><p>Method name</p></th>
  340. </tr>
  341. </thead>
  342. <tbody>
  343. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">&lt;</span></code></p></td>
  344. <td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code></p></td>
  345. </tr>
  346. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">&lt;=</span></code></p></td>
  347. <td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__le__()</span></code></p></td>
  348. </tr>
  349. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">&gt;</span></code></p></td>
  350. <td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__gt__()</span></code></p></td>
  351. </tr>
  352. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">&gt;=</span></code></p></td>
  353. <td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__ge__()</span></code></p></td>
  354. </tr>
  355. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">==</span></code></p></td>
  356. <td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></p></td>
  357. </tr>
  358. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">!=</span></code></p></td>
  359. <td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__ne__()</span></code></p></td>
  360. </tr>
  361. </tbody>
  362. </table>
  363. <p>(The magic methods are named after the corresponding Fortran operators <code class="docutils literal notranslate"><span class="pre">.LT.</span></code>.
  364. <code class="docutils literal notranslate"><span class="pre">.LE.</span></code>, &amp;c. Numeric programmers are almost certainly quite familiar with
  365. these names and will find them easy to remember.)</p>
  366. <p>Each of these magic methods is of the form <code class="docutils literal notranslate"><span class="pre">method(self,</span> <span class="pre">other)</span></code>, where
  367. <code class="docutils literal notranslate"><span class="pre">self</span></code> will be the object on the left-hand side of the operator, while
  368. <code class="docutils literal notranslate"><span class="pre">other</span></code> will be the object on the right-hand side. For example, the
  369. expression <code class="docutils literal notranslate"><span class="pre">A</span> <span class="pre">&lt;</span> <span class="pre">B</span></code> will cause <code class="docutils literal notranslate"><span class="pre">A.__lt__(B)</span></code> to be called.</p>
  370. <p>Each of these magic methods can return anything at all: a Boolean, a matrix, a
  371. list, or any other Python object. Alternatively they can raise an exception if
  372. the comparison is impossible, inconsistent, or otherwise meaningless.</p>
  373. <p>The built-in <code class="docutils literal notranslate"><span class="pre">cmp(A,B)</span></code> function can use the rich comparison machinery,
  374. and now accepts an optional argument specifying which comparison operation to
  375. use; this is given as one of the strings <code class="docutils literal notranslate"><span class="pre">&quot;&lt;&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;&lt;=&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;&gt;&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;&gt;=&quot;</span></code>,
  376. <code class="docutils literal notranslate"><span class="pre">&quot;==&quot;</span></code>, or <code class="docutils literal notranslate"><span class="pre">&quot;!=&quot;</span></code>. If called without the optional third argument,
  377. <code class="xref py py-func docutils literal notranslate"><span class="pre">cmp()</span></code> will only return -1, 0, or +1 as in previous versions of Python;
  378. otherwise it will call the appropriate method and can return any Python object.</p>
  379. <p>There are also corresponding changes of interest to C programmers; there’s a new
  380. slot <code class="docutils literal notranslate"><span class="pre">tp_richcmp</span></code> in type objects and an API for performing a given rich
  381. comparison. I won’t cover the C API here, but will refer you to <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0207/"><strong>PEP 207</strong></a>, or to
  382. 2.1’s C API documentation, for the full list of related functions.</p>
  383. <div class="admonition seealso">
  384. <p class="admonition-title">See also</p>
  385. <dl class="simple">
  386. <dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0207/"><strong>PEP 207</strong></a> - Rich Comparisons</dt><dd><p>Written by Guido van Rossum, heavily based on earlier work by David Ascher, and
  387. implemented by Guido van Rossum.</p>
  388. </dd>
  389. </dl>
  390. </div>
  391. </section>
  392. <section id="pep-230-warning-framework">
  393. <h2>PEP 230: Warning Framework<a class="headerlink" href="#pep-230-warning-framework" title="Permalink to this headline">¶</a></h2>
  394. <p>Over its 10 years of existence, Python has accumulated a certain number of
  395. obsolete modules and features along the way. It’s difficult to know when a
  396. feature is safe to remove, since there’s no way of knowing how much code uses it
  397. — perhaps no programs depend on the feature, or perhaps many do. To enable
  398. removing old features in a more structured way, a warning framework was added.
  399. When the Python developers want to get rid of a feature, it will first trigger a
  400. warning in the next version of Python. The following Python version can then
  401. drop the feature, and users will have had a full release cycle to remove uses of
  402. the old feature.</p>
  403. <p>Python 2.1 adds the warning framework to be used in this scheme. It adds a
  404. <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> module that provide functions to issue warnings, and to filter
  405. out warnings that you don’t want to be displayed. Third-party modules can also
  406. use this framework to deprecate old features that they no longer wish to
  407. support.</p>
  408. <p>For example, in Python 2.1 the <code class="xref py py-mod docutils literal notranslate"><span class="pre">regex</span></code> module is deprecated, so importing
  409. it causes a warning to be printed:</p>
  410. <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">regex</span>
  411. <span class="go">__main__:1: DeprecationWarning: the regex module</span>
  412. <span class="go"> is deprecated; please use the re module</span>
  413. <span class="gp">&gt;&gt;&gt;</span>
  414. </pre></div>
  415. </div>
  416. <p>Warnings can be issued by calling the <a class="reference internal" href="../library/warnings.html#warnings.warn" title="warnings.warn"><code class="xref py py-func docutils literal notranslate"><span class="pre">warnings.warn()</span></code></a> function:</p>
  417. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;feature X no longer supported&quot;</span><span class="p">)</span>
  418. </pre></div>
  419. </div>
  420. <p>The first parameter is the warning message; an additional optional parameters
  421. can be used to specify a particular warning category.</p>
  422. <p>Filters can be added to disable certain warnings; a regular expression pattern
  423. can be applied to the message or to the module name in order to suppress a
  424. warning. For example, you may have a program that uses the <code class="xref py py-mod docutils literal notranslate"><span class="pre">regex</span></code> module
  425. and not want to spare the time to convert it to use 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 right
  426. now. The warning can be suppressed by calling</p>
  427. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">warnings</span>
  428. <span class="n">warnings</span><span class="o">.</span><span class="n">filterwarnings</span><span class="p">(</span><span class="n">action</span> <span class="o">=</span> <span class="s1">&#39;ignore&#39;</span><span class="p">,</span>
  429. <span class="n">message</span><span class="o">=</span><span class="s1">&#39;.*regex module is deprecated&#39;</span><span class="p">,</span>
  430. <span class="n">category</span><span class="o">=</span><span class="ne">DeprecationWarning</span><span class="p">,</span>
  431. <span class="n">module</span> <span class="o">=</span> <span class="s1">&#39;__main__&#39;</span><span class="p">)</span>
  432. </pre></div>
  433. </div>
  434. <p>This adds a filter that will apply only to warnings of the class
  435. <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> triggered in the <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> module, and applies
  436. a regular expression to only match the message about the <code class="xref py py-mod docutils literal notranslate"><span class="pre">regex</span></code> module
  437. being deprecated, and will cause such warnings to be ignored. Warnings can also
  438. be printed only once, printed every time the offending code is executed, or
  439. turned into exceptions that will cause the program to stop (unless the
  440. exceptions are caught in the usual way, of course).</p>
  441. <p>Functions were also added to Python’s C API for issuing warnings; refer to PEP
  442. 230 or to Python’s API documentation for the details.</p>
  443. <div class="admonition seealso">
  444. <p class="admonition-title">See also</p>
  445. <dl class="simple">
  446. <dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0005/"><strong>PEP 5</strong></a> - Guidelines for Language Evolution</dt><dd><p>Written by Paul Prescod, to specify procedures to be followed when removing old
  447. features from Python. The policy described in this PEP hasn’t been officially
  448. adopted, but the eventual policy probably won’t be too different from Prescod’s
  449. proposal.</p>
  450. </dd>
  451. <dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0230/"><strong>PEP 230</strong></a> - Warning Framework</dt><dd><p>Written and implemented by Guido van Rossum.</p>
  452. </dd>
  453. </dl>
  454. </div>
  455. </section>
  456. <section id="pep-229-new-build-system">
  457. <h2>PEP 229: New Build System<a class="headerlink" href="#pep-229-new-build-system" title="Permalink to this headline">¶</a></h2>
  458. <p>When compiling Python, the user had to go in and edit the <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code>
  459. file in order to enable various additional modules; the default set is
  460. relatively small and limited to modules that compile on most Unix platforms.
  461. This means that on Unix platforms with many more features, most notably Linux,
  462. Python installations often don’t contain all useful modules they could.</p>
  463. <p>Python 2.0 added the Distutils, a set of modules for distributing and installing
  464. extensions. In Python 2.1, the Distutils are used to compile much of the
  465. standard library of extension modules, autodetecting which ones are supported on
  466. the current machine. It’s hoped that this will make Python installations easier
  467. and more featureful.</p>
  468. <p>Instead of having to edit the <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code> file in order to enable
  469. modules, a <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> script in the top directory of the Python source
  470. distribution is run at build time, and attempts to discover which modules can be
  471. enabled by examining the modules and header files on the system. If a module is
  472. configured in <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code>, the <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> script won’t attempt
  473. to compile that module and will defer to the <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code> file’s
  474. contents. This provides a way to specific any strange command-line flags or
  475. libraries that are required for a specific platform.</p>
  476. <p>In another far-reaching change to the build mechanism, Neil Schemenauer
  477. restructured things so Python now uses a single makefile that isn’t recursive,
  478. instead of makefiles in the top directory and in each of the <code class="file docutils literal notranslate"><span class="pre">Python/</span></code>,
  479. <code class="file docutils literal notranslate"><span class="pre">Parser/</span></code>, <code class="file docutils literal notranslate"><span class="pre">Objects/</span></code>, and <code class="file docutils literal notranslate"><span class="pre">Modules/</span></code> subdirectories. This
  480. makes building Python faster and also makes hacking the Makefiles clearer and
  481. simpler.</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-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0229/"><strong>PEP 229</strong></a> - Using Distutils to Build Python</dt><dd><p>Written and implemented by A.M. Kuchling.</p>
  486. </dd>
  487. </dl>
  488. </div>
  489. </section>
  490. <section id="pep-205-weak-references">
  491. <h2>PEP 205: Weak References<a class="headerlink" href="#pep-205-weak-references" title="Permalink to this headline">¶</a></h2>
  492. <p>Weak references, available through 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, are a minor but
  493. useful new data type in the Python programmer’s toolbox.</p>
  494. <p>Storing a reference to an object (say, in a dictionary or a list) has the side
  495. effect of keeping that object alive forever. There are a few specific cases
  496. where this behaviour is undesirable, object caches being the most common one,
  497. and another being circular references in data structures such as trees.</p>
  498. <p>For example, consider a memoizing function that caches the results of another
  499. function <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> by storing the function’s argument and its result in a
  500. dictionary:</p>
  501. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">_cache</span> <span class="o">=</span> <span class="p">{}</span>
  502. <span class="k">def</span> <span class="nf">memoize</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  503. <span class="k">if</span> <span class="n">_cache</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  504. <span class="k">return</span> <span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]</span>
  505. <span class="n">retval</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
  506. <span class="c1"># Cache the returned object</span>
  507. <span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">retval</span>
  508. <span class="k">return</span> <span class="n">retval</span>
  509. </pre></div>
  510. </div>
  511. <p>This version works for simple things such as integers, but it has a side effect;
  512. the <code class="docutils literal notranslate"><span class="pre">_cache</span></code> dictionary holds a reference to the return values, so they’ll
  513. never be deallocated until the Python process exits and cleans up. This isn’t
  514. very noticeable for integers, but if <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code> returns an object, or a data
  515. structure that takes up a lot of memory, this can be a problem.</p>
  516. <p>Weak references provide a way to implement a cache that won’t keep objects alive
  517. beyond their time. If an object is only accessible through weak references, the
  518. object will be deallocated and the weak references will now indicate that the
  519. object it referred to no longer exists. A weak reference to an object <em>obj</em> is
  520. created by calling <code class="docutils literal notranslate"><span class="pre">wr</span> <span class="pre">=</span> <span class="pre">weakref.ref(obj)</span></code>. The object being referred to is
  521. returned by calling the weak reference as if it were a function: <code class="docutils literal notranslate"><span class="pre">wr()</span></code>. It
  522. will return the referenced object, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the object no longer exists.</p>
  523. <p>This makes it possible to write a <code class="xref py py-func docutils literal notranslate"><span class="pre">memoize()</span></code> function whose cache doesn’t
  524. keep objects alive, by storing weak references in the cache.</p>
  525. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">_cache</span> <span class="o">=</span> <span class="p">{}</span>
  526. <span class="k">def</span> <span class="nf">memoize</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  527. <span class="k">if</span> <span class="n">_cache</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  528. <span class="n">obj</span> <span class="o">=</span> <span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]()</span>
  529. <span class="c1"># If weak reference object still exists,</span>
  530. <span class="c1"># return it</span>
  531. <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="k">return</span> <span class="n">obj</span>
  532. <span class="n">retval</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
  533. <span class="c1"># Cache a weak reference</span>
  534. <span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ref</span><span class="p">(</span><span class="n">retval</span><span class="p">)</span>
  535. <span class="k">return</span> <span class="n">retval</span>
  536. </pre></div>
  537. </div>
  538. <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 also allows creating proxy objects which behave like
  539. weak references — an object referenced only by proxy objects is deallocated –
  540. but instead of requiring an explicit call to retrieve the object, the proxy
  541. transparently forwards all operations to the object as long as the object still
  542. exists. If the object is deallocated, attempting to use a proxy will cause a
  543. <code class="xref py py-exc docutils literal notranslate"><span class="pre">weakref.ReferenceError</span></code> exception to be raised.</p>
  544. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">proxy</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">proxy</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
  545. <span class="n">proxy</span><span class="o">.</span><span class="n">attr</span> <span class="c1"># Equivalent to obj.attr</span>
  546. <span class="n">proxy</span><span class="o">.</span><span class="n">meth</span><span class="p">()</span> <span class="c1"># Equivalent to obj.meth()</span>
  547. <span class="k">del</span> <span class="n">obj</span>
  548. <span class="n">proxy</span><span class="o">.</span><span class="n">attr</span> <span class="c1"># raises weakref.ReferenceError</span>
  549. </pre></div>
  550. </div>
  551. <div class="admonition seealso">
  552. <p class="admonition-title">See also</p>
  553. <dl class="simple">
  554. <dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0205/"><strong>PEP 205</strong></a> - Weak References</dt><dd><p>Written and implemented by Fred L. Drake, Jr.</p>
  555. </dd>
  556. </dl>
  557. </div>
  558. </section>
  559. <section id="pep-232-function-attributes">
  560. <h2>PEP 232: Function Attributes<a class="headerlink" href="#pep-232-function-attributes" title="Permalink to this headline">¶</a></h2>
  561. <p>In Python 2.1, functions can now have arbitrary information attached to them.
  562. People were often using docstrings to hold information about functions and
  563. methods, because the <code class="docutils literal notranslate"><span class="pre">__doc__</span></code> attribute was the only way of attaching any
  564. information to a function. For example, in the Zope web application server,
  565. functions are marked as safe for public access by having a docstring, and in
  566. John Aycock’s SPARK parsing framework, docstrings hold parts of the BNF grammar
  567. to be parsed. This overloading is unfortunate, since docstrings are really
  568. intended to hold a function’s documentation; for example, it means you can’t
  569. properly document functions intended for private use in Zope.</p>
  570. <p>Arbitrary attributes can now be set and retrieved on functions using the regular
  571. Python syntax:</p>
  572. <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="k">pass</span>
  573. <span class="n">f</span><span class="o">.</span><span class="n">publish</span> <span class="o">=</span> <span class="mi">1</span>
  574. <span class="n">f</span><span class="o">.</span><span class="n">secure</span> <span class="o">=</span> <span class="mi">1</span>
  575. <span class="n">f</span><span class="o">.</span><span class="n">grammar</span> <span class="o">=</span> <span class="s2">&quot;A ::= B (C D)*&quot;</span>
  576. </pre></div>
  577. </div>
  578. <p>The dictionary containing attributes can be accessed as the function’s
  579. <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>. Unlike the <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> attribute of class instances, in
  580. functions you can actually assign a new dictionary to <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>, though
  581. the new value is restricted to a regular Python dictionary; you <em>can’t</em> be
  582. tricky and set it to a <code class="xref py py-class docutils literal notranslate"><span class="pre">UserDict</span></code> instance, or any other random object
  583. that behaves like a mapping.</p>
  584. <div class="admonition seealso">
  585. <p class="admonition-title">See also</p>
  586. <dl class="simple">
  587. <dt><span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0232/"><strong>PEP 232</strong></a> - Function Attributes</dt><dd><p>Written and implemented by Barry Warsaw.</p>
  588. </dd>
  589. </dl>
  590. </div>
  591. </section>
  592. <section id="pep-235-importing-modules-on-case-insensitive-platforms">
  593. <h2>PEP 235: Importing Modules on Case-Insensitive Platforms<a class="headerlink" href="#pep-235-importing-modules-on-case-insensitive-platforms" title="Permalink to this headline">¶</a></h2>
  594. <p>Some operating systems have filesystems that are case-insensitive, MacOS and
  595. Windows being the primary examples; on these systems, it’s impossible to
  596. distinguish the filenames <code class="docutils literal notranslate"><span class="pre">FILE.PY</span></code> and <code class="docutils literal notranslate"><span class="pre">file.py</span></code>, even though they do store
  597. the file’s name in its original case (they’re case-preserving, too).</p>
  598. <p>In Python 2.1, the <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> statement will work to simulate case-sensitivity
  599. on case-insensitive platforms. Python will now search for the first
  600. case-sensitive match by default, raising an <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> if no such file
  601. is found, so <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">file</span></code> will not import a module named <code class="docutils literal notranslate"><span class="pre">FILE.PY</span></code>.
  602. Case-insensitive matching can be requested by setting the <span class="target" id="index-11"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONCASEOK"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONCASEOK</span></code></a>
  603. environment variable before starting the Python interpreter.</p>
  604. </section>
  605. <section id="pep-217-interactive-display-hook">
  606. <h2>PEP 217: Interactive Display Hook<a class="headerlink" href="#pep-217-interactive-display-hook" title="Permalink to this headline">¶</a></h2>
  607. <p>When using the Python interpreter interactively, the output of commands is
  608. displayed using the built-in <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> function. In Python 2.1, the variable
  609. <a class="reference internal" href="../library/sys.html#sys.displayhook" title="sys.displayhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.displayhook()</span></code></a> can be set to a callable object which will be called
  610. instead of <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a>. For example, you can set it to a special
  611. pretty-printing function:</p>
  612. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Create a recursive data structure</span>
  613. <span class="gp">... </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>
  614. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">L</span><span class="p">)</span>
  615. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="c1"># Show Python&#39;s default output</span>
  616. <span class="go">[1, 2, 3, [...]]</span>
  617. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Use pprint.pprint() as the display function</span>
  618. <span class="gp">... </span><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">pprint</span>
  619. <span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">displayhook</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span>
  620. <span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
  621. <span class="go">[1, 2, 3, &lt;Recursion on list with id=135143996&gt;]</span>
  622. <span class="gp">&gt;&gt;&gt;</span>
  623. </pre></div>
  624. </div>
  625. <div class="admonition seealso">
  626. <p class="admonition-title">See also</p>
  627. <dl class="simple">
  628. <dt><span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0217/"><strong>PEP 217</strong></a> - Display Hook for Interactive Use</dt><dd><p>Written and implemented by Moshe Zadka.</p>
  629. </dd>
  630. </dl>
  631. </div>
  632. </section>
  633. <section id="pep-208-new-coercion-model">
  634. <h2>PEP 208: New Coercion Model<a class="headerlink" href="#pep-208-new-coercion-model" title="Permalink to this headline">¶</a></h2>
  635. <p>How numeric coercion is done at the C level was significantly modified. This
  636. will only affect the authors of C extensions to Python, allowing them more
  637. flexibility in writing extension types that support numeric operations.</p>
  638. <p>Extension types can now set the type flag <code class="docutils literal notranslate"><span class="pre">Py_TPFLAGS_CHECKTYPES</span></code> in their
  639. <code class="docutils literal notranslate"><span class="pre">PyTypeObject</span></code> structure to indicate that they support the new coercion model.
  640. In such extension types, the numeric slot functions can no longer assume that
  641. they’ll be passed two arguments of the same type; instead they may be passed two
  642. arguments of differing types, and can then perform their own internal coercion.
  643. If the slot function is passed a type it can’t handle, it can indicate the
  644. failure by returning a reference to the <code class="docutils literal notranslate"><span class="pre">Py_NotImplemented</span></code> singleton value.
  645. The numeric functions of the other type will then be tried, and perhaps they can
  646. handle the operation; if the other type also returns <code class="docutils literal notranslate"><span class="pre">Py_NotImplemented</span></code>, then
  647. 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> will be raised. Numeric methods written in Python can also
  648. return <code class="docutils literal notranslate"><span class="pre">Py_NotImplemented</span></code>, causing the interpreter to act as if the method
  649. did not exist (perhaps 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>, perhaps trying another
  650. object’s numeric methods).</p>
  651. <div class="admonition seealso">
  652. <p class="admonition-title">See also</p>
  653. <dl class="simple">
  654. <dt><span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-0208/"><strong>PEP 208</strong></a> - Reworking the Coercion Model</dt><dd><p>Written and implemented by Neil Schemenauer, heavily based upon earlier work by
  655. Marc-André Lemburg. Read this to understand the fine points of how numeric
  656. operations will now be processed at the C level.</p>
  657. </dd>
  658. </dl>
  659. </div>
  660. </section>
  661. <section id="pep-241-metadata-in-python-packages">
  662. <h2>PEP 241: Metadata in Python Packages<a class="headerlink" href="#pep-241-metadata-in-python-packages" title="Permalink to this headline">¶</a></h2>
  663. <p>A common complaint from Python users is that there’s no single catalog of all
  664. the Python modules in existence. T. Middleton’s Vaults of Parnassus at
  665. <code class="docutils literal notranslate"><span class="pre">www.vex.net/parnassus/</span></code> (retired in February 2009, <a class="reference external" href="https://web.archive.org/web/20090130140102/http://www.vex.net/parnassus/">available in the
  666. Internet Archive Wayback Machine</a>)
  667. was the largest catalog of Python modules, but
  668. registering software at the Vaults is optional, and many people did not bother.</p>
  669. <p>As a first small step toward fixing the problem, Python software packaged using
  670. the Distutils <strong class="command">sdist</strong> command will include a file named
  671. <code class="file docutils literal notranslate"><span class="pre">PKG-INFO</span></code> containing information about the package such as its name,
  672. version, and author (metadata, in cataloguing terminology). <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0241/"><strong>PEP 241</strong></a> contains
  673. the full list of fields that can be present in the <code class="file docutils literal notranslate"><span class="pre">PKG-INFO</span></code> file. As
  674. people began to package their software using Python 2.1, more and more packages
  675. will include metadata, making it possible to build automated cataloguing systems
  676. and experiment with them. With the result experience, perhaps it’ll be possible
  677. to design a really good catalog and then build support for it into Python 2.2.
  678. For example, the Distutils <strong class="command">sdist</strong> and <strong class="command">bdist_*</strong> commands
  679. could support an <code class="docutils literal notranslate"><span class="pre">upload</span></code> option that would automatically upload your
  680. package to a catalog server.</p>
  681. <p>You can start creating packages containing <code class="file docutils literal notranslate"><span class="pre">PKG-INFO</span></code> even if you’re not
  682. using Python 2.1, since a new release of the Distutils will be made for users of
  683. earlier Python versions. Version 1.0.2 of the Distutils includes the changes
  684. described in <span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0241/"><strong>PEP 241</strong></a>, as well as various bugfixes and enhancements. It will be
  685. available from the Distutils SIG at <a class="reference external" href="https://www.python.org/community/sigs/current/distutils-sig/">https://www.python.org/community/sigs/current/distutils-sig/</a>.</p>
  686. <div class="admonition seealso">
  687. <p class="admonition-title">See also</p>
  688. <dl class="simple">
  689. <dt><span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-0241/"><strong>PEP 241</strong></a> - Metadata for Python Software Packages</dt><dd><p>Written and implemented by A.M. Kuchling.</p>
  690. </dd>
  691. <dt><span class="target" id="index-17"></span><a class="pep reference external" href="https://peps.python.org/pep-0243/"><strong>PEP 243</strong></a> - Module Repository Upload Mechanism</dt><dd><p>Written by Sean Reifschneider, this draft PEP describes a proposed mechanism for
  692. uploading Python packages to a central server.</p>
  693. </dd>
  694. </dl>
  695. </div>
  696. </section>
  697. <section id="new-and-improved-modules">
  698. <h2>New and Improved Modules<a class="headerlink" href="#new-and-improved-modules" title="Permalink to this headline">¶</a></h2>
  699. <ul>
  700. <li><p>Ka-Ping Yee contributed two new modules: <code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect.py</span></code>, a module for
  701. getting information about live Python code, and <code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc.py</span></code>, a module for
  702. interactively converting docstrings to HTML or text. As a bonus,
  703. <code class="file docutils literal notranslate"><span class="pre">Tools/scripts/pydoc</span></code>, which is now automatically installed, uses
  704. <code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc.py</span></code> to display documentation given a Python module, package, or
  705. class name. For example, <code class="docutils literal notranslate"><span class="pre">pydoc</span> <span class="pre">xml.dom</span></code> displays the following:</p>
  706. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Python</span> <span class="n">Library</span> <span class="n">Documentation</span><span class="p">:</span> <span class="n">package</span> <span class="n">xml</span><span class="o">.</span><span class="n">dom</span> <span class="ow">in</span> <span class="n">xml</span>
  707. <span class="n">NAME</span>
  708. <span class="n">xml</span><span class="o">.</span><span class="n">dom</span> <span class="o">-</span> <span class="n">W3C</span> <span class="n">Document</span> <span class="n">Object</span> <span class="n">Model</span> <span class="n">implementation</span> <span class="k">for</span> <span class="n">Python</span><span class="o">.</span>
  709. <span class="n">FILE</span>
  710. <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python2</span><span class="mf">.1</span><span class="o">/</span><span class="n">xml</span><span class="o">/</span><span class="n">dom</span><span class="o">/</span><span class="fm">__init__</span><span class="o">.</span><span class="n">pyc</span>
  711. <span class="n">DESCRIPTION</span>
  712. <span class="n">The</span> <span class="n">Python</span> <span class="n">mapping</span> <span class="n">of</span> <span class="n">the</span> <span class="n">Document</span> <span class="n">Object</span> <span class="n">Model</span> <span class="ow">is</span> <span class="n">documented</span> <span class="ow">in</span> <span class="n">the</span>
  713. <span class="n">Python</span> <span class="n">Library</span> <span class="n">Reference</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">section</span> <span class="n">on</span> <span class="n">the</span> <span class="n">xml</span><span class="o">.</span><span class="n">dom</span> <span class="n">package</span><span class="o">.</span>
  714. <span class="n">This</span> <span class="n">package</span> <span class="n">contains</span> <span class="n">the</span> <span class="n">following</span> <span class="n">modules</span><span class="p">:</span>
  715. <span class="o">...</span>
  716. </pre></div>
  717. </div>
  718. <p><code class="file docutils literal notranslate"><span class="pre">pydoc</span></code> also includes a Tk-based interactive help browser. <code class="file docutils literal notranslate"><span class="pre">pydoc</span></code>
  719. quickly becomes addictive; try it out!</p>
  720. </li>
  721. <li><p>Two different modules for unit testing were added to the standard library.
  722. 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, contributed by Tim Peters, provides a testing
  723. framework based on running embedded examples in docstrings and comparing the
  724. results against the expected output. PyUnit, contributed by Steve Purcell, is a
  725. unit testing framework inspired by JUnit, which was in turn an adaptation of
  726. Kent Beck’s Smalltalk testing framework. See <a class="reference external" href="https://pyunit.sourceforge.net/">https://pyunit.sourceforge.net/</a> for
  727. more information about PyUnit.</p></li>
  728. <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 contains a class, <code class="xref py py-class docutils literal notranslate"><span class="pre">SequenceMatcher</span></code>, which
  729. compares two sequences and computes the changes required to transform one
  730. sequence into the other. For example, this module can be used to write a tool
  731. similar to the Unix <strong class="program">diff</strong> program, and in fact the sample program
  732. <code class="file docutils literal notranslate"><span class="pre">Tools/scripts/ndiff.py</span></code> demonstrates how to write such a script.</p></li>
  733. <li><p><a class="reference internal" href="../library/curses.panel.html#module-curses.panel" title="curses.panel: A panel stack extension that adds depth to curses windows."><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses.panel</span></code></a>, a wrapper for the panel library, part of ncurses and of
  734. SYSV curses, was contributed by Thomas Gellekum. The panel library provides
  735. windows with the additional feature of depth. Windows can be moved higher or
  736. lower in the depth ordering, and the panel library figures out where panels
  737. overlap and which sections are visible.</p></li>
  738. <li><p>The PyXML package has gone through a few releases since Python 2.0, and Python
  739. 2.1 includes an updated version of the <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package. Some of the
  740. noteworthy changes include support for Expat 1.2 and later versions, the ability
  741. for Expat parsers to handle files in any encoding supported by Python, and
  742. various bugfixes for SAX, DOM, and the <code class="xref py py-mod docutils literal notranslate"><span class="pre">minidom</span></code> module.</p></li>
  743. <li><p>Ping also contributed another hook for handling uncaught exceptions.
  744. <a class="reference internal" href="../library/sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.excepthook()</span></code></a> can be set to a callable object. When an exception isn’t
  745. caught by any <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>…<a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> blocks, the exception will be
  746. passed to <a class="reference internal" href="../library/sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.excepthook()</span></code></a>, which can then do whatever it likes. At the
  747. Ninth Python Conference, Ping demonstrated an application for this hook:
  748. printing an extended traceback that not only lists the stack frames, but also
  749. lists the function arguments and the local variables for each frame.</p></li>
  750. <li><p>Various functions in the <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> module, such as <code class="xref py py-func docutils literal notranslate"><span class="pre">asctime()</span></code> and
  751. <code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code>, require a floating point argument containing the time in
  752. seconds since the epoch. The most common use of these functions is to work with
  753. the current time, so the floating point argument has been made optional; when a
  754. value isn’t provided, the current time will be used. For example, log file
  755. entries usually need a string containing the current time; in Python 2.1,
  756. <code class="docutils literal notranslate"><span class="pre">time.asctime()</span></code> can be used, instead of the lengthier
  757. <code class="docutils literal notranslate"><span class="pre">time.asctime(time.localtime(time.time()))</span></code> that was previously required.</p>
  758. <p>This change was proposed and implemented by Thomas Wouters.</p>
  759. </li>
  760. <li><p>The <a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> module now defaults to retrieving files in passive mode,
  761. because passive mode is more likely to work from behind a firewall. This
  762. request came from the Debian bug tracking system, since other Debian packages
  763. use <a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> to retrieve files and then don’t work from behind a firewall.
  764. It’s deemed unlikely that this will cause problems for anyone, because Netscape
  765. defaults to passive mode and few people complain, but if passive mode is
  766. unsuitable for your application or network setup, call <code class="docutils literal notranslate"><span class="pre">set_pasv(0)</span></code> on
  767. FTP objects to disable passive mode.</p></li>
  768. <li><p>Support for raw socket access has been 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,
  769. contributed by Grant Edwards.</p></li>
  770. <li><p>The <a class="reference internal" href="../library/profile.html#module-pstats" title="pstats: Statistics object for use with the profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pstats</span></code></a> module now contains a simple interactive statistics browser
  771. for displaying timing profiles for Python programs, invoked when the module is
  772. run as a script. Contributed by Eric S. Raymond.</p></li>
  773. <li><p>A new implementation-dependent function, <code class="docutils literal notranslate"><span class="pre">sys._getframe([depth])</span></code>, has
  774. been added to return a given frame object from the current call stack.
  775. <a class="reference internal" href="../library/sys.html#sys._getframe" title="sys._getframe"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys._getframe()</span></code></a> returns the frame at the top of the call stack; if the
  776. optional integer argument <em>depth</em> is supplied, the function returns the frame
  777. that is <em>depth</em> calls below the top of the stack. For example,
  778. <code class="docutils literal notranslate"><span class="pre">sys._getframe(1)</span></code> returns the caller’s frame object.</p>
  779. <p>This function is only present in CPython, not in Jython or the .NET
  780. implementation. Use it for debugging, and resist the temptation to put it into
  781. production code.</p>
  782. </li>
  783. </ul>
  784. </section>
  785. <section id="other-changes-and-fixes">
  786. <h2>Other Changes and Fixes<a class="headerlink" href="#other-changes-and-fixes" title="Permalink to this headline">¶</a></h2>
  787. <p>There were relatively few smaller changes made in Python 2.1 due to the shorter
  788. release cycle. A search through the CVS change logs turns up 117 patches
  789. applied, and 136 bugs fixed; both figures are likely to be underestimates. Some
  790. of the more notable changes are:</p>
  791. <ul>
  792. <li><p>A specialized object allocator is now optionally available, that should be
  793. faster than the system <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> and have less memory overhead. The
  794. allocator uses C’s <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> function to get large pools of memory, and
  795. then fulfills smaller memory requests from these pools. It can be enabled by
  796. providing the <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-pymalloc</span></code> option to the <strong class="program">configure</strong>
  797. script; see <code class="file docutils literal notranslate"><span class="pre">Objects/obmalloc.c</span></code> for the implementation details.</p>
  798. <p>Authors of C extension modules should test their code with the object allocator
  799. enabled, because some incorrect code may break, causing core dumps at runtime.
  800. There are a bunch of memory allocation functions in Python’s C API that have
  801. previously been just aliases for the C library’s <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> and
  802. <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code>, meaning that if you accidentally called mismatched functions, the
  803. error wouldn’t be noticeable. When the object allocator is enabled, these
  804. functions aren’t aliases of <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code> any more, and
  805. calling the wrong function to free memory will get you a core dump. For
  806. example, if memory was allocated using <a class="reference internal" href="../c-api/memory.html#c.PyMem_New" title="PyMem_New"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyMem_New</span></code></a>, it has to be freed
  807. using <a class="reference internal" href="../c-api/memory.html#c.PyMem_Del" title="PyMem_Del"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Del()</span></code></a>, not <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code>. A few modules included with Python
  808. fell afoul of this and had to be fixed; doubtless there are more third-party
  809. modules that will have the same problem.</p>
  810. <p>The object allocator was contributed by Vladimir Marangozov.</p>
  811. </li>
  812. <li><p>The speed of line-oriented file I/O has been improved because people often
  813. complain about its lack of speed, and because it’s often been used as a naïve
  814. benchmark. The <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> method of file objects has therefore been
  815. rewritten to be much faster. The exact amount of the speedup will vary from
  816. platform to platform depending on how slow the C library’s <code class="xref c c-func docutils literal notranslate"><span class="pre">getc()</span></code> was, but
  817. is around 66%, and potentially much faster on some particular operating systems.
  818. Tim Peters did much of the benchmarking and coding for this change, motivated by
  819. a discussion in comp.lang.python.</p>
  820. <p>A new module and method for file objects was also added, contributed by Jeff
  821. Epler. The new method, <code class="xref py py-meth docutils literal notranslate"><span class="pre">xreadlines()</span></code>, is similar to the existing
  822. <code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code> built-in. <code class="xref py py-func docutils literal notranslate"><span class="pre">xreadlines()</span></code> returns an opaque sequence object
  823. that only supports being iterated over, reading a line on every iteration but
  824. not reading the entire file into memory as the existing <code class="xref py py-meth docutils literal notranslate"><span class="pre">readlines()</span></code> method
  825. does. You’d use it like this:</p>
  826. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">xreadlines</span><span class="p">():</span>
  827. <span class="c1"># ... do something for each line ...</span>
  828. <span class="o">...</span>
  829. </pre></div>
  830. </div>
  831. <p>For a fuller discussion of the line I/O changes, see the python-dev summary for
  832. January 1–15, 2001 at <a class="reference external" href="https://mail.python.org/pipermail/python-dev/2001-January/">https://mail.python.org/pipermail/python-dev/2001-January/</a>.</p>
  833. </li>
  834. <li><p>A new method, <code class="xref py py-meth docutils literal notranslate"><span class="pre">popitem()</span></code>, was added to dictionaries to enable
  835. destructively iterating through the contents of a dictionary; this can be faster
  836. for large dictionaries because there’s no need to construct a list containing
  837. all the keys or values. <code class="docutils literal notranslate"><span class="pre">D.popitem()</span></code> removes a random <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code> pair
  838. from the dictionary <code class="docutils literal notranslate"><span class="pre">D</span></code> and returns it as a 2-tuple. This was implemented
  839. mostly by Tim Peters and Guido van Rossum, after a suggestion and preliminary
  840. patch by Moshe Zadka.</p></li>
  841. <li><p>Modules can now control which names are imported when <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code>
  842. is used, by defining an <code class="docutils literal notranslate"><span class="pre">__all__</span></code> attribute containing a list of names that
  843. will be imported. One common complaint is that if the module imports other
  844. modules such as <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> or <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>, <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> will add
  845. them to the importing module’s namespace. To fix this, simply list the public
  846. names in <code class="docutils literal notranslate"><span class="pre">__all__</span></code>:</p>
  847. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># List public names</span>
  848. <span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Database&#39;</span><span class="p">,</span> <span class="s1">&#39;open&#39;</span><span class="p">]</span>
  849. </pre></div>
  850. </div>
  851. <p>A stricter version of this patch was first suggested and implemented by Ben
  852. Wolfson, but after some python-dev discussion, a weaker final version was
  853. checked in.</p>
  854. </li>
  855. <li><p>Applying <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> to strings previously used octal escapes for
  856. non-printable characters; for example, a newline was <code class="docutils literal notranslate"><span class="pre">'\012'</span></code>. This was a
  857. vestigial trace of Python’s C ancestry, but today octal is of very little
  858. practical use. Ka-Ping Yee suggested using hex escapes instead of octal ones,
  859. and using the <code class="docutils literal notranslate"><span class="pre">\n</span></code>, <code class="docutils literal notranslate"><span class="pre">\t</span></code>, <code class="docutils literal notranslate"><span class="pre">\r</span></code> escapes for the appropriate characters,
  860. and implemented this new formatting.</p></li>
  861. <li><p>Syntax errors detected at compile-time can now raise exceptions containing the
  862. filename and line number of the error, a pleasant side effect of the compiler
  863. reorganization done by Jeremy Hylton.</p></li>
  864. <li><p>C extensions which import other modules have been changed to use
  865. <a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a>, which means that they will use any import hooks
  866. that have been installed. This is also encouraged for third-party extensions
  867. that need to import some other module from C code.</p></li>
  868. <li><p>The size of the Unicode character database was shrunk by another 340K thanks
  869. to Fredrik Lundh.</p></li>
  870. <li><p>Some new ports were contributed: MacOS X (by Steven Majewski), Cygwin (by
  871. Jason Tishler); RISCOS (by Dietmar Schwertberger); Unixware 7 (by Billy G.
  872. Allie).</p></li>
  873. </ul>
  874. <p>And there’s the usual list of minor bugfixes, minor memory leaks, docstring
  875. edits, and other tweaks, too lengthy to be worth itemizing; see the CVS logs for
  876. the full details if you want them.</p>
  877. </section>
  878. <section id="acknowledgements">
  879. <h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Permalink to this headline">¶</a></h2>
  880. <p>The author would like to thank the following people for offering suggestions on
  881. various drafts of this article: Graeme Cross, David Goodger, Jay Graves, Michael
  882. Hudson, Marc-André Lemburg, Fredrik Lundh, Neil Schemenauer, Thomas Wouters.</p>
  883. </section>
  884. </section>
  885. <div class="clearer"></div>
  886. </div>
  887. </div>
  888. </div>
  889. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  890. <div class="sphinxsidebarwrapper">
  891. <div>
  892. <h3><a href="../contents.html">Table of Contents</a></h3>
  893. <ul>
  894. <li><a class="reference internal" href="#">What’s New in Python 2.1</a><ul>
  895. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  896. <li><a class="reference internal" href="#pep-227-nested-scopes">PEP 227: Nested Scopes</a></li>
  897. <li><a class="reference internal" href="#pep-236-future-directives">PEP 236: __future__ Directives</a></li>
  898. <li><a class="reference internal" href="#pep-207-rich-comparisons">PEP 207: Rich Comparisons</a></li>
  899. <li><a class="reference internal" href="#pep-230-warning-framework">PEP 230: Warning Framework</a></li>
  900. <li><a class="reference internal" href="#pep-229-new-build-system">PEP 229: New Build System</a></li>
  901. <li><a class="reference internal" href="#pep-205-weak-references">PEP 205: Weak References</a></li>
  902. <li><a class="reference internal" href="#pep-232-function-attributes">PEP 232: Function Attributes</a></li>
  903. <li><a class="reference internal" href="#pep-235-importing-modules-on-case-insensitive-platforms">PEP 235: Importing Modules on Case-Insensitive Platforms</a></li>
  904. <li><a class="reference internal" href="#pep-217-interactive-display-hook">PEP 217: Interactive Display Hook</a></li>
  905. <li><a class="reference internal" href="#pep-208-new-coercion-model">PEP 208: New Coercion Model</a></li>
  906. <li><a class="reference internal" href="#pep-241-metadata-in-python-packages">PEP 241: Metadata in Python Packages</a></li>
  907. <li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a></li>
  908. <li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
  909. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  910. </ul>
  911. </li>
  912. </ul>
  913. </div>
  914. <div>
  915. <h4>Previous topic</h4>
  916. <p class="topless"><a href="2.2.html"
  917. title="previous chapter">What’s New in Python 2.2</a></p>
  918. </div>
  919. <div>
  920. <h4>Next topic</h4>
  921. <p class="topless"><a href="2.0.html"
  922. title="next chapter">What’s New in Python 2.0</a></p>
  923. </div>
  924. <div role="note" aria-label="source link">
  925. <h3>This Page</h3>
  926. <ul class="this-page-menu">
  927. <li><a href="../bugs.html">Report a Bug</a></li>
  928. <li>
  929. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.1.rst"
  930. rel="nofollow">Show Source
  931. </a>
  932. </li>
  933. </ul>
  934. </div>
  935. </div>
  936. </div>
  937. <div class="clearer"></div>
  938. </div>
  939. <div class="related" role="navigation" aria-label="related navigation">
  940. <h3>Navigation</h3>
  941. <ul>
  942. <li class="right" style="margin-right: 10px">
  943. <a href="../genindex.html" title="General Index"
  944. >index</a></li>
  945. <li class="right" >
  946. <a href="../py-modindex.html" title="Python Module Index"
  947. >modules</a> |</li>
  948. <li class="right" >
  949. <a href="2.0.html" title="What’s New in Python 2.0"
  950. >next</a> |</li>
  951. <li class="right" >
  952. <a href="2.2.html" title="What’s New in Python 2.2"
  953. >previous</a> |</li>
  954. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  955. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  956. <li class="switchers">
  957. <div class="language_switcher_placeholder"></div>
  958. <div class="version_switcher_placeholder"></div>
  959. </li>
  960. <li>
  961. </li>
  962. <li id="cpython-language-and-version">
  963. <a href="../index.html">3.12.0 Documentation</a> &#187;
  964. </li>
  965. <li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> &#187;</li>
  966. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.1</a></li>
  967. <li class="right">
  968. <div class="inline-search" role="search">
  969. <form class="inline-search" action="../search.html" method="get">
  970. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  971. <input type="submit" value="Go" />
  972. </form>
  973. </div>
  974. |
  975. </li>
  976. <li class="right">
  977. <label class="theme-selector-label">
  978. Theme
  979. <select class="theme-selector" oninput="activateTheme(this.value)">
  980. <option value="auto" selected>Auto</option>
  981. <option value="light">Light</option>
  982. <option value="dark">Dark</option>
  983. </select>
  984. </label> |</li>
  985. </ul>
  986. </div>
  987. <div class="footer">
  988. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  989. <br />
  990. This page is licensed under the Python Software Foundation License Version 2.
  991. <br />
  992. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  993. <br />
  994. See <a href="/license.html">History and License</a> for more information.<br />
  995. <br />
  996. The Python Software Foundation is a non-profit corporation.
  997. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  998. <br />
  999. <br />
  1000. Last updated on Oct 02, 2023.
  1001. <a href="/bugs.html">Found a bug</a>?
  1002. <br />
  1003. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  1004. </div>
  1005. </body>
  1006. </html>