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

1457 line
142KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="What’s New in Python 2.2" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/whatsnew/2.2.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.2.2, released on October 14, 2002. Python 2.2.2 is a bugfix release of Python 2.2, originally released on De..." />
  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.2.2, released on October 14, 2002. Python 2.2.2 is a bugfix release of Python 2.2, originally released on De..." />
  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.2 &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="What’s New in Python 2.1" href="2.1.html" />
  33. <link rel="prev" title="What’s New in Python 2.3" href="2.3.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/whatsnew/2.2.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">What’s New in Python 2.2</a><ul>
  86. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  87. <li><a class="reference internal" href="#peps-252-and-253-type-and-class-changes">PEPs 252 and 253: Type and Class Changes</a><ul>
  88. <li><a class="reference internal" href="#old-and-new-classes">Old and New Classes</a></li>
  89. <li><a class="reference internal" href="#descriptors">Descriptors</a></li>
  90. <li><a class="reference internal" href="#multiple-inheritance-the-diamond-rule">Multiple Inheritance: The Diamond Rule</a></li>
  91. <li><a class="reference internal" href="#attribute-access">Attribute Access</a></li>
  92. <li><a class="reference internal" href="#related-links">Related Links</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#pep-234-iterators">PEP 234: Iterators</a></li>
  96. <li><a class="reference internal" href="#pep-255-simple-generators">PEP 255: Simple Generators</a></li>
  97. <li><a class="reference internal" href="#pep-237-unifying-long-integers-and-integers">PEP 237: Unifying Long Integers and Integers</a></li>
  98. <li><a class="reference internal" href="#pep-238-changing-the-division-operator">PEP 238: Changing the Division Operator</a></li>
  99. <li><a class="reference internal" href="#unicode-changes">Unicode Changes</a></li>
  100. <li><a class="reference internal" href="#pep-227-nested-scopes">PEP 227: Nested Scopes</a></li>
  101. <li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a></li>
  102. <li><a class="reference internal" href="#interpreter-changes-and-fixes">Interpreter Changes and Fixes</a></li>
  103. <li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
  104. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  105. </ul>
  106. </li>
  107. </ul>
  108. </div>
  109. <div>
  110. <h4>Previous topic</h4>
  111. <p class="topless"><a href="2.3.html"
  112. title="previous chapter">What’s New in Python 2.3</a></p>
  113. </div>
  114. <div>
  115. <h4>Next topic</h4>
  116. <p class="topless"><a href="2.1.html"
  117. title="next chapter">What’s New in Python 2.1</a></p>
  118. </div>
  119. <div role="note" aria-label="source link">
  120. <h3>This Page</h3>
  121. <ul class="this-page-menu">
  122. <li><a href="../bugs.html">Report a Bug</a></li>
  123. <li>
  124. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.2.rst"
  125. rel="nofollow">Show Source
  126. </a>
  127. </li>
  128. </ul>
  129. </div>
  130. </nav>
  131. </div>
  132. </div>
  133. <div class="related" role="navigation" aria-label="related navigation">
  134. <h3>Navigation</h3>
  135. <ul>
  136. <li class="right" style="margin-right: 10px">
  137. <a href="../genindex.html" title="General Index"
  138. accesskey="I">index</a></li>
  139. <li class="right" >
  140. <a href="../py-modindex.html" title="Python Module Index"
  141. >modules</a> |</li>
  142. <li class="right" >
  143. <a href="2.1.html" title="What’s New in Python 2.1"
  144. accesskey="N">next</a> |</li>
  145. <li class="right" >
  146. <a href="2.3.html" title="What’s New in Python 2.3"
  147. accesskey="P">previous</a> |</li>
  148. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  149. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  150. <li class="switchers">
  151. <div class="language_switcher_placeholder"></div>
  152. <div class="version_switcher_placeholder"></div>
  153. </li>
  154. <li>
  155. </li>
  156. <li id="cpython-language-and-version">
  157. <a href="../index.html">3.12.3 Documentation</a> &#187;
  158. </li>
  159. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> &#187;</li>
  160. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.2</a></li>
  161. <li class="right">
  162. <div class="inline-search" role="search">
  163. <form class="inline-search" action="../search.html" method="get">
  164. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  165. <input type="submit" value="Go" />
  166. </form>
  167. </div>
  168. |
  169. </li>
  170. <li class="right">
  171. <label class="theme-selector-label">
  172. Theme
  173. <select class="theme-selector" oninput="activateTheme(this.value)">
  174. <option value="auto" selected>Auto</option>
  175. <option value="light">Light</option>
  176. <option value="dark">Dark</option>
  177. </select>
  178. </label> |</li>
  179. </ul>
  180. </div>
  181. <div class="document">
  182. <div class="documentwrapper">
  183. <div class="bodywrapper">
  184. <div class="body" role="main">
  185. <section id="what-s-new-in-python-2-2">
  186. <h1>What’s New in Python 2.2<a class="headerlink" href="#what-s-new-in-python-2-2" title="Link to this heading">¶</a></h1>
  187. <dl class="field-list simple">
  188. <dt class="field-odd">Author<span class="colon">:</span></dt>
  189. <dd class="field-odd"><p>A.M. Kuchling</p>
  190. </dd>
  191. </dl>
  192. <section id="introduction">
  193. <h2>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h2>
  194. <p>This article explains the new features in Python 2.2.2, released on October 14,
  195. 2002. Python 2.2.2 is a bugfix release of Python 2.2, originally released on
  196. December 21, 2001.</p>
  197. <p>Python 2.2 can be thought of as the “cleanup release”. There are some features
  198. such as generators and iterators that are completely new, but most of the
  199. changes, significant and far-reaching though they may be, are aimed at cleaning
  200. up irregularities and dark corners of the language design.</p>
  201. <p>This article doesn’t attempt to provide a complete specification of the new
  202. features, but instead provides a convenient overview. For full details, you
  203. should refer to the documentation for Python 2.2, such as the <a class="reference external" href="https://docs.python.org/2.2/lib/lib.html">Python Library
  204. Reference</a> and the <a class="reference external" href="https://docs.python.org/2.2/ref/ref.html">Python
  205. Reference Manual</a>. If you want to
  206. understand the complete implementation and design rationale for a change, refer
  207. to the PEP for a particular new feature.</p>
  208. </section>
  209. <section id="peps-252-and-253-type-and-class-changes">
  210. <h2>PEPs 252 and 253: Type and Class Changes<a class="headerlink" href="#peps-252-and-253-type-and-class-changes" title="Link to this heading">¶</a></h2>
  211. <p>The largest and most far-reaching changes in Python 2.2 are to Python’s model of
  212. objects and classes. The changes should be backward compatible, so it’s likely
  213. that your code will continue to run unchanged, but the changes provide some
  214. amazing new capabilities. Before beginning this, the longest and most
  215. complicated section of this article, I’ll provide an overview of the changes and
  216. offer some comments.</p>
  217. <p>A long time ago I wrote a web page listing flaws in Python’s design. One of the
  218. most significant flaws was that it’s impossible to subclass Python types
  219. implemented in C. In particular, it’s not possible to subclass built-in types,
  220. so you can’t just subclass, say, lists in order to add a single useful method to
  221. them. The <code class="xref py py-mod docutils literal notranslate"><span class="pre">UserList</span></code> module provides a class that supports all of the
  222. methods of lists and that can be subclassed further, but there’s lots of C code
  223. that expects a regular Python list and won’t accept a <a class="reference internal" href="../library/collections.html#collections.UserList" title="collections.UserList"><code class="xref py py-class docutils literal notranslate"><span class="pre">UserList</span></code></a>
  224. instance.</p>
  225. <p>Python 2.2 fixes this, and in the process adds some exciting new capabilities.
  226. A brief summary:</p>
  227. <ul class="simple">
  228. <li><p>You can subclass built-in types such as lists and even integers, and your
  229. subclasses should work in every place that requires the original type.</p></li>
  230. <li><p>It’s now possible to define static and class methods, in addition to the
  231. instance methods available in previous versions of Python.</p></li>
  232. <li><p>It’s also possible to automatically call methods on accessing or setting an
  233. instance attribute by using a new mechanism called <em class="dfn">properties</em>. Many uses
  234. of <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> can be rewritten to use properties instead, making the
  235. resulting code simpler and faster. As a small side benefit, attributes can now
  236. have docstrings, too.</p></li>
  237. <li><p>The list of legal attributes for an instance can be limited to a particular
  238. set using <em class="dfn">slots</em>, making it possible to safeguard against typos and
  239. perhaps make more optimizations possible in future versions of Python.</p></li>
  240. </ul>
  241. <p>Some users have voiced concern about all these changes. Sure, they say, the new
  242. features are neat and lend themselves to all sorts of tricks that weren’t
  243. possible in previous versions of Python, but they also make the language more
  244. complicated. Some people have said that they’ve always recommended Python for
  245. its simplicity, and feel that its simplicity is being lost.</p>
  246. <p>Personally, I think there’s no need to worry. Many of the new features are
  247. quite esoteric, and you can write a lot of Python code without ever needed to be
  248. aware of them. Writing a simple class is no more difficult than it ever was, so
  249. you don’t need to bother learning or teaching them unless they’re actually
  250. needed. Some very complicated tasks that were previously only possible from C
  251. will now be possible in pure Python, and to my mind that’s all for the better.</p>
  252. <p>I’m not going to attempt to cover every single corner case and small change that
  253. were required to make the new features work. Instead this section will paint
  254. only the broad strokes. See section <a class="reference internal" href="#sect-rellinks"><span class="std std-ref">Related Links</span></a>, “Related Links”, for
  255. further sources of information about Python 2.2’s new object model.</p>
  256. <section id="old-and-new-classes">
  257. <h3>Old and New Classes<a class="headerlink" href="#old-and-new-classes" title="Link to this heading">¶</a></h3>
  258. <p>First, you should know that Python 2.2 really has two kinds of classes: classic
  259. or old-style classes, and new-style classes. The old-style class model is
  260. exactly the same as the class model in earlier versions of Python. All the new
  261. features described in this section apply only to new-style classes. This
  262. divergence isn’t intended to last forever; eventually old-style classes will be
  263. dropped, possibly in Python 3.0.</p>
  264. <p>So how do you define a new-style class? You do it by subclassing an existing
  265. new-style class. Most of Python’s built-in types, such as integers, lists,
  266. dictionaries, and even files, are new-style classes now. A new-style class
  267. named <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>, the base class for all built-in types, has also been
  268. added so if no built-in type is suitable, you can just subclass
  269. <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>:</p>
  270. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  271. <span class="k">def</span> <span class="fm">__init__</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  272. <span class="o">...</span>
  273. <span class="o">...</span>
  274. </pre></div>
  275. </div>
  276. <p>This means that <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> statements that don’t have any base classes are
  277. always classic classes in Python 2.2. (Actually you can also change this by
  278. setting a module-level variable named <code class="xref py py-attr docutils literal notranslate"><span class="pre">__metaclass__</span></code> — see <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0253/"><strong>PEP 253</strong></a>
  279. for the details — but it’s easier to just subclass <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>.)</p>
  280. <p>The type objects for the built-in types are available as built-ins, named using
  281. a clever trick. Python has always had built-in functions named <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a>,
  282. <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>. In 2.2, they aren’t functions any more, but
  283. type objects that behave as factories when called.</p>
  284. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span>
  285. <span class="go">&lt;type &#39;int&#39;&gt;</span>
  286. <span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="s1">&#39;123&#39;</span><span class="p">)</span>
  287. <span class="go">123</span>
  288. </pre></div>
  289. </div>
  290. <p>To make the set of types complete, new type objects such as <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-func docutils literal notranslate"><span class="pre">dict()</span></code></a> and
  291. <code class="xref py py-func docutils literal notranslate"><span class="pre">file()</span></code> have been added. Here’s a more interesting example, adding a
  292. <code class="xref py py-meth docutils literal notranslate"><span class="pre">lock()</span></code> method to file objects:</p>
  293. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">LockableFile</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
  294. <span class="k">def</span> <span class="nf">lock</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">length</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">whence</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  295. <span class="kn">import</span> <span class="nn">fcntl</span>
  296. <span class="k">return</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">lockf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">operation</span><span class="p">,</span>
  297. <span class="n">length</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">whence</span><span class="p">)</span>
  298. </pre></div>
  299. </div>
  300. <p>The now-obsolete <code class="xref py py-mod docutils literal notranslate"><span class="pre">posixfile</span></code> module contained a class that emulated all of
  301. a file object’s methods and also added a <code class="xref py py-meth docutils literal notranslate"><span class="pre">lock()</span></code> method, but this class
  302. couldn’t be passed to internal functions that expected a built-in file,
  303. something which is possible with our new <code class="xref py py-class docutils literal notranslate"><span class="pre">LockableFile</span></code>.</p>
  304. </section>
  305. <section id="descriptors">
  306. <h3>Descriptors<a class="headerlink" href="#descriptors" title="Link to this heading">¶</a></h3>
  307. <p>In previous versions of Python, there was no consistent way to discover what
  308. attributes and methods were supported by an object. There were some informal
  309. conventions, such as defining <code class="xref py py-attr docutils literal notranslate"><span class="pre">__members__</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__methods__</span></code>
  310. attributes that were lists of names, but often the author of an extension type
  311. or a class wouldn’t bother to define them. You could fall back on inspecting
  312. 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> of an object, but when class inheritance or an arbitrary
  313. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code> hook were in use this could still be inaccurate.</p>
  314. <p>The one big idea underlying the new class model is that an API for describing
  315. the attributes of an object using <em class="dfn">descriptors</em> has been formalized.
  316. Descriptors specify the value of an attribute, stating whether it’s a method or
  317. a field. With the descriptor API, static methods and class methods become
  318. possible, as well as more exotic constructs.</p>
  319. <p>Attribute descriptors are objects that live inside class objects, and have a few
  320. attributes of their own:</p>
  321. <ul class="simple">
  322. <li><p><a class="reference internal" href="../library/stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> is the attribute’s name.</p></li>
  323. <li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code> is the attribute’s docstring.</p></li>
  324. <li><p><code class="docutils literal notranslate"><span class="pre">__get__(object)</span></code> is a method that retrieves the attribute value from
  325. <em>object</em>.</p></li>
  326. <li><p><code class="docutils literal notranslate"><span class="pre">__set__(object,</span> <span class="pre">value)</span></code> sets the attribute on <em>object</em> to <em>value</em>.</p></li>
  327. <li><p><code class="docutils literal notranslate"><span class="pre">__delete__(object,</span> <span class="pre">value)</span></code> deletes the <em>value</em> attribute of <em>object</em>.</p></li>
  328. </ul>
  329. <p>For example, when you write <code class="docutils literal notranslate"><span class="pre">obj.x</span></code>, the steps that Python actually performs
  330. are:</p>
  331. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">descriptor</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">x</span>
  332. <span class="n">descriptor</span><span class="o">.</span><span class="fm">__get__</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
  333. </pre></div>
  334. </div>
  335. <p>For methods, <code class="xref py py-meth docutils literal notranslate"><span class="pre">descriptor.__get__()</span></code> returns a temporary object that’s
  336. callable, and wraps up the instance and the method to be called on it. This is
  337. also why static methods and class methods are now possible; they have
  338. descriptors that wrap up just the method, or the method and the class. As a
  339. brief explanation of these new kinds of methods, static methods aren’t passed
  340. the instance, and therefore resemble regular functions. Class methods are
  341. passed the class of the object, but not the object itself. Static and class
  342. methods are defined like this:</p>
  343. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  344. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span>
  345. <span class="o">...</span>
  346. <span class="n">f</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  347. <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span>
  348. <span class="o">...</span>
  349. <span class="n">g</span> <span class="o">=</span> <span class="nb">classmethod</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
  350. </pre></div>
  351. </div>
  352. <p>The <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> function takes the function <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code>, and returns it
  353. wrapped up in a descriptor so it can be stored in the class object. You might
  354. expect there to be special syntax for creating such methods (<code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">static</span> <span class="pre">f</span></code>,
  355. <code class="docutils literal notranslate"><span class="pre">defstatic</span> <span class="pre">f()</span></code>, or something like that) but no such syntax has been defined
  356. yet; that’s been left for future versions of Python.</p>
  357. <p>More new features, such as slots and properties, are also implemented as new
  358. kinds of descriptors, and it’s not difficult to write a descriptor class that
  359. does something novel. For example, it would be possible to write a descriptor
  360. class that made it possible to write Eiffel-style preconditions and
  361. postconditions for a method. A class that used this feature might be defined
  362. like this:</p>
  363. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">eiffel</span> <span class="kn">import</span> <span class="n">eiffelmethod</span>
  364. <span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  365. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span>
  366. <span class="c1"># The actual function</span>
  367. <span class="o">...</span>
  368. <span class="k">def</span> <span class="nf">pre_f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  369. <span class="c1"># Check preconditions</span>
  370. <span class="o">...</span>
  371. <span class="k">def</span> <span class="nf">post_f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  372. <span class="c1"># Check postconditions</span>
  373. <span class="o">...</span>
  374. <span class="n">f</span> <span class="o">=</span> <span class="n">eiffelmethod</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">pre_f</span><span class="p">,</span> <span class="n">post_f</span><span class="p">)</span>
  375. </pre></div>
  376. </div>
  377. <p>Note that a person using the new <code class="xref py py-func docutils literal notranslate"><span class="pre">eiffelmethod()</span></code> doesn’t have to understand
  378. anything about descriptors. This is why I think the new features don’t increase
  379. the basic complexity of the language. There will be a few wizards who need to
  380. know about it in order to write <code class="xref py py-func docutils literal notranslate"><span class="pre">eiffelmethod()</span></code> or the ZODB or whatever,
  381. but most users will just write code on top of the resulting libraries and ignore
  382. the implementation details.</p>
  383. </section>
  384. <section id="multiple-inheritance-the-diamond-rule">
  385. <h3>Multiple Inheritance: The Diamond Rule<a class="headerlink" href="#multiple-inheritance-the-diamond-rule" title="Link to this heading">¶</a></h3>
  386. <p>Multiple inheritance has also been made more useful through changing the rules
  387. under which names are resolved. Consider this set of classes (diagram taken
  388. from <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0253/"><strong>PEP 253</strong></a> by Guido van Rossum):</p>
  389. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
  390. <span class="o">^</span> <span class="o">^</span> <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span>
  391. <span class="o">/</span> \
  392. <span class="o">/</span> \
  393. <span class="o">/</span> \
  394. <span class="o">/</span> \
  395. <span class="k">class</span> <span class="nc">B</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  396. <span class="o">^</span> <span class="o">^</span> <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span>
  397. \ <span class="o">/</span>
  398. \ <span class="o">/</span>
  399. \ <span class="o">/</span>
  400. \ <span class="o">/</span>
  401. <span class="k">class</span> <span class="nc">D</span>
  402. </pre></div>
  403. </div>
  404. <p>The lookup rule for classic classes is simple but not very smart; the base
  405. classes are searched depth-first, going from left to right. A reference to
  406. <code class="xref py py-meth docutils literal notranslate"><span class="pre">D.save()</span></code> will search the classes <code class="xref py py-class docutils literal notranslate"><span class="pre">D</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">B</span></code>, and then
  407. <code class="xref py py-class docutils literal notranslate"><span class="pre">A</span></code>, where <code class="xref py py-meth docutils literal notranslate"><span class="pre">save()</span></code> would be found and returned. <code class="xref py py-meth docutils literal notranslate"><span class="pre">C.save()</span></code>
  408. would never be found at all. This is bad, because if <code class="xref py py-class docutils literal notranslate"><span class="pre">C</span></code>’s <code class="xref py py-meth docutils literal notranslate"><span class="pre">save()</span></code>
  409. method is saving some internal state specific to <code class="xref py py-class docutils literal notranslate"><span class="pre">C</span></code>, not calling it will
  410. result in that state never getting saved.</p>
  411. <p>New-style classes follow a different algorithm that’s a bit more complicated to
  412. explain, but does the right thing in this situation. (Note that Python 2.3
  413. changes this algorithm to one that produces the same results in most cases, but
  414. produces more useful results for really complicated inheritance graphs.)</p>
  415. <ol class="arabic simple">
  416. <li><p>List all the base classes, following the classic lookup rule and include a
  417. class multiple times if it’s visited repeatedly. In the above example, the list
  418. of visited classes is [<code class="xref py py-class docutils literal notranslate"><span class="pre">D</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">B</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">A</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">C</span></code>,
  419. <code class="xref py py-class docutils literal notranslate"><span class="pre">A</span></code>].</p></li>
  420. <li><p>Scan the list for duplicated classes. If any are found, remove all but one
  421. occurrence, leaving the <em>last</em> one in the list. In the above example, the list
  422. becomes [<code class="xref py py-class docutils literal notranslate"><span class="pre">D</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">B</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">C</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">A</span></code>] after dropping
  423. duplicates.</p></li>
  424. </ol>
  425. <p>Following this rule, referring to <code class="xref py py-meth docutils literal notranslate"><span class="pre">D.save()</span></code> will return <code class="xref py py-meth docutils literal notranslate"><span class="pre">C.save()</span></code>,
  426. which is the behaviour we’re after. This lookup rule is the same as the one
  427. followed by Common Lisp. A new built-in function, <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a>, provides a way
  428. to get at a class’s superclasses without having to reimplement Python’s
  429. algorithm. The most commonly used form will be <code class="docutils literal notranslate"><span class="pre">super(class,</span> <span class="pre">obj)</span></code>, which
  430. returns a bound superclass object (not the actual class object). This form
  431. will be used in methods to call a method in the superclass; for example,
  432. <code class="xref py py-class docutils literal notranslate"><span class="pre">D</span></code>’s <code class="xref py py-meth docutils literal notranslate"><span class="pre">save()</span></code> method would look like this:</p>
  433. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">D</span> <span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="n">C</span><span class="p">):</span>
  434. <span class="k">def</span> <span class="nf">save</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  435. <span class="c1"># Call superclass .save()</span>
  436. <span class="nb">super</span><span class="p">(</span><span class="n">D</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
  437. <span class="c1"># Save D&#39;s private information here</span>
  438. <span class="o">...</span>
  439. </pre></div>
  440. </div>
  441. <p><a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> can also return unbound superclass objects when called as
  442. <code class="docutils literal notranslate"><span class="pre">super(class)</span></code> or <code class="docutils literal notranslate"><span class="pre">super(class1,</span> <span class="pre">class2)</span></code>, but this probably won’t
  443. often be useful.</p>
  444. </section>
  445. <section id="attribute-access">
  446. <h3>Attribute Access<a class="headerlink" href="#attribute-access" title="Link to this heading">¶</a></h3>
  447. <p>A fair number of sophisticated Python classes define hooks for attribute access
  448. using <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a>; most commonly this is done for convenience, to make
  449. code more readable by automatically mapping an attribute access such as
  450. <code class="docutils literal notranslate"><span class="pre">obj.parent</span></code> into a method call such as <code class="docutils literal notranslate"><span class="pre">obj.get_parent</span></code>. Python 2.2 adds
  451. some new ways of controlling attribute access.</p>
  452. <p>First, <code class="docutils literal notranslate"><span class="pre">__getattr__(attr_name)</span></code> is still supported by new-style classes,
  453. and nothing about it has changed. As before, it will be called when an attempt
  454. is made to access <code class="docutils literal notranslate"><span class="pre">obj.foo</span></code> and no attribute named <code class="docutils literal notranslate"><span class="pre">foo</span></code> is found in the
  455. instance’s dictionary.</p>
  456. <p>New-style classes also support a new method,
  457. <code class="docutils literal notranslate"><span class="pre">__getattribute__(attr_name)</span></code>. The difference between the two methods is
  458. that <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> is <em>always</em> called whenever any attribute is
  459. accessed, while the old <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> is only called if <code class="docutils literal notranslate"><span class="pre">foo</span></code> isn’t
  460. found in the instance’s dictionary.</p>
  461. <p>However, Python 2.2’s support for <em class="dfn">properties</em> will often be a simpler way
  462. to trap attribute references. Writing a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code> method is
  463. complicated because to avoid recursion you can’t use regular attribute accesses
  464. inside them, and instead have to mess around with the contents of
  465. <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>. <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> methods also end up being called by Python
  466. when it checks for other methods such as <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">__coerce__()</span></code>,
  467. and so have to be written with this in mind. Finally, calling a function on
  468. every attribute access results in a sizable performance loss.</p>
  469. <p><a class="reference internal" href="../library/functions.html#property" title="property"><code class="xref py py-class docutils literal notranslate"><span class="pre">property</span></code></a> is a new built-in type that packages up three functions that
  470. get, set, or delete an attribute, and a docstring. For example, if you want to
  471. define a <code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code> attribute that’s computed, but also settable, you could
  472. write:</p>
  473. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  474. <span class="k">def</span> <span class="nf">get_size</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  475. <span class="n">result</span> <span class="o">=</span> <span class="o">...</span> <span class="n">computation</span> <span class="o">...</span>
  476. <span class="k">return</span> <span class="n">result</span>
  477. <span class="k">def</span> <span class="nf">set_size</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
  478. <span class="o">...</span> <span class="n">compute</span> <span class="n">something</span> <span class="n">based</span> <span class="n">on</span> <span class="n">the</span> <span class="n">size</span>
  479. <span class="ow">and</span> <span class="nb">set</span> <span class="n">internal</span> <span class="n">state</span> <span class="n">appropriately</span> <span class="o">...</span>
  480. <span class="c1"># Define a property. The &#39;delete this attribute&#39;</span>
  481. <span class="c1"># method is defined as None, so the attribute</span>
  482. <span class="c1"># can&#39;t be deleted.</span>
  483. <span class="n">size</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">get_size</span><span class="p">,</span> <span class="n">set_size</span><span class="p">,</span>
  484. <span class="kc">None</span><span class="p">,</span>
  485. <span class="s2">&quot;Storage size of this instance&quot;</span><span class="p">)</span>
  486. </pre></div>
  487. </div>
  488. <p>That is certainly clearer and easier to write than a pair of
  489. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code>/<code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code> methods that check for the <code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code>
  490. attribute and handle it specially while retrieving all other attributes from the
  491. instance’s <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>. Accesses to <code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code> are also the only ones
  492. which have to perform the work of calling a function, so references to other
  493. attributes run at their usual speed.</p>
  494. <p>Finally, it’s possible to constrain the list of attributes that can be
  495. referenced on an object using the new <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code></a> class attribute. Python
  496. objects are usually very dynamic; at any time it’s possible to define a new
  497. attribute on an instance by just doing <code class="docutils literal notranslate"><span class="pre">obj.new_attr=1</span></code>. A new-style class
  498. can define a class attribute named <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code></a> to limit the legal
  499. attributes to a particular set of names. An example will make this clear:</p>
  500. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  501. <span class="gp">... </span> <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;template&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">)</span>
  502. <span class="gp">...</span>
  503. <span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
  504. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">template</span>
  505. <span class="go">None</span>
  506. <span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span><span class="o">.</span><span class="n">template</span> <span class="o">=</span> <span class="s1">&#39;Test&#39;</span>
  507. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">template</span>
  508. <span class="go">Test</span>
  509. <span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span><span class="o">.</span><span class="n">newattr</span> <span class="o">=</span> <span class="kc">None</span>
  510. <span class="gt">Traceback (most recent call last):</span>
  511. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
  512. <span class="gr">AttributeError</span>: <span class="n">&#39;C&#39; object has no attribute &#39;newattr&#39;</span>
  513. </pre></div>
  514. </div>
  515. <p>Note how you get an <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> on the attempt to assign to an
  516. attribute not listed in <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code></a>.</p>
  517. </section>
  518. <section id="related-links">
  519. <span id="sect-rellinks"></span><h3>Related Links<a class="headerlink" href="#related-links" title="Link to this heading">¶</a></h3>
  520. <p>This section has just been a quick overview of the new features, giving enough
  521. of an explanation to start you programming, but many details have been
  522. simplified or ignored. Where should you go to get a more complete picture?</p>
  523. <p>The <a class="reference internal" href="../howto/descriptor.html#descriptorhowto"><span class="std std-ref">Descriptor Guide</span></a> is a lengthy tutorial introduction to
  524. the descriptor features, written by Guido van Rossum. If my description has
  525. whetted your appetite, go read this tutorial next, because it goes into much
  526. more detail about the new features while still remaining quite easy to read.</p>
  527. <p>Next, there are two relevant PEPs, <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0252/"><strong>PEP 252</strong></a> and <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0253/"><strong>PEP 253</strong></a>. <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0252/"><strong>PEP 252</strong></a> is
  528. titled “Making Types Look More Like Classes”, and covers the descriptor API.
  529. <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0253/"><strong>PEP 253</strong></a> is titled “Subtyping Built-in Types”, and describes the changes to
  530. type objects that make it possible to subtype built-in objects. <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0253/"><strong>PEP 253</strong></a> is
  531. the more complicated PEP of the two, and at a few points the necessary
  532. explanations of types and meta-types may cause your head to explode. Both PEPs
  533. were written and implemented by Guido van Rossum, with substantial assistance
  534. from the rest of the Zope Corp. team.</p>
  535. <p>Finally, there’s the ultimate authority: the source code. Most of the machinery
  536. for the type handling is in <code class="file docutils literal notranslate"><span class="pre">Objects/typeobject.c</span></code>, but you should only
  537. resort to it after all other avenues have been exhausted, including posting a
  538. question to python-list or python-dev.</p>
  539. </section>
  540. </section>
  541. <section id="pep-234-iterators">
  542. <h2>PEP 234: Iterators<a class="headerlink" href="#pep-234-iterators" title="Link to this heading">¶</a></h2>
  543. <p>Another significant addition to 2.2 is an iteration interface at both the C and
  544. Python levels. Objects can define how they can be looped over by callers.</p>
  545. <p>In Python versions up to 2.1, the usual way to make <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">obj</span></code> work is
  546. to define a <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> method that looks something like this:</p>
  547. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
  548. <span class="k">return</span> <span class="o">&lt;</span><span class="nb">next</span> <span class="n">item</span><span class="o">&gt;</span>
  549. </pre></div>
  550. </div>
  551. <p><a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> is more properly used to define an indexing operation on an
  552. object so that you can write <code class="docutils literal notranslate"><span class="pre">obj[5]</span></code> to retrieve the sixth element. It’s a
  553. bit misleading when you’re using this only to support <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> loops.
  554. Consider some file-like object that wants to be looped over; the <em>index</em>
  555. parameter is essentially meaningless, as the class probably assumes that a
  556. series of <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> calls will be made with <em>index</em> incrementing by
  557. one each time. In other words, the presence of the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> method
  558. doesn’t mean that using <code class="docutils literal notranslate"><span class="pre">file[5]</span></code> to randomly access the sixth element will
  559. work, though it really should.</p>
  560. <p>In Python 2.2, iteration can be implemented separately, and <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a>
  561. methods can be limited to classes that really do support random access. The
  562. basic idea of iterators is simple. A new built-in function, <code class="docutils literal notranslate"><span class="pre">iter(obj)</span></code>
  563. or <code class="docutils literal notranslate"><span class="pre">iter(C,</span> <span class="pre">sentinel)</span></code>, is used to get an iterator. <code class="docutils literal notranslate"><span class="pre">iter(obj)</span></code> returns
  564. an iterator for the object <em>obj</em>, while <code class="docutils literal notranslate"><span class="pre">iter(C,</span> <span class="pre">sentinel)</span></code> returns an
  565. iterator that will invoke the callable object <em>C</em> until it returns <em>sentinel</em> to
  566. signal that the iterator is done.</p>
  567. <p>Python classes can define an <code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code> method, which should create and
  568. return a new iterator for the object; if the object is its own iterator, this
  569. method can just return <code class="docutils literal notranslate"><span class="pre">self</span></code>. In particular, iterators will usually be their
  570. own iterators. Extension types implemented in C can implement a <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_iter" title="PyTypeObject.tp_iter"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_iter</span></code></a>
  571. function in order to return an iterator, and extension types that want to behave
  572. as iterators can define a <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_iternext" title="PyTypeObject.tp_iternext"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_iternext</span></code></a> function.</p>
  573. <p>So, after all this, what do iterators actually do? They have one required
  574. method, <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a>, which takes no arguments and returns the next value. When
  575. there are no more values to be returned, calling <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> should raise the
  576. <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> exception.</p>
  577. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
  578. <span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">L</span><span class="p">)</span>
  579. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">i</span>
  580. <span class="go">&lt;iterator object at 0x8116870&gt;</span>
  581. <span class="gp">&gt;&gt;&gt; </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  582. <span class="go">1</span>
  583. <span class="gp">&gt;&gt;&gt; </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  584. <span class="go">2</span>
  585. <span class="gp">&gt;&gt;&gt; </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  586. <span class="go">3</span>
  587. <span class="gp">&gt;&gt;&gt; </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  588. <span class="gt">Traceback (most recent call last):</span>
  589. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
  590. <span class="gr">StopIteration</span>
  591. <span class="gp">&gt;&gt;&gt;</span>
  592. </pre></div>
  593. </div>
  594. <p>In 2.2, Python’s <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> statement no longer expects a sequence; it
  595. expects something for which <a class="reference internal" href="../library/functions.html#iter" title="iter"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter()</span></code></a> will return an iterator. For backward
  596. compatibility and convenience, an iterator is automatically constructed for
  597. sequences that don’t implement <code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code> or a <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_iter" title="PyTypeObject.tp_iter"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_iter</span></code></a> slot, so
  598. <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">[1,2,3]</span></code> will still work. Wherever the Python interpreter loops
  599. over a sequence, it’s been changed to use the iterator protocol. This means you
  600. can do things like this:</p>
  601. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
  602. <span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">L</span><span class="p">)</span>
  603. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">i</span>
  604. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span>
  605. <span class="go">(1, 2, 3)</span>
  606. </pre></div>
  607. </div>
  608. <p>Iterator support has been added to some of Python’s basic types. Calling
  609. <a class="reference internal" href="../library/functions.html#iter" title="iter"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter()</span></code></a> on a dictionary will return an iterator which loops over its keys:</p>
  610. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Jan&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;Feb&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;Mar&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;Apr&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="s1">&#39;May&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="s1">&#39;Jun&#39;</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
  611. <span class="gp">... </span> <span class="s1">&#39;Jul&#39;</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span> <span class="s1">&#39;Aug&#39;</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span> <span class="s1">&#39;Sep&#39;</span><span class="p">:</span> <span class="mi">9</span><span class="p">,</span> <span class="s1">&#39;Oct&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span> <span class="s1">&#39;Nov&#39;</span><span class="p">:</span> <span class="mi">11</span><span class="p">,</span> <span class="s1">&#39;Dec&#39;</span><span class="p">:</span> <span class="mi">12</span><span class="p">}</span>
  612. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">m</span><span class="p">:</span> <span class="nb">print</span> <span class="n">key</span><span class="p">,</span> <span class="n">m</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  613. <span class="gp">...</span>
  614. <span class="go">Mar 3</span>
  615. <span class="go">Feb 2</span>
  616. <span class="go">Aug 8</span>
  617. <span class="go">Sep 9</span>
  618. <span class="go">May 5</span>
  619. <span class="go">Jun 6</span>
  620. <span class="go">Jul 7</span>
  621. <span class="go">Jan 1</span>
  622. <span class="go">Apr 4</span>
  623. <span class="go">Nov 11</span>
  624. <span class="go">Dec 12</span>
  625. <span class="go">Oct 10</span>
  626. </pre></div>
  627. </div>
  628. <p>That’s just the default behaviour. If you want to iterate over keys, values, or
  629. key/value pairs, you can explicitly call the <code class="xref py py-meth docutils literal notranslate"><span class="pre">iterkeys()</span></code>,
  630. <code class="xref py py-meth docutils literal notranslate"><span class="pre">itervalues()</span></code>, or <code class="xref py py-meth docutils literal notranslate"><span class="pre">iteritems()</span></code> methods to get an appropriate iterator.
  631. In a minor related change, the <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> operator now works on dictionaries,
  632. so <code class="docutils literal notranslate"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">dict</span></code> is now equivalent to <code class="docutils literal notranslate"><span class="pre">dict.has_key(key)</span></code>.</p>
  633. <p>Files also provide an iterator, which calls 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 until
  634. there are no more lines in the file. This means you can now read each line of a
  635. file using code like this:</p>
  636. <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">file</span><span class="p">:</span>
  637. <span class="c1"># do something for each line</span>
  638. <span class="o">...</span>
  639. </pre></div>
  640. </div>
  641. <p>Note that you can only go forward in an iterator; there’s no way to get the
  642. previous element, reset the iterator, or make a copy of it. An iterator object
  643. could provide such additional capabilities, but the iterator protocol only
  644. requires a <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> method.</p>
  645. <div class="admonition seealso">
  646. <p class="admonition-title">See also</p>
  647. <dl class="simple">
  648. <dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0234/"><strong>PEP 234</strong></a> - Iterators</dt><dd><p>Written by Ka-Ping Yee and GvR; implemented by the Python Labs crew, mostly by
  649. GvR and Tim Peters.</p>
  650. </dd>
  651. </dl>
  652. </div>
  653. </section>
  654. <section id="pep-255-simple-generators">
  655. <h2>PEP 255: Simple Generators<a class="headerlink" href="#pep-255-simple-generators" title="Link to this heading">¶</a></h2>
  656. <p>Generators are another new feature, one that interacts with the introduction of
  657. iterators.</p>
  658. <p>You’re doubtless familiar with how function calls work in Python or C. When you
  659. call a function, it gets a private namespace where its local variables are
  660. created. When the function reaches a <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> statement, the local
  661. variables are destroyed and the resulting value is returned to the caller. A
  662. later call to the same function will get a fresh new set of local variables.
  663. But, what if the local variables weren’t thrown away on exiting a function?
  664. What if you could later resume the function where it left off? This is what
  665. generators provide; they can be thought of as resumable functions.</p>
  666. <p>Here’s the simplest example of a generator function:</p>
  667. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">generate_ints</span><span class="p">(</span><span class="n">N</span><span class="p">):</span>
  668. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">):</span>
  669. <span class="k">yield</span> <span class="n">i</span>
  670. </pre></div>
  671. </div>
  672. <p>A new keyword, <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a>, was introduced for generators. Any function
  673. containing a <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> statement is a generator function; this is
  674. detected by Python’s bytecode compiler which compiles the function specially as
  675. a result. Because a new keyword was introduced, generators must be explicitly
  676. enabled in a module by including a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">generators</span></code>
  677. statement near the top of the module’s source code. In Python 2.3 this
  678. statement will become unnecessary.</p>
  679. <p>When you call a generator function, it doesn’t return a single value; instead it
  680. returns a generator object that supports the iterator protocol. On executing
  681. the <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> statement, the generator outputs the value of <code class="docutils literal notranslate"><span class="pre">i</span></code>,
  682. similar to a <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> statement. The big difference between
  683. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> and a <code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code> statement is that on reaching a
  684. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> the generator’s state of execution is suspended and local
  685. variables are preserved. On the next call to the generator’s <code class="docutils literal notranslate"><span class="pre">next()</span></code> method,
  686. the function will resume executing immediately after the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code>
  687. statement. (For complicated reasons, the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> statement isn’t
  688. allowed inside the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code> block of a
  689. <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#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> statement; read <span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0255/"><strong>PEP 255</strong></a> for a full
  690. explanation of the interaction between <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> and exceptions.)</p>
  691. <p>Here’s a sample usage of the <code class="xref py py-func docutils literal notranslate"><span class="pre">generate_ints()</span></code> generator:</p>
  692. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gen</span> <span class="o">=</span> <span class="n">generate_ints</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
  693. <span class="gp">&gt;&gt;&gt; </span><span class="n">gen</span>
  694. <span class="go">&lt;generator object at 0x8117f90&gt;</span>
  695. <span class="gp">&gt;&gt;&gt; </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  696. <span class="go">0</span>
  697. <span class="gp">&gt;&gt;&gt; </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  698. <span class="go">1</span>
  699. <span class="gp">&gt;&gt;&gt; </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  700. <span class="go">2</span>
  701. <span class="gp">&gt;&gt;&gt; </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  702. <span class="gt">Traceback (most recent call last):</span>
  703. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
  704. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">2</span>, in <span class="n">generate_ints</span>
  705. <span class="gr">StopIteration</span>
  706. </pre></div>
  707. </div>
  708. <p>You could equally write <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">generate_ints(5)</span></code>, or <code class="docutils literal notranslate"><span class="pre">a,b,c</span> <span class="pre">=</span>
  709. <span class="pre">generate_ints(3)</span></code>.</p>
  710. <p>Inside a generator function, the <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> statement can only be used
  711. without a value, and signals the end of the procession of values; afterwards the
  712. generator cannot return any further values. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code> with a value, such
  713. as <code class="docutils literal notranslate"><span class="pre">return</span> <span class="pre">5</span></code>, is a syntax error inside a generator function. The end of the
  714. generator’s results can also be indicated by raising <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a>
  715. manually, or by just letting the flow of execution fall off the bottom of the
  716. function.</p>
  717. <p>You could achieve the effect of generators manually by writing your own class
  718. and storing all the local variables of the generator as instance variables. For
  719. example, returning a list of integers could be done by setting <code class="docutils literal notranslate"><span class="pre">self.count</span></code> to
  720. 0, and having the <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> method increment <code class="docutils literal notranslate"><span class="pre">self.count</span></code> and return it.
  721. However, for a moderately complicated generator, writing a corresponding class
  722. would be much messier. <code class="file docutils literal notranslate"><span class="pre">Lib/test/test_generators.py</span></code> contains a number of
  723. more interesting examples. The simplest one implements an in-order traversal of
  724. a tree using generators recursively.</p>
  725. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># A recursive generator that generates Tree leaves in in-order.</span>
  726. <span class="k">def</span> <span class="nf">inorder</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
  727. <span class="k">if</span> <span class="n">t</span><span class="p">:</span>
  728. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inorder</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">left</span><span class="p">):</span>
  729. <span class="k">yield</span> <span class="n">x</span>
  730. <span class="k">yield</span> <span class="n">t</span><span class="o">.</span><span class="n">label</span>
  731. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inorder</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">right</span><span class="p">):</span>
  732. <span class="k">yield</span> <span class="n">x</span>
  733. </pre></div>
  734. </div>
  735. <p>Two other examples in <code class="file docutils literal notranslate"><span class="pre">Lib/test/test_generators.py</span></code> produce solutions for
  736. the N-Queens problem (placing $N$ queens on an $NxN$ chess board so that no
  737. queen threatens another) and the Knight’s Tour (a route that takes a knight to
  738. every square of an $NxN$ chessboard without visiting any square twice).</p>
  739. <p>The idea of generators comes from other programming languages, especially Icon
  740. (<a class="reference external" href="https://www2.cs.arizona.edu/icon/">https://www2.cs.arizona.edu/icon/</a>), where the idea of generators is central. In
  741. Icon, every expression and function call behaves like a generator. One example
  742. from “An Overview of the Icon Programming Language” at
  743. <a class="reference external" href="https://www2.cs.arizona.edu/icon/docs/ipd266.htm">https://www2.cs.arizona.edu/icon/docs/ipd266.htm</a> gives an idea of what this looks
  744. like:</p>
  745. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sentence</span> <span class="o">:=</span> <span class="s2">&quot;Store it in the neighboring harbor&quot;</span>
  746. <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">:=</span> <span class="n">find</span><span class="p">(</span><span class="s2">&quot;or&quot;</span><span class="p">,</span> <span class="n">sentence</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">5</span> <span class="n">then</span> <span class="n">write</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
  747. </pre></div>
  748. </div>
  749. <p>In Icon the <code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code> function returns the indexes at which the substring
  750. “or” is found: 3, 23, 33. In the <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a> statement, <code class="docutils literal notranslate"><span class="pre">i</span></code> is first
  751. assigned a value of 3, but 3 is less than 5, so the comparison fails, and Icon
  752. retries it with the second value of 23. 23 is greater than 5, so the comparison
  753. now succeeds, and the code prints the value 23 to the screen.</p>
  754. <p>Python doesn’t go nearly as far as Icon in adopting generators as a central
  755. concept. Generators are considered a new part of the core Python language, but
  756. learning or using them isn’t compulsory; if they don’t solve any problems that
  757. you have, feel free to ignore them. One novel feature of Python’s interface as
  758. compared to Icon’s is that a generator’s state is represented as a concrete
  759. object (the iterator) that can be passed around to other functions or stored in
  760. a data structure.</p>
  761. <div class="admonition seealso">
  762. <p class="admonition-title">See also</p>
  763. <dl class="simple">
  764. <dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0255/"><strong>PEP 255</strong></a> - Simple Generators</dt><dd><p>Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland. Implemented mostly
  765. by Neil Schemenauer and Tim Peters, with other fixes from the Python Labs crew.</p>
  766. </dd>
  767. </dl>
  768. </div>
  769. </section>
  770. <section id="pep-237-unifying-long-integers-and-integers">
  771. <h2>PEP 237: Unifying Long Integers and Integers<a class="headerlink" href="#pep-237-unifying-long-integers-and-integers" title="Link to this heading">¶</a></h2>
  772. <p>In recent versions, the distinction between regular integers, which are 32-bit
  773. values on most machines, and long integers, which can be of arbitrary size, was
  774. becoming an annoyance. For example, on platforms that support files larger than
  775. <code class="docutils literal notranslate"><span class="pre">2**32</span></code> bytes, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code> method of file objects has to return a long
  776. integer. However, there were various bits of Python that expected plain integers
  777. and would raise an error if a long integer was provided instead. For example,
  778. in Python 1.5, only regular integers could be used as a slice index, and
  779. <code class="docutils literal notranslate"><span class="pre">'abc'[1L:]</span></code> would raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception with the message ‘slice
  780. index must be int’.</p>
  781. <p>Python 2.2 will shift values from short to long integers as required. The ‘L’
  782. suffix is no longer needed to indicate a long integer literal, as now the
  783. compiler will choose the appropriate type. (Using the ‘L’ suffix will be
  784. discouraged in future 2.x versions of Python, triggering a warning in Python
  785. 2.4, and probably dropped in Python 3.0.) Many operations that used to raise an
  786. <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> will now return a long integer as their result. For
  787. example:</p>
  788. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">1234567890123</span>
  789. <span class="go">1234567890123L</span>
  790. <span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span> <span class="o">**</span> <span class="mi">64</span>
  791. <span class="go">18446744073709551616L</span>
  792. </pre></div>
  793. </div>
  794. <p>In most cases, integers and long integers will now be treated identically. You
  795. can still distinguish them with the <a class="reference internal" href="../library/functions.html#type" title="type"><code class="xref py py-func docutils literal notranslate"><span class="pre">type()</span></code></a> built-in function, but that’s
  796. rarely needed.</p>
  797. <div class="admonition seealso">
  798. <p class="admonition-title">See also</p>
  799. <dl class="simple">
  800. <dt><span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0237/"><strong>PEP 237</strong></a> - Unifying Long Integers and Integers</dt><dd><p>Written by Moshe Zadka and Guido van Rossum. Implemented mostly by Guido van
  801. Rossum.</p>
  802. </dd>
  803. </dl>
  804. </div>
  805. </section>
  806. <section id="pep-238-changing-the-division-operator">
  807. <h2>PEP 238: Changing the Division Operator<a class="headerlink" href="#pep-238-changing-the-division-operator" title="Link to this heading">¶</a></h2>
  808. <p>The most controversial change in Python 2.2 heralds the start of an effort to
  809. fix an old design flaw that’s been in Python from the beginning. Currently
  810. Python’s division operator, <code class="docutils literal notranslate"><span class="pre">/</span></code>, behaves like C’s division operator when
  811. presented with two integer arguments: it returns an integer result that’s
  812. truncated down when there would be a fractional part. For example, <code class="docutils literal notranslate"><span class="pre">3/2</span></code> is
  813. 1, not 1.5, and <code class="docutils literal notranslate"><span class="pre">(-1)/2</span></code> is -1, not -0.5. This means that the results of
  814. division can vary unexpectedly depending on the type of the two operands and
  815. because Python is dynamically typed, it can be difficult to determine the
  816. possible types of the operands.</p>
  817. <p>(The controversy is over whether this is <em>really</em> a design flaw, and whether
  818. it’s worth breaking existing code to fix this. It’s caused endless discussions
  819. on python-dev, and in July 2001 erupted into a storm of acidly sarcastic
  820. postings on <em class="newsgroup">comp.lang.python</em>. I won’t argue for either side here
  821. and will stick to describing what’s implemented in 2.2. Read <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0238/"><strong>PEP 238</strong></a> for a
  822. summary of arguments and counter-arguments.)</p>
  823. <p>Because this change might break code, it’s being introduced very gradually.
  824. Python 2.2 begins the transition, but the switch won’t be complete until Python
  825. 3.0.</p>
  826. <p>First, I’ll borrow some terminology from <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0238/"><strong>PEP 238</strong></a>. “True division” is the
  827. division that most non-programmers are familiar with: 3/2 is 1.5, 1/4 is 0.25,
  828. and so forth. “Floor division” is what Python’s <code class="docutils literal notranslate"><span class="pre">/</span></code> operator currently does
  829. when given integer operands; the result is the floor of the value returned by
  830. true division. “Classic division” is the current mixed behaviour of <code class="docutils literal notranslate"><span class="pre">/</span></code>; it
  831. returns the result of floor division when the operands are integers, and returns
  832. the result of true division when one of the operands is a floating-point number.</p>
  833. <p>Here are the changes 2.2 introduces:</p>
  834. <ul>
  835. <li><p>A new operator, <code class="docutils literal notranslate"><span class="pre">//</span></code>, is the floor division operator. (Yes, we know it looks
  836. like C++’s comment symbol.) <code class="docutils literal notranslate"><span class="pre">//</span></code> <em>always</em> performs floor division no matter
  837. what the types of its operands are, so <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">//</span> <span class="pre">2</span></code> is 0 and <code class="docutils literal notranslate"><span class="pre">1.0</span> <span class="pre">//</span> <span class="pre">2.0</span></code> is
  838. also 0.0.</p>
  839. <p><code class="docutils literal notranslate"><span class="pre">//</span></code> is always available in Python 2.2; you don’t need to enable it using a
  840. <code class="docutils literal notranslate"><span class="pre">__future__</span></code> statement.</p>
  841. </li>
  842. <li><p>By including a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></code> in a module, the <code class="docutils literal notranslate"><span class="pre">/</span></code>
  843. operator will be changed to return the result of true division, so <code class="docutils literal notranslate"><span class="pre">1/2</span></code> is
  844. 0.5. Without the <code class="docutils literal notranslate"><span class="pre">__future__</span></code> statement, <code class="docutils literal notranslate"><span class="pre">/</span></code> still means classic division.
  845. The default meaning of <code class="docutils literal notranslate"><span class="pre">/</span></code> will not change until Python 3.0.</p></li>
  846. <li><p>Classes can define methods called <a class="reference internal" href="../reference/datamodel.html#object.__truediv__" title="object.__truediv__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__truediv__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__floordiv__" title="object.__floordiv__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__floordiv__()</span></code></a>
  847. to overload the two division operators. At the C level, there are also slots in
  848. the <a class="reference internal" href="../c-api/typeobj.html#c.PyNumberMethods" title="PyNumberMethods"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyNumberMethods</span></code></a> structure so extension types can define the two
  849. operators.</p></li>
  850. <li><p>Python 2.2 supports some command-line arguments for testing whether code will
  851. work with the changed division semantics. Running python with <code class="xref std std-option docutils literal notranslate"><span class="pre">-Q</span>
  852. <span class="pre">warn</span></code> will cause a warning to be issued whenever division is applied to two
  853. integers. You can use this to find code that’s affected by the change and fix
  854. it. By default, Python 2.2 will simply perform classic division without a
  855. warning; the warning will be turned on by default in Python 2.3.</p></li>
  856. </ul>
  857. <div class="admonition seealso">
  858. <p class="admonition-title">See also</p>
  859. <dl class="simple">
  860. <dt><span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-0238/"><strong>PEP 238</strong></a> - Changing the Division Operator</dt><dd><p>Written by Moshe Zadka and Guido van Rossum. Implemented by Guido van Rossum..</p>
  861. </dd>
  862. </dl>
  863. </div>
  864. </section>
  865. <section id="unicode-changes">
  866. <h2>Unicode Changes<a class="headerlink" href="#unicode-changes" title="Link to this heading">¶</a></h2>
  867. <p>Python’s Unicode support has been enhanced a bit in 2.2. Unicode strings are
  868. usually stored as UCS-2, as 16-bit unsigned integers. Python 2.2 can also be
  869. compiled to use UCS-4, 32-bit unsigned integers, as its internal encoding by
  870. supplying <code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-unicode=ucs4</span></code> to the configure script. (It’s also
  871. possible to specify <code class="xref std std-option docutils literal notranslate"><span class="pre">--disable-unicode</span></code> to completely disable Unicode
  872. support.)</p>
  873. <p>When built to use UCS-4 (a “wide Python”), the interpreter can natively handle
  874. Unicode characters from U+000000 to U+110000, so the range of legal values for
  875. the <code class="xref py py-func docutils literal notranslate"><span class="pre">unichr()</span></code> function is expanded accordingly. Using an interpreter
  876. compiled to use UCS-2 (a “narrow Python”), values greater than 65535 will still
  877. cause <code class="xref py py-func docutils literal notranslate"><span class="pre">unichr()</span></code> to raise a <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> exception. This is all
  878. described in <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0261/"><strong>PEP 261</strong></a>, “Support for ‘wide’ Unicode characters”; consult it for
  879. further details.</p>
  880. <p>Another change is simpler to explain. Since their introduction, Unicode strings
  881. have supported an <code class="xref py py-meth docutils literal notranslate"><span class="pre">encode()</span></code> method to convert the string to a selected
  882. encoding such as UTF-8 or Latin-1. A symmetric <code class="docutils literal notranslate"><span class="pre">decode([*encoding*])</span></code>
  883. method has been added to 8-bit strings (though not to Unicode strings) in 2.2.
  884. <code class="xref py py-meth docutils literal notranslate"><span class="pre">decode()</span></code> assumes that the string is in the specified encoding and decodes
  885. it, returning whatever is returned by the codec.</p>
  886. <p>Using this new feature, codecs have been added for tasks not directly related to
  887. Unicode. For example, codecs have been added for uu-encoding, MIME’s base64
  888. encoding, and compression with the <a class="reference internal" href="../library/zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> module:</p>
  889. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;Here is a lengthy piece of redundant, overly verbose,</span>
  890. <span class="gp">... </span><span class="s2">and repetitive text.</span>
  891. <span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span>
  892. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;zlib&#39;</span><span class="p">)</span>
  893. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
  894. <span class="go">&#39;x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...&#39;</span>
  895. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;zlib&#39;</span><span class="p">)</span>
  896. <span class="go">&#39;Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n&#39;</span>
  897. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">s</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;uu&#39;</span><span class="p">)</span>
  898. <span class="go">begin 666 &lt;data&gt;</span>
  899. <span class="go">M2&amp;5R92!I&lt;R!A(&amp;QE;F=T:&#39;D@&lt;&amp;EE8V4@;V8@&lt;F5D=6YD86YT+&quot;!O=F5R;&#39;D@</span>
  900. <span class="go">&gt;=F5R8F]S92P*86YD(&#39;)E&lt;&amp;5T:71I=F4@=&amp;5X=&quot;X*</span>
  901. <span class="go">end</span>
  902. <span class="gp">&gt;&gt;&gt; </span><span class="s2">&quot;sheesh&quot;</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;rot-13&#39;</span><span class="p">)</span>
  903. <span class="go">&#39;furrfu&#39;</span>
  904. </pre></div>
  905. </div>
  906. <p>To convert a class instance to Unicode, a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__unicode__()</span></code> method can be
  907. defined by a class, analogous to <code class="xref py py-meth docutils literal notranslate"><span class="pre">__str__()</span></code>.</p>
  908. <p><code class="xref py py-meth docutils literal notranslate"><span class="pre">encode()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">decode()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__unicode__()</span></code> were implemented by
  909. Marc-André Lemburg. The changes to support using UCS-4 internally were
  910. implemented by Fredrik Lundh and Martin von Löwis.</p>
  911. <div class="admonition seealso">
  912. <p class="admonition-title">See also</p>
  913. <dl class="simple">
  914. <dt><span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0261/"><strong>PEP 261</strong></a> - Support for ‘wide’ Unicode characters</dt><dd><p>Written by Paul Prescod.</p>
  915. </dd>
  916. </dl>
  917. </div>
  918. </section>
  919. <section id="pep-227-nested-scopes">
  920. <h2>PEP 227: Nested Scopes<a class="headerlink" href="#pep-227-nested-scopes" title="Link to this heading">¶</a></h2>
  921. <p>In Python 2.1, statically nested scopes were added as an optional feature, to be
  922. enabled by a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">nested_scopes</span></code> directive. In 2.2 nested
  923. scopes no longer need to be specially enabled, and are now always present. The
  924. rest of this section is a copy of the description of nested scopes from my
  925. “What’s New in Python 2.1” document; if you read it when 2.1 came out, you can
  926. skip the rest of this section.</p>
  927. <p>The largest change introduced in Python 2.1, and made complete in 2.2, is to
  928. Python’s scoping rules. In Python 2.0, at any given time there are at most
  929. three namespaces used to look up variable names: local, module-level, and the
  930. built-in namespace. This often surprised people because it didn’t match their
  931. intuitive expectations. For example, a nested recursive function definition
  932. doesn’t work:</p>
  933. <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>
  934. <span class="o">...</span>
  935. <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
  936. <span class="o">...</span>
  937. <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>
  938. <span class="o">...</span>
  939. </pre></div>
  940. </div>
  941. <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
  942. 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
  943. module-level namespace. This isn’t much of a problem in practice (how often do
  944. you recursively define interior functions like this?), but this also made using
  945. 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.
  946. In code which uses <code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code> you can often find local variables being
  947. copied by passing them as the default values of arguments.</p>
  948. <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>
  949. <span class="s2">&quot;Return list of any entries equal to &#39;name&#39;&quot;</span>
  950. <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>
  951. <span class="bp">self</span><span class="o">.</span><span class="n">list_attribute</span><span class="p">)</span>
  952. <span class="k">return</span> <span class="n">L</span>
  953. </pre></div>
  954. </div>
  955. <p>The readability of Python code written in a strongly functional style suffers
  956. greatly as a result.</p>
  957. <p>The most significant change to Python 2.2 is that static scoping has been added
  958. to the language to fix this problem. As a first effect, the <code class="docutils literal notranslate"><span class="pre">name=name</span></code>
  959. default argument is now unnecessary in the above example. Put simply, when a
  960. given variable name is not assigned a value within a function (by an assignment,
  961. 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),
  962. references to the variable will be looked up in the local namespace of the
  963. enclosing scope. A more detailed explanation of the rules, and a dissection of
  964. the implementation, can be found in the PEP.</p>
  965. <p>This change may cause some compatibility problems for code where the same
  966. variable name is used both at the module level and as a local variable within a
  967. function that contains further function definitions. This seems rather unlikely
  968. though, since such code would have been pretty confusing to read in the first
  969. place.</p>
  970. <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
  971. <code class="docutils literal notranslate"><span class="pre">exec</span></code> statements have been made illegal inside a function scope under
  972. certain conditions. The Python reference manual has said all along that <code class="docutils literal notranslate"><span class="pre">from</span>
  973. <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
  974. interpreter has never enforced this before. As part of the implementation of
  975. nested scopes, the compiler which turns Python source into bytecodes has to
  976. generate different code to access variables in a containing scope. <code class="docutils literal notranslate"><span class="pre">from</span>
  977. <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
  978. figure this out, because they add names to the local namespace that are
  979. unknowable at compile time. Therefore, if a function contains function
  980. 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
  981. 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>
  982. <p>To make the preceding explanation a bit clearer, here’s an example:</p>
  983. <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>
  984. <span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
  985. <span class="c1"># The next line is a syntax error</span>
  986. <span class="n">exec</span> <span class="s1">&#39;x=2&#39;</span>
  987. <span class="k">def</span> <span class="nf">g</span><span class="p">():</span>
  988. <span class="k">return</span> <span class="n">x</span>
  989. </pre></div>
  990. </div>
  991. <p>Line 4 containing the <code class="docutils literal notranslate"><span class="pre">exec</span></code> statement is a syntax error, since
  992. <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
  993. be accessed by <code class="xref py py-func docutils literal notranslate"><span class="pre">g()</span></code>.</p>
  994. <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
  995. most Python code (and when it is used, it’s often a sign of a poor design
  996. anyway).</p>
  997. <div class="admonition seealso">
  998. <p class="admonition-title">See also</p>
  999. <dl class="simple">
  1000. <dt><span class="target" id="index-16"></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>
  1001. </dd>
  1002. </dl>
  1003. </div>
  1004. </section>
  1005. <section id="new-and-improved-modules">
  1006. <h2>New and Improved Modules<a class="headerlink" href="#new-and-improved-modules" title="Link to this heading">¶</a></h2>
  1007. <ul>
  1008. <li><p>The <a class="reference internal" href="../library/xmlrpc.client.html#module-xmlrpc.client" title="xmlrpc.client: XML-RPC client access."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code></a> module was contributed to the standard library by Fredrik
  1009. Lundh, providing support for writing XML-RPC clients. XML-RPC is a simple
  1010. remote procedure call protocol built on top of HTTP and XML. For example, the
  1011. following snippet retrieves a list of RSS channels from the O’Reilly Network,
  1012. and then lists the recent headlines for one channel:</p>
  1013. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xmlrpclib</span>
  1014. <span class="n">s</span> <span class="o">=</span> <span class="n">xmlrpclib</span><span class="o">.</span><span class="n">Server</span><span class="p">(</span>
  1015. <span class="s1">&#39;http://www.oreillynet.com/meerkat/xml-rpc/server.php&#39;</span><span class="p">)</span>
  1016. <span class="n">channels</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">meerkat</span><span class="o">.</span><span class="n">getChannels</span><span class="p">()</span>
  1017. <span class="c1"># channels is a list of dictionaries, like this:</span>
  1018. <span class="c1"># [{&#39;id&#39;: 4, &#39;title&#39;: &#39;Freshmeat Daily News&#39;}</span>
  1019. <span class="c1"># {&#39;id&#39;: 190, &#39;title&#39;: &#39;32Bits Online&#39;},</span>
  1020. <span class="c1"># {&#39;id&#39;: 4549, &#39;title&#39;: &#39;3DGamers&#39;}, ... ]</span>
  1021. <span class="c1"># Get the items for one channel</span>
  1022. <span class="n">items</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">meerkat</span><span class="o">.</span><span class="n">getItems</span><span class="p">(</span> <span class="p">{</span><span class="s1">&#39;channel&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">}</span> <span class="p">)</span>
  1023. <span class="c1"># &#39;items&#39; is another list of dictionaries, like this:</span>
  1024. <span class="c1"># [{&#39;link&#39;: &#39;http://freshmeat.net/releases/52719/&#39;,</span>
  1025. <span class="c1"># &#39;description&#39;: &#39;A utility which converts HTML to XSL FO.&#39;,</span>
  1026. <span class="c1"># &#39;title&#39;: &#39;html2fo 0.3 (Default)&#39;}, ... ]</span>
  1027. </pre></div>
  1028. </div>
  1029. <p>The <a class="reference internal" href="../library/xmlrpc.server.html#module-xmlrpc.server" title="xmlrpc.server: Basic XML-RPC server implementations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleXMLRPCServer</span></code></a> module makes it easy to create straightforward
  1030. XML-RPC servers. See <a class="reference external" href="http://xmlrpc.scripting.com/">http://xmlrpc.scripting.com/</a> for more information about XML-RPC.</p>
  1031. </li>
  1032. <li><p>The new <a class="reference internal" href="../library/hmac.html#module-hmac" title="hmac: Keyed-Hashing for Message Authentication (HMAC) implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code></a> module implements the HMAC algorithm described by
  1033. <span class="target" id="index-17"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2104.html"><strong>RFC 2104</strong></a>. (Contributed by Gerhard Häring.)</p></li>
  1034. <li><p>Several functions that originally returned lengthy tuples now return
  1035. pseudo-sequences that still behave like tuples but also have mnemonic attributes such
  1036. as <code class="xref py py-attr docutils literal notranslate"><span class="pre">memberst_mtime</span></code> or <a class="reference internal" href="../library/time.html#time.struct_time.tm_year" title="time.struct_time.tm_year"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_year</span></code></a>. The enhanced functions include
  1037. <a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a>, <a class="reference internal" href="../library/os.html#os.fstat" title="os.fstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">fstat()</span></code></a>, <a class="reference internal" href="../library/os.html#os.statvfs" title="os.statvfs"><code class="xref py py-func docutils literal notranslate"><span class="pre">statvfs()</span></code></a>, and <a class="reference internal" href="../library/os.html#os.fstatvfs" title="os.fstatvfs"><code class="xref py py-func docutils literal notranslate"><span class="pre">fstatvfs()</span></code></a> in the
  1038. <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module, and <a class="reference internal" href="../library/time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a>, <a class="reference internal" href="../library/time.html#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a>, and <a class="reference internal" href="../library/time.html#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a> in
  1039. 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.</p>
  1040. <p>For example, to obtain a file’s size using the old tuples, you’d end up writing
  1041. something like <code class="docutils literal notranslate"><span class="pre">file_size</span> <span class="pre">=</span> <span class="pre">os.stat(filename)[stat.ST_SIZE]</span></code>, but now this can
  1042. be written more clearly as <code class="docutils literal notranslate"><span class="pre">file_size</span> <span class="pre">=</span> <span class="pre">os.stat(filename).st_size</span></code>.</p>
  1043. <p>The original patch for this feature was contributed by Nick Mathewson.</p>
  1044. </li>
  1045. <li><p>The Python profiler has been extensively reworked and various errors in its
  1046. output have been corrected. (Contributed by Fred L. Drake, Jr. and Tim Peters.)</p></li>
  1047. <li><p>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 can be compiled to support IPv6; specify the
  1048. <code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-ipv6</span></code> option to Python’s configure script. (Contributed by
  1049. Jun-ichiro “itojun” Hagino.)</p></li>
  1050. <li><p>Two new format characters were added to the <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> module for 64-bit
  1051. integers on platforms that support the C <span class="c-expr sig sig-inline c"><span class="kt">long</span><span class="w"> </span><span class="kt">long</span></span> type. <code class="docutils literal notranslate"><span class="pre">q</span></code> is for
  1052. a signed 64-bit integer, and <code class="docutils literal notranslate"><span class="pre">Q</span></code> is for an unsigned one. The value is
  1053. returned in Python’s long integer type. (Contributed by Tim Peters.)</p></li>
  1054. <li><p>In the interpreter’s interactive mode, there’s a new built-in function
  1055. <a class="reference internal" href="../library/functions.html#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a> that uses the <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> module introduced in Python 2.1 to
  1056. provide interactive help. <code class="docutils literal notranslate"><span class="pre">help(object)</span></code> displays any available help text
  1057. about <em>object</em>. <a class="reference internal" href="../library/functions.html#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a> with no argument puts you in an online help
  1058. utility, where you can enter the names of functions, classes, or modules to read
  1059. their help text. (Contributed by Guido van Rossum, using Ka-Ping Yee’s
  1060. <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> module.)</p></li>
  1061. <li><p>Various bugfixes and performance improvements have been made to the SRE engine
  1062. underlying 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. For example, the <a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.sub()</span></code></a> and
  1063. <a class="reference internal" href="../library/re.html#re.split" title="re.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.split()</span></code></a> functions have been rewritten in C. Another contributed patch
  1064. speeds up certain Unicode character ranges by a factor of two, and a new
  1065. <a class="reference internal" href="../library/re.html#re.finditer" title="re.finditer"><code class="xref py py-meth docutils literal notranslate"><span class="pre">finditer()</span></code></a> method that returns an iterator over all the non-overlapping
  1066. matches in a given string. (SRE is maintained by Fredrik Lundh. The
  1067. BIGCHARSET patch was contributed by Martin von Löwis.)</p></li>
  1068. <li><p>The <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> module now supports <span class="target" id="index-18"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2487.html"><strong>RFC 2487</strong></a>, “Secure SMTP over TLS”, so
  1069. it’s now possible to encrypt the SMTP traffic between a Python program and the
  1070. mail transport agent being handed a message. <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> also supports SMTP
  1071. authentication. (Contributed by Gerhard Häring.)</p></li>
  1072. <li><p>The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> module, maintained by Piers Lauder, has support for several
  1073. new extensions: the NAMESPACE extension defined in <span class="target" id="index-19"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2342.html"><strong>RFC 2342</strong></a>, SORT, GETACL and
  1074. SETACL. (Contributed by Anthony Baxter and Michel Pelletier.)</p></li>
  1075. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">rfc822</span></code> module’s parsing of email addresses is now compliant with
  1076. <span class="target" id="index-20"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>, an update to <span class="target" id="index-21"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc822.html"><strong>RFC 822</strong></a>. (The module’s name is <em>not</em> going to be
  1077. changed to <code class="docutils literal notranslate"><span class="pre">rfc2822</span></code>.) A new package, <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a>, has also been added for
  1078. parsing and generating e-mail messages. (Contributed by Barry Warsaw, and
  1079. arising out of his work on Mailman.)</p></li>
  1080. <li><p>The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code></a> module now contains a new <code class="xref py py-class docutils literal notranslate"><span class="pre">Differ</span></code> class for
  1081. producing human-readable lists of changes (a “delta”) between two sequences of
  1082. lines of text. There are also two generator functions, <code class="xref py py-func docutils literal notranslate"><span class="pre">ndiff()</span></code> and
  1083. <code class="xref py py-func docutils literal notranslate"><span class="pre">restore()</span></code>, which respectively return a delta from two sequences, or one of
  1084. the original sequences from a delta. (Grunt work contributed by David Goodger,
  1085. from ndiff.py code by Tim Peters who then did the generatorization.)</p></li>
  1086. <li><p>New constants <code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_letters</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_lowercase</span></code>, and
  1087. <code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_uppercase</span></code> were added to the <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module. There were
  1088. several modules in the standard library that used <code class="xref py py-const docutils literal notranslate"><span class="pre">string.letters</span></code> to
  1089. mean the ranges A-Za-z, but that assumption is incorrect when locales are in
  1090. use, because <code class="xref py py-const docutils literal notranslate"><span class="pre">string.letters</span></code> varies depending on the set of legal
  1091. characters defined by the current locale. The buggy modules have all been fixed
  1092. to use <code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_letters</span></code> instead. (Reported by an unknown person; fixed by
  1093. Fred L. Drake, Jr.)</p></li>
  1094. <li><p>The <a class="reference internal" href="../library/mimetypes.html#module-mimetypes" title="mimetypes: Mapping of filename extensions to MIME types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mimetypes</span></code></a> module now makes it easier to use alternative MIME-type
  1095. databases by the addition of a <a class="reference internal" href="../library/mimetypes.html#mimetypes.MimeTypes" title="mimetypes.MimeTypes"><code class="xref py py-class docutils literal notranslate"><span class="pre">MimeTypes</span></code></a> class, which takes a list of
  1096. filenames to be parsed. (Contributed by Fred L. Drake, Jr.)</p></li>
  1097. <li><p>A <a class="reference internal" href="../library/threading.html#threading.Timer" title="threading.Timer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Timer</span></code></a> class was added to the <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module that allows
  1098. scheduling an activity to happen at some future time. (Contributed by Itamar
  1099. Shtull-Trauring.)</p></li>
  1100. </ul>
  1101. </section>
  1102. <section id="interpreter-changes-and-fixes">
  1103. <h2>Interpreter Changes and Fixes<a class="headerlink" href="#interpreter-changes-and-fixes" title="Link to this heading">¶</a></h2>
  1104. <p>Some of the changes only affect people who deal with the Python interpreter at
  1105. the C level because they’re writing Python extension modules, embedding the
  1106. interpreter, or just hacking on the interpreter itself. If you only write Python
  1107. code, none of the changes described here will affect you very much.</p>
  1108. <ul>
  1109. <li><p>Profiling and tracing functions can now be implemented in C, which can operate
  1110. at much higher speeds than Python-based functions and should reduce the overhead
  1111. of profiling and tracing. This will be of interest to authors of development
  1112. environments for Python. Two new C functions were added to Python’s API,
  1113. <a class="reference internal" href="../c-api/init.html#c.PyEval_SetProfile" title="PyEval_SetProfile"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetProfile()</span></code></a> and <a class="reference internal" href="../c-api/init.html#c.PyEval_SetTrace" title="PyEval_SetTrace"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetTrace()</span></code></a>. The existing
  1114. <a class="reference internal" href="../library/sys.html#sys.setprofile" title="sys.setprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setprofile()</span></code></a> and <a class="reference internal" href="../library/sys.html#sys.settrace" title="sys.settrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.settrace()</span></code></a> functions still exist, and have
  1115. simply been changed to use the new C-level interface. (Contributed by Fred L.
  1116. Drake, Jr.)</p></li>
  1117. <li><p>Another low-level API, primarily of interest to implementors of Python
  1118. debuggers and development tools, was added. <a class="reference internal" href="../c-api/init.html#c.PyInterpreterState_Head" title="PyInterpreterState_Head"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyInterpreterState_Head()</span></code></a> and
  1119. <a class="reference internal" href="../c-api/init.html#c.PyInterpreterState_Next" title="PyInterpreterState_Next"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyInterpreterState_Next()</span></code></a> let a caller walk through all the existing
  1120. interpreter objects; <a class="reference internal" href="../c-api/init.html#c.PyInterpreterState_ThreadHead" title="PyInterpreterState_ThreadHead"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyInterpreterState_ThreadHead()</span></code></a> and
  1121. <a class="reference internal" href="../c-api/init.html#c.PyThreadState_Next" title="PyThreadState_Next"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_Next()</span></code></a> allow looping over all the thread states for a given
  1122. interpreter. (Contributed by David Beazley.)</p></li>
  1123. <li><p>The C-level interface to the garbage collector has been changed to make it
  1124. easier to write extension types that support garbage collection and to debug
  1125. misuses of the functions. Various functions have slightly different semantics,
  1126. so a bunch of functions had to be renamed. Extensions that use the old API will
  1127. still compile but will <em>not</em> participate in garbage collection, so updating them
  1128. for 2.2 should be considered fairly high priority.</p>
  1129. <p>To upgrade an extension module to the new API, perform the following steps:</p>
  1130. </li>
  1131. <li><p>Rename <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_GC</span></code> to <a class="reference internal" href="../c-api/typeobj.html#c.Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a>.</p></li>
  1132. <li><dl class="simple">
  1133. <dt>Use <a class="reference internal" href="../c-api/gcsupport.html#c.PyObject_GC_New" title="PyObject_GC_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_New()</span></code></a> or <a class="reference internal" href="../c-api/gcsupport.html#c.PyObject_GC_NewVar" title="PyObject_GC_NewVar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_NewVar()</span></code></a> to allocate</dt><dd><p>objects, and <a class="reference internal" href="../c-api/gcsupport.html#c.PyObject_GC_Del" title="PyObject_GC_Del"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_Del()</span></code></a> to deallocate them.</p>
  1134. </dd>
  1135. </dl>
  1136. </li>
  1137. <li><p>Rename <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_Init()</span></code> to <a class="reference internal" href="../c-api/gcsupport.html#c.PyObject_GC_Track" title="PyObject_GC_Track"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_Track()</span></code></a> and
  1138. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_Fini()</span></code> to <a class="reference internal" href="../c-api/gcsupport.html#c.PyObject_GC_UnTrack" title="PyObject_GC_UnTrack"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_UnTrack()</span></code></a>.</p></li>
  1139. <li><p>Remove <code class="xref c c-macro docutils literal notranslate"><span class="pre">PyGC_HEAD_SIZE</span></code> from object size calculations.</p></li>
  1140. <li><p>Remove calls to <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_AS_GC()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_FROM_GC()</span></code>.</p></li>
  1141. <li><p>A new <code class="docutils literal notranslate"><span class="pre">et</span></code> format sequence was added to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>; <code class="docutils literal notranslate"><span class="pre">et</span></code>
  1142. takes both a parameter and an encoding name, and converts the parameter to the
  1143. given encoding if the parameter turns out to be a Unicode string, or leaves it
  1144. alone if it’s an 8-bit string, assuming it to already be in the desired
  1145. encoding. This differs from the <code class="docutils literal notranslate"><span class="pre">es</span></code> format character, which assumes that
  1146. 8-bit strings are in Python’s default ASCII encoding and converts them to the
  1147. specified new encoding. (Contributed by M.-A. Lemburg, and used for the MBCS
  1148. support on Windows described in the following section.)</p></li>
  1149. <li><p>A different argument parsing function, <a class="reference internal" href="../c-api/arg.html#c.PyArg_UnpackTuple" title="PyArg_UnpackTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_UnpackTuple()</span></code></a>, has been
  1150. added that’s simpler and presumably faster. Instead of specifying a format
  1151. string, the caller simply gives the minimum and maximum number of arguments
  1152. expected, and a set of pointers to <span class="c-expr sig sig-inline c"><a class="reference internal" href="../c-api/structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> variables that will be
  1153. filled in with argument values.</p></li>
  1154. <li><p>Two new flags <a class="reference internal" href="../c-api/structures.html#c.METH_NOARGS" title="METH_NOARGS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_NOARGS</span></code></a> and <a class="reference internal" href="../c-api/structures.html#c.METH_O" title="METH_O"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_O</span></code></a> are available in method
  1155. definition tables to simplify implementation of methods with no arguments or a
  1156. single untyped argument. Calling such methods is more efficient than calling a
  1157. corresponding method that uses <a class="reference internal" href="../c-api/structures.html#c.METH_VARARGS" title="METH_VARARGS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_VARARGS</span></code></a>. Also, the old
  1158. <code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_OLDARGS</span></code> style of writing C methods is now officially deprecated.</p></li>
  1159. <li><p>Two new wrapper functions, <a class="reference internal" href="../c-api/conversion.html#c.PyOS_snprintf" title="PyOS_snprintf"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_snprintf()</span></code></a> and <a class="reference internal" href="../c-api/conversion.html#c.PyOS_vsnprintf" title="PyOS_vsnprintf"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_vsnprintf()</span></code></a>
  1160. were added to provide cross-platform implementations for the relatively new
  1161. <code class="xref c c-func docutils literal notranslate"><span class="pre">snprintf()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">vsnprintf()</span></code> C lib APIs. In contrast to the standard
  1162. <code class="xref c c-func docutils literal notranslate"><span class="pre">sprintf()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">vsprintf()</span></code> functions, the Python versions check the
  1163. bounds of the buffer used to protect against buffer overruns. (Contributed by
  1164. M.-A. Lemburg.)</p></li>
  1165. <li><p>The <a class="reference internal" href="../c-api/tuple.html#c._PyTuple_Resize" title="_PyTuple_Resize"><code class="xref c c-func docutils literal notranslate"><span class="pre">_PyTuple_Resize()</span></code></a> function has lost an unused parameter, so now it
  1166. takes 2 parameters instead of 3. The third argument was never used, and can
  1167. simply be discarded when porting code from earlier versions to Python 2.2.</p></li>
  1168. </ul>
  1169. </section>
  1170. <section id="other-changes-and-fixes">
  1171. <h2>Other Changes and Fixes<a class="headerlink" href="#other-changes-and-fixes" title="Link to this heading">¶</a></h2>
  1172. <p>As usual there were a bunch of other improvements and bugfixes scattered
  1173. throughout the source tree. A search through the CVS change logs finds there
  1174. were 527 patches applied and 683 bugs fixed between Python 2.1 and 2.2; 2.2.1
  1175. applied 139 patches and fixed 143 bugs; 2.2.2 applied 106 patches and fixed 82
  1176. bugs. These figures are likely to be underestimates.</p>
  1177. <p>Some of the more notable changes are:</p>
  1178. <ul>
  1179. <li><p>The code for the MacOS port for Python, maintained by Jack Jansen, is now kept
  1180. in the main Python CVS tree, and many changes have been made to support MacOS X.</p>
  1181. <p>The most significant change is the ability to build Python as a framework,
  1182. enabled by supplying the <code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-framework</span></code> option to the configure
  1183. script when compiling Python. According to Jack Jansen, “This installs a
  1184. self-contained Python installation plus the OS X framework “glue” into
  1185. <code class="file docutils literal notranslate"><span class="pre">/Library/Frameworks/Python.framework</span></code> (or another location of choice).
  1186. For now there is little immediate added benefit to this (actually, there is the
  1187. disadvantage that you have to change your PATH to be able to find Python), but
  1188. it is the basis for creating a full-blown Python application, porting the
  1189. MacPython IDE, possibly using Python as a standard OSA scripting language and
  1190. much more.”</p>
  1191. <p>Most of the MacPython toolbox modules, which interface to MacOS APIs such as
  1192. windowing, QuickTime, scripting, etc. have been ported to OS X, but they’ve been
  1193. left commented out in <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code>. People who want to experiment with
  1194. these modules can uncomment them manually.</p>
  1195. </li>
  1196. <li><p>Keyword arguments passed to built-in functions that don’t take them now cause a
  1197. <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception to be raised, with the message “<em>function</em> takes no
  1198. keyword arguments”.</p></li>
  1199. <li><p>Weak references, added in Python 2.1 as an extension module, are now part of
  1200. the core because they’re used in the implementation of new-style classes. The
  1201. <a class="reference internal" href="../library/exceptions.html#ReferenceError" title="ReferenceError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ReferenceError</span></code></a> exception has therefore moved from 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>
  1202. module to become a built-in exception.</p></li>
  1203. <li><p>A new script, <code class="file docutils literal notranslate"><span class="pre">Tools/scripts/cleanfuture.py</span></code> by Tim Peters,
  1204. automatically removes obsolete <code class="docutils literal notranslate"><span class="pre">__future__</span></code> statements from Python source
  1205. code.</p></li>
  1206. <li><p>An additional <em>flags</em> argument has been added to the built-in function
  1207. <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>, so the behaviour of <code class="docutils literal notranslate"><span class="pre">__future__</span></code> statements can now be
  1208. correctly observed in simulated shells, such as those presented by IDLE and
  1209. other development environments. This is described in <span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0264/"><strong>PEP 264</strong></a>. (Contributed
  1210. by Michael Hudson.)</p></li>
  1211. <li><p>The new license introduced with Python 1.6 wasn’t GPL-compatible. This is
  1212. fixed by some minor textual changes to the 2.2 license, so it’s now legal to
  1213. embed Python inside a GPLed program again. Note that Python itself is not
  1214. GPLed, but instead is under a license that’s essentially equivalent to the BSD
  1215. license, same as it always was. The license changes were also applied to the
  1216. Python 2.0.1 and 2.1.1 releases.</p></li>
  1217. <li><p>When presented with a Unicode filename on Windows, Python will now convert it
  1218. to an MBCS encoded string, as used by the Microsoft file APIs. As MBCS is
  1219. explicitly used by the file APIs, Python’s choice of ASCII as the default
  1220. encoding turns out to be an annoyance. On Unix, the locale’s character set is
  1221. used if <code class="docutils literal notranslate"><span class="pre">locale.nl_langinfo(CODESET)</span></code> is available. (Windows support was
  1222. contributed by Mark Hammond with assistance from Marc-André Lemburg. Unix
  1223. support was added by Martin von Löwis.)</p></li>
  1224. <li><p>Large file support is now enabled on Windows. (Contributed by Tim Peters.)</p></li>
  1225. <li><p>The <code class="file docutils literal notranslate"><span class="pre">Tools/scripts/ftpmirror.py</span></code> script now parses a <code class="file docutils literal notranslate"><span class="pre">.netrc</span></code>
  1226. file, if you have one. (Contributed by Mike Romberg.)</p></li>
  1227. <li><p>Some features of the object returned by the <code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code> function are now
  1228. deprecated, and trigger warnings when they’re accessed; they’ll disappear in
  1229. Python 2.3. <code class="xref py py-class docutils literal notranslate"><span class="pre">xrange</span></code> objects tried to pretend they were full sequence
  1230. types by supporting slicing, sequence multiplication, and the <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a>
  1231. operator, but these features were rarely used and therefore buggy. The
  1232. <code class="xref py py-meth docutils literal notranslate"><span class="pre">tolist()</span></code> method and the <code class="xref py py-attr docutils literal notranslate"><span class="pre">start</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">stop</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">step</span></code>
  1233. attributes are also being deprecated. At the C level, the fourth argument to
  1234. the <code class="xref c c-func docutils literal notranslate"><span class="pre">PyRange_New()</span></code> function, <code class="docutils literal notranslate"><span class="pre">repeat</span></code>, has also been deprecated.</p></li>
  1235. <li><p>There were a bunch of patches to the dictionary implementation, mostly to fix
  1236. potential core dumps if a dictionary contains objects that sneakily changed
  1237. their hash value, or mutated the dictionary they were contained in. For a while
  1238. python-dev fell into a gentle rhythm of Michael Hudson finding a case that
  1239. dumped core, Tim Peters fixing the bug, Michael finding another case, and round
  1240. and round it went.</p></li>
  1241. <li><p>On Windows, Python can now be compiled with Borland C thanks to a number of
  1242. patches contributed by Stephen Hansen, though the result isn’t fully functional
  1243. yet. (But this <em>is</em> progress…)</p></li>
  1244. <li><p>Another Windows enhancement: Wise Solutions generously offered PythonLabs use
  1245. of their InstallerMaster 8.1 system. Earlier PythonLabs Windows installers used
  1246. Wise 5.0a, which was beginning to show its age. (Packaged up by Tim Peters.)</p></li>
  1247. <li><p>Files ending in <code class="docutils literal notranslate"><span class="pre">.pyw</span></code> can now be imported on Windows. <code class="docutils literal notranslate"><span class="pre">.pyw</span></code> is a
  1248. Windows-only thing, used to indicate that a script needs to be run using
  1249. PYTHONW.EXE instead of PYTHON.EXE in order to prevent a DOS console from popping
  1250. up to display the output. This patch makes it possible to import such scripts,
  1251. in case they’re also usable as modules. (Implemented by David Bolen.)</p></li>
  1252. <li><p>On platforms where Python uses the C <code class="xref c c-func docutils literal notranslate"><span class="pre">dlopen()</span></code> function to load
  1253. extension modules, it’s now possible to set the flags used by <code class="xref c c-func docutils literal notranslate"><span class="pre">dlopen()</span></code>
  1254. using the <a class="reference internal" href="../library/sys.html#sys.getdlopenflags" title="sys.getdlopenflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getdlopenflags()</span></code></a> and <a class="reference internal" href="../library/sys.html#sys.setdlopenflags" title="sys.setdlopenflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setdlopenflags()</span></code></a> functions.
  1255. (Contributed by Bram Stolk.)</p></li>
  1256. <li><p>The <a class="reference internal" href="../library/functions.html#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a> built-in function no longer supports 3 arguments when
  1257. floating-point numbers are supplied. <code class="docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">y,</span> <span class="pre">z)</span></code> returns <code class="docutils literal notranslate"><span class="pre">(x**y)</span> <span class="pre">%</span> <span class="pre">z</span></code>,
  1258. but this is never useful for floating point numbers, and the final result varies
  1259. unpredictably depending on the platform. A call such as <code class="docutils literal notranslate"><span class="pre">pow(2.0,</span> <span class="pre">8.0,</span> <span class="pre">7.0)</span></code>
  1260. will now raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p></li>
  1261. </ul>
  1262. </section>
  1263. <section id="acknowledgements">
  1264. <h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Link to this heading">¶</a></h2>
  1265. <p>The author would like to thank the following people for offering suggestions,
  1266. corrections and assistance with various drafts of this article: Fred Bremmer,
  1267. Keith Briggs, Andrew Dalke, Fred L. Drake, Jr., Carel Fellinger, David Goodger,
  1268. Mark Hammond, Stephen Hansen, Michael Hudson, Jack Jansen, Marc-André Lemburg,
  1269. Martin von Löwis, Fredrik Lundh, Michael McLay, Nick Mathewson, Paul Moore,
  1270. Gustavo Niemeyer, Don O’Donnell, Joonas Paalasma, Tim Peters, Jens Quade, Tom
  1271. Reinhardt, Neil Schemenauer, Guido van Rossum, Greg Ward, Edward Welbourne.</p>
  1272. </section>
  1273. </section>
  1274. <div class="clearer"></div>
  1275. </div>
  1276. </div>
  1277. </div>
  1278. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1279. <div class="sphinxsidebarwrapper">
  1280. <div>
  1281. <h3><a href="../contents.html">Table of Contents</a></h3>
  1282. <ul>
  1283. <li><a class="reference internal" href="#">What’s New in Python 2.2</a><ul>
  1284. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  1285. <li><a class="reference internal" href="#peps-252-and-253-type-and-class-changes">PEPs 252 and 253: Type and Class Changes</a><ul>
  1286. <li><a class="reference internal" href="#old-and-new-classes">Old and New Classes</a></li>
  1287. <li><a class="reference internal" href="#descriptors">Descriptors</a></li>
  1288. <li><a class="reference internal" href="#multiple-inheritance-the-diamond-rule">Multiple Inheritance: The Diamond Rule</a></li>
  1289. <li><a class="reference internal" href="#attribute-access">Attribute Access</a></li>
  1290. <li><a class="reference internal" href="#related-links">Related Links</a></li>
  1291. </ul>
  1292. </li>
  1293. <li><a class="reference internal" href="#pep-234-iterators">PEP 234: Iterators</a></li>
  1294. <li><a class="reference internal" href="#pep-255-simple-generators">PEP 255: Simple Generators</a></li>
  1295. <li><a class="reference internal" href="#pep-237-unifying-long-integers-and-integers">PEP 237: Unifying Long Integers and Integers</a></li>
  1296. <li><a class="reference internal" href="#pep-238-changing-the-division-operator">PEP 238: Changing the Division Operator</a></li>
  1297. <li><a class="reference internal" href="#unicode-changes">Unicode Changes</a></li>
  1298. <li><a class="reference internal" href="#pep-227-nested-scopes">PEP 227: Nested Scopes</a></li>
  1299. <li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a></li>
  1300. <li><a class="reference internal" href="#interpreter-changes-and-fixes">Interpreter Changes and Fixes</a></li>
  1301. <li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
  1302. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  1303. </ul>
  1304. </li>
  1305. </ul>
  1306. </div>
  1307. <div>
  1308. <h4>Previous topic</h4>
  1309. <p class="topless"><a href="2.3.html"
  1310. title="previous chapter">What’s New in Python 2.3</a></p>
  1311. </div>
  1312. <div>
  1313. <h4>Next topic</h4>
  1314. <p class="topless"><a href="2.1.html"
  1315. title="next chapter">What’s New in Python 2.1</a></p>
  1316. </div>
  1317. <div role="note" aria-label="source link">
  1318. <h3>This Page</h3>
  1319. <ul class="this-page-menu">
  1320. <li><a href="../bugs.html">Report a Bug</a></li>
  1321. <li>
  1322. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.2.rst"
  1323. rel="nofollow">Show Source
  1324. </a>
  1325. </li>
  1326. </ul>
  1327. </div>
  1328. </div>
  1329. <div id="sidebarbutton" title="Collapse sidebar">
  1330. <span>«</span>
  1331. </div>
  1332. </div>
  1333. <div class="clearer"></div>
  1334. </div>
  1335. <div class="related" role="navigation" aria-label="related navigation">
  1336. <h3>Navigation</h3>
  1337. <ul>
  1338. <li class="right" style="margin-right: 10px">
  1339. <a href="../genindex.html" title="General Index"
  1340. >index</a></li>
  1341. <li class="right" >
  1342. <a href="../py-modindex.html" title="Python Module Index"
  1343. >modules</a> |</li>
  1344. <li class="right" >
  1345. <a href="2.1.html" title="What’s New in Python 2.1"
  1346. >next</a> |</li>
  1347. <li class="right" >
  1348. <a href="2.3.html" title="What’s New in Python 2.3"
  1349. >previous</a> |</li>
  1350. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1351. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1352. <li class="switchers">
  1353. <div class="language_switcher_placeholder"></div>
  1354. <div class="version_switcher_placeholder"></div>
  1355. </li>
  1356. <li>
  1357. </li>
  1358. <li id="cpython-language-and-version">
  1359. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1360. </li>
  1361. <li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> &#187;</li>
  1362. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.2</a></li>
  1363. <li class="right">
  1364. <div class="inline-search" role="search">
  1365. <form class="inline-search" action="../search.html" method="get">
  1366. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1367. <input type="submit" value="Go" />
  1368. </form>
  1369. </div>
  1370. |
  1371. </li>
  1372. <li class="right">
  1373. <label class="theme-selector-label">
  1374. Theme
  1375. <select class="theme-selector" oninput="activateTheme(this.value)">
  1376. <option value="auto" selected>Auto</option>
  1377. <option value="light">Light</option>
  1378. <option value="dark">Dark</option>
  1379. </select>
  1380. </label> |</li>
  1381. </ul>
  1382. </div>
  1383. <div class="footer">
  1384. &copy;
  1385. <a href="../copyright.html">
  1386. Copyright
  1387. </a>
  1388. 2001-2024, Python Software Foundation.
  1389. <br />
  1390. This page is licensed under the Python Software Foundation License Version 2.
  1391. <br />
  1392. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1393. <br />
  1394. See <a href="/license.html">History and License</a> for more information.<br />
  1395. <br />
  1396. The Python Software Foundation is a non-profit corporation.
  1397. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1398. <br />
  1399. <br />
  1400. Last updated on Apr 09, 2024 (13:47 UTC).
  1401. <a href="/bugs.html">Found a bug</a>?
  1402. <br />
  1403. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1404. </div>
  1405. </body>
  1406. </html>
上海开阖软件有限公司 沪ICP备12045867号-1