Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 3.144.45.187
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
whatsnew /
[ HOME SHELL ]
Name
Size
Permission
Action
2.0.html
121.13
KB
-rw-r--r--
2.1.html
74.86
KB
-rw-r--r--
2.2.html
129.31
KB
-rw-r--r--
2.3.html
214.29
KB
-rw-r--r--
2.4.html
155.06
KB
-rw-r--r--
2.5.html
231.71
KB
-rw-r--r--
2.6.html
329.35
KB
-rw-r--r--
2.7.html
276.15
KB
-rw-r--r--
index.html
24.76
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : 2.2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>What’s New in Python 2.2 — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="What’s New in Python" href="index.html" /> <link rel="next" title="What’s New in Python 2.1" href="2.1.html" /> <link rel="prev" title="What’s New in Python 2.3" href="2.3.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="2.1.html" title="What’s New in Python 2.1" accesskey="N">next</a> |</li> <li class="right" > <a href="2.3.html" title="What’s New in Python 2.3" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" accesskey="U">What’s New in Python</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="what-s-new-in-python-2-2"> <h1>What’s New in Python 2.2<a class="headerlink" href="#what-s-new-in-python-2-2" title="Permalink to this headline">¶</a></h1> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Author:</th><td class="field-body">A.M. Kuchling</td> </tr> </tbody> </table> <div class="section" id="introduction"> <h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2> <p>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 December 21, 2001.</p> <p>Python 2.2 can be thought of as the “cleanup release”. There are some features such as generators and iterators that are completely new, but most of the changes, significant and far-reaching though they may be, are aimed at cleaning up irregularities and dark corners of the language design.</p> <p>This article doesn’t attempt to provide a complete specification of the new features, but instead provides a convenient overview. For full details, you should refer to the documentation for Python 2.2, such as the <a class="reference external" href="http://www.python.org/doc/2.2/lib/lib.html">Python Library Reference</a> and the <a class="reference external" href="http://www.python.org/doc/2.2/ref/ref.html">Python Reference Manual</a>. If you want to understand the complete implementation and design rationale for a change, refer to the PEP for a particular new feature.</p> </div> <div class="section" id="peps-252-and-253-type-and-class-changes"> <h2>PEPs 252 and 253: Type and Class Changes<a class="headerlink" href="#peps-252-and-253-type-and-class-changes" title="Permalink to this headline">¶</a></h2> <p>The largest and most far-reaching changes in Python 2.2 are to Python’s model of objects and classes. The changes should be backward compatible, so it’s likely that your code will continue to run unchanged, but the changes provide some amazing new capabilities. Before beginning this, the longest and most complicated section of this article, I’ll provide an overview of the changes and offer some comments.</p> <p>A long time ago I wrote a Web page listing flaws in Python’s design. One of the most significant flaws was that it’s impossible to subclass Python types implemented in C. In particular, it’s not possible to subclass built-in types, so you can’t just subclass, say, lists in order to add a single useful method to them. The <a class="reference internal" href="../library/userdict.html#module-UserList" title="UserList: Class wrapper for list objects."><tt class="xref py py-mod docutils literal"><span class="pre">UserList</span></tt></a> module provides a class that supports all of the methods of lists and that can be subclassed further, but there’s lots of C code that expects a regular Python list and won’t accept a <a class="reference internal" href="../library/userdict.html#module-UserList" title="UserList: Class wrapper for list objects."><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a> instance.</p> <p>Python 2.2 fixes this, and in the process adds some exciting new capabilities. A brief summary:</p> <ul class="simple"> <li>You can subclass built-in types such as lists and even integers, and your subclasses should work in every place that requires the original type.</li> <li>It’s now possible to define static and class methods, in addition to the instance methods available in previous versions of Python.</li> <li>It’s also possible to automatically call methods on accessing or setting an instance attribute by using a new mechanism called <em class="dfn">properties</em>. Many uses of <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> can be rewritten to use properties instead, making the resulting code simpler and faster. As a small side benefit, attributes can now have docstrings, too.</li> <li>The list of legal attributes for an instance can be limited to a particular set using <em class="dfn">slots</em>, making it possible to safeguard against typos and perhaps make more optimizations possible in future versions of Python.</li> </ul> <p>Some users have voiced concern about all these changes. Sure, they say, the new features are neat and lend themselves to all sorts of tricks that weren’t possible in previous versions of Python, but they also make the language more complicated. Some people have said that they’ve always recommended Python for its simplicity, and feel that its simplicity is being lost.</p> <p>Personally, I think there’s no need to worry. Many of the new features are quite esoteric, and you can write a lot of Python code without ever needed to be aware of them. Writing a simple class is no more difficult than it ever was, so you don’t need to bother learning or teaching them unless they’re actually needed. Some very complicated tasks that were previously only possible from C will now be possible in pure Python, and to my mind that’s all for the better.</p> <p>I’m not going to attempt to cover every single corner case and small change that were required to make the new features work. Instead this section will paint only the broad strokes. See section <a class="reference internal" href="#sect-rellinks"><em>Related Links</em></a>, “Related Links”, for further sources of information about Python 2.2’s new object model.</p> <div class="section" id="old-and-new-classes"> <h3>Old and New Classes<a class="headerlink" href="#old-and-new-classes" title="Permalink to this headline">¶</a></h3> <p>First, you should know that Python 2.2 really has two kinds of classes: classic or old-style classes, and new-style classes. The old-style class model is exactly the same as the class model in earlier versions of Python. All the new features described in this section apply only to new-style classes. This divergence isn’t intended to last forever; eventually old-style classes will be dropped, possibly in Python 3.0.</p> <p>So how do you define a new-style class? You do it by subclassing an existing new-style class. Most of Python’s built-in types, such as integers, lists, dictionaries, and even files, are new-style classes now. A new-style class named <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a>, the base class for all built-in types, has also been added so if no built-in type is suitable, you can just subclass <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span> <span class="o">...</span> </pre></div> </div> <p>This means that <a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a> statements that don’t have any base classes are always classic classes in Python 2.2. (Actually you can also change this by setting a module-level variable named <a class="reference internal" href="../reference/datamodel.html#__metaclass__" title="__metaclass__"><tt class="xref py py-attr docutils literal"><span class="pre">__metaclass__</span></tt></a> — see <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0253"><strong>PEP 253</strong></a> for the details — but it’s easier to just subclass <a class="reference internal" href="../c-api/object.html#object"><tt class="xref std std-keyword docutils literal"><span class="pre">object</span></tt></a>.)</p> <p>The type objects for the built-in types are available as built-ins, named using a clever trick. Python has always had built-in functions named <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a>, <a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a>, and <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a>. In 2.2, they aren’t functions any more, but type objects that behave as factories when called.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">int</span> <span class="go"><type 'int'></span> <span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="s">'123'</span><span class="p">)</span> <span class="go">123</span> </pre></div> </div> <p>To make the set of types complete, new type objects such as <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><tt class="xref py py-func docutils literal"><span class="pre">dict()</span></tt></a> and <a class="reference internal" href="../library/functions.html#file" title="file"><tt class="xref py py-func docutils literal"><span class="pre">file()</span></tt></a> have been added. Here’s a more interesting example, adding a <tt class="xref py py-meth docutils literal"><span class="pre">lock()</span></tt> method to file objects:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">LockableFile</span><span class="p">(</span><span class="nb">file</span><span class="p">):</span> <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> <span class="kn">import</span> <span class="nn">fcntl</span> <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> <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> </pre></div> </div> <p>The now-obsolete <a class="reference internal" href="../library/posixfile.html#module-posixfile" title="posixfile: A file-like object with support for locking. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">posixfile</span></tt></a> module contained a class that emulated all of a file object’s methods and also added a <tt class="xref py py-meth docutils literal"><span class="pre">lock()</span></tt> method, but this class couldn’t be passed to internal functions that expected a built-in file, something which is possible with our new <tt class="xref py py-class docutils literal"><span class="pre">LockableFile</span></tt>.</p> </div> <div class="section" id="descriptors"> <h3>Descriptors<a class="headerlink" href="#descriptors" title="Permalink to this headline">¶</a></h3> <p>In previous versions of Python, there was no consistent way to discover what attributes and methods were supported by an object. There were some informal conventions, such as defining <tt class="xref py py-attr docutils literal"><span class="pre">__members__</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">__methods__</span></tt> attributes that were lists of names, but often the author of an extension type or a class wouldn’t bother to define them. You could fall back on inspecting the <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt> of an object, but when class inheritance or an arbitrary <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> hook were in use this could still be inaccurate.</p> <p>The one big idea underlying the new class model is that an API for describing the attributes of an object using <em class="dfn">descriptors</em> has been formalized. Descriptors specify the value of an attribute, stating whether it’s a method or a field. With the descriptor API, static methods and class methods become possible, as well as more exotic constructs.</p> <p>Attribute descriptors are objects that live inside class objects, and have a few attributes of their own:</p> <ul class="simple"> <li><tt class="xref py py-attr docutils literal"><span class="pre">__name__</span></tt> is the attribute’s name.</li> <li><tt class="xref py py-attr docutils literal"><span class="pre">__doc__</span></tt> is the attribute’s docstring.</li> <li><tt class="xref py py-meth docutils literal"><span class="pre">__get__(object)()</span></tt> is a method that retrieves the attribute value from <em>object</em>.</li> <li><tt class="xref py py-meth docutils literal"><span class="pre">__set__(object,</span> <span class="pre">value)()</span></tt> sets the attribute on <em>object</em> to <em>value</em>.</li> <li><tt class="xref py py-meth docutils literal"><span class="pre">__delete__(object,</span> <span class="pre">value)()</span></tt> deletes the <em>value</em> attribute of <em>object</em>.</li> </ul> <p>For example, when you write <tt class="docutils literal"><span class="pre">obj.x</span></tt>, the steps that Python actually performs are:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">descriptor</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">x</span> <span class="n">descriptor</span><span class="o">.</span><span class="n">__get__</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span> </pre></div> </div> <p>For methods, <tt class="xref py py-meth docutils literal"><span class="pre">descriptor.__get__()</span></tt> returns a temporary object that’s callable, and wraps up the instance and the method to be called on it. This is also why static methods and class methods are now possible; they have descriptors that wrap up just the method, or the method and the class. As a brief explanation of these new kinds of methods, static methods aren’t passed the instance, and therefore resemble regular functions. Class methods are passed the class of the object, but not the object itself. Static and class methods are defined like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <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> <span class="o">...</span> <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> <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span> <span class="o">...</span> <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> </pre></div> </div> <p>The <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><tt class="xref py py-func docutils literal"><span class="pre">staticmethod()</span></tt></a> function takes the function <tt class="xref py py-func docutils literal"><span class="pre">f()</span></tt>, and returns it wrapped up in a descriptor so it can be stored in the class object. You might expect there to be special syntax for creating such methods (<tt class="docutils literal"><span class="pre">def</span> <span class="pre">static</span> <span class="pre">f</span></tt>, <tt class="docutils literal"><span class="pre">defstatic</span> <span class="pre">f()</span></tt>, or something like that) but no such syntax has been defined yet; that’s been left for future versions of Python.</p> <p>More new features, such as slots and properties, are also implemented as new kinds of descriptors, and it’s not difficult to write a descriptor class that does something novel. For example, it would be possible to write a descriptor class that made it possible to write Eiffel-style preconditions and postconditions for a method. A class that used this feature might be defined like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">eiffel</span> <span class="kn">import</span> <span class="n">eiffelmethod</span> <span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <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> <span class="c"># The actual function</span> <span class="o">...</span> <span class="k">def</span> <span class="nf">pre_f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># Check preconditions</span> <span class="o">...</span> <span class="k">def</span> <span class="nf">post_f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># Check postconditions</span> <span class="o">...</span> <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> </pre></div> </div> <p>Note that a person using the new <tt class="xref py py-func docutils literal"><span class="pre">eiffelmethod()</span></tt> doesn’t have to understand anything about descriptors. This is why I think the new features don’t increase the basic complexity of the language. There will be a few wizards who need to know about it in order to write <tt class="xref py py-func docutils literal"><span class="pre">eiffelmethod()</span></tt> or the ZODB or whatever, but most users will just write code on top of the resulting libraries and ignore the implementation details.</p> </div> <div class="section" id="multiple-inheritance-the-diamond-rule"> <h3>Multiple Inheritance: The Diamond Rule<a class="headerlink" href="#multiple-inheritance-the-diamond-rule" title="Permalink to this headline">¶</a></h3> <p>Multiple inheritance has also been made more useful through changing the rules under which names are resolved. Consider this set of classes (diagram taken from <span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0253"><strong>PEP 253</strong></a> by Guido van Rossum):</p> <div class="highlight-python"><pre> class A: ^ ^ def save(self): ... / \ / \ / \ / \ class B class C: ^ ^ def save(self): ... \ / \ / \ / \ / class D</pre> </div> <p>The lookup rule for classic classes is simple but not very smart; the base classes are searched depth-first, going from left to right. A reference to <tt class="xref py py-meth docutils literal"><span class="pre">D.save()</span></tt> will search the classes <tt class="xref py py-class docutils literal"><span class="pre">D</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">B</span></tt>, and then <tt class="xref py py-class docutils literal"><span class="pre">A</span></tt>, where <tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt> would be found and returned. <tt class="xref py py-meth docutils literal"><span class="pre">C.save()</span></tt> would never be found at all. This is bad, because if <tt class="xref py py-class docutils literal"><span class="pre">C</span></tt>‘s <tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt> method is saving some internal state specific to <tt class="xref py py-class docutils literal"><span class="pre">C</span></tt>, not calling it will result in that state never getting saved.</p> <p>New-style classes follow a different algorithm that’s a bit more complicated to explain, but does the right thing in this situation. (Note that Python 2.3 changes this algorithm to one that produces the same results in most cases, but produces more useful results for really complicated inheritance graphs.)</p> <ol class="arabic simple"> <li>List all the base classes, following the classic lookup rule and include a class multiple times if it’s visited repeatedly. In the above example, the list of visited classes is [<tt class="xref py py-class docutils literal"><span class="pre">D</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">B</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">A</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">C</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">A</span></tt>].</li> <li>Scan the list for duplicated classes. If any are found, remove all but one occurrence, leaving the <em>last</em> one in the list. In the above example, the list becomes [<tt class="xref py py-class docutils literal"><span class="pre">D</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">B</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">C</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">A</span></tt>] after dropping duplicates.</li> </ol> <p>Following this rule, referring to <tt class="xref py py-meth docutils literal"><span class="pre">D.save()</span></tt> will return <tt class="xref py py-meth docutils literal"><span class="pre">C.save()</span></tt>, which is the behaviour we’re after. This lookup rule is the same as the one followed by Common Lisp. A new built-in function, <a class="reference internal" href="../library/functions.html#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a>, provides a way to get at a class’s superclasses without having to reimplement Python’s algorithm. The most commonly used form will be <tt class="xref py py-func docutils literal"><span class="pre">super(class,</span> <span class="pre">obj)()</span></tt>, which returns a bound superclass object (not the actual class object). This form will be used in methods to call a method in the superclass; for example, <tt class="xref py py-class docutils literal"><span class="pre">D</span></tt>‘s <tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt> method would look like this:</p> <div class="highlight-python"><div class="highlight"><pre><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> <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="c"># Call superclass .save()</span> <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> <span class="c"># Save D's private information here</span> <span class="o">...</span> </pre></div> </div> <p><a class="reference internal" href="../library/functions.html#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a> can also return unbound superclass objects when called as <tt class="xref py py-func docutils literal"><span class="pre">super(class)()</span></tt> or <tt class="xref py py-func docutils literal"><span class="pre">super(class1,</span> <span class="pre">class2)()</span></tt>, but this probably won’t often be useful.</p> </div> <div class="section" id="attribute-access"> <h3>Attribute Access<a class="headerlink" href="#attribute-access" title="Permalink to this headline">¶</a></h3> <p>A fair number of sophisticated Python classes define hooks for attribute access using <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a>; most commonly this is done for convenience, to make code more readable by automatically mapping an attribute access such as <tt class="docutils literal"><span class="pre">obj.parent</span></tt> into a method call such as <tt class="docutils literal"><span class="pre">obj.get_parent</span></tt>. Python 2.2 adds some new ways of controlling attribute access.</p> <p>First, <tt class="xref py py-meth docutils literal"><span class="pre">__getattr__(attr_name)()</span></tt> is still supported by new-style classes, and nothing about it has changed. As before, it will be called when an attempt is made to access <tt class="docutils literal"><span class="pre">obj.foo</span></tt> and no attribute named <tt class="docutils literal"><span class="pre">foo</span></tt> is found in the instance’s dictionary.</p> <p>New-style classes also support a new method, <tt class="xref py py-meth docutils literal"><span class="pre">__getattribute__(attr_name)()</span></tt>. The difference between the two methods is that <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattribute__()</span></tt></a> is <em>always</em> called whenever any attribute is accessed, while the old <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> is only called if <tt class="docutils literal"><span class="pre">foo</span></tt> isn’t found in the instance’s dictionary.</p> <p>However, Python 2.2’s support for <em class="dfn">properties</em> will often be a simpler way to trap attribute references. Writing a <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> method is complicated because to avoid recursion you can’t use regular attribute accesses inside them, and instead have to mess around with the contents of <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt>. <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> methods also end up being called by Python when it checks for other methods such as <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> or <a class="reference internal" href="../reference/datamodel.html#object.__coerce__" title="object.__coerce__"><tt class="xref py py-meth docutils literal"><span class="pre">__coerce__()</span></tt></a>, and so have to be written with this in mind. Finally, calling a function on every attribute access results in a sizable performance loss.</p> <p><a class="reference internal" href="../library/functions.html#property" title="property"><tt class="xref py py-class docutils literal"><span class="pre">property</span></tt></a> is a new built-in type that packages up three functions that get, set, or delete an attribute, and a docstring. For example, if you want to define a <tt class="xref py py-attr docutils literal"><span class="pre">size</span></tt> attribute that’s computed, but also settable, you could write:</p> <div class="highlight-python"><pre>class C(object): def get_size (self): result = ... computation ... return result def set_size (self, size): ... compute something based on the size and set internal state appropriately ... # Define a property. The 'delete this attribute' # method is defined as None, so the attribute # can't be deleted. size = property(get_size, set_size, None, "Storage size of this instance")</pre> </div> <p>That is certainly clearer and easier to write than a pair of <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a>/<a class="reference internal" href="../reference/datamodel.html#object.__setattr__" title="object.__setattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__setattr__()</span></tt></a> methods that check for the <tt class="xref py py-attr docutils literal"><span class="pre">size</span></tt> attribute and handle it specially while retrieving all other attributes from the instance’s <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt>. Accesses to <tt class="xref py py-attr docutils literal"><span class="pre">size</span></tt> are also the only ones which have to perform the work of calling a function, so references to other attributes run at their usual speed.</p> <p>Finally, it’s possible to constrain the list of attributes that can be referenced on an object using the new <a class="reference internal" href="../reference/datamodel.html#__slots__" title="__slots__"><tt class="xref py py-attr docutils literal"><span class="pre">__slots__</span></tt></a> class attribute. Python objects are usually very dynamic; at any time it’s possible to define a new attribute on an instance by just doing <tt class="docutils literal"><span class="pre">obj.new_attr=1</span></tt>. A new-style class can define a class attribute named <a class="reference internal" href="../reference/datamodel.html#__slots__" title="__slots__"><tt class="xref py py-attr docutils literal"><span class="pre">__slots__</span></tt></a> to limit the legal attributes to a particular set of names. An example will make this clear:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="gp">... </span> <span class="n">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s">'template'</span><span class="p">,</span> <span class="s">'name'</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">obj</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">template</span> <span class="go">None</span> <span class="gp">>>> </span><span class="n">obj</span><span class="o">.</span><span class="n">template</span> <span class="o">=</span> <span class="s">'Test'</span> <span class="gp">>>> </span><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">template</span> <span class="go">Test</span> <span class="gp">>>> </span><span class="n">obj</span><span class="o">.</span><span class="n">newattr</span> <span class="o">=</span> <span class="bp">None</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> <span class="gr">AttributeError</span>: <span class="n">'C' object has no attribute 'newattr'</span> </pre></div> </div> <p>Note how you get an <a class="reference internal" href="../library/exceptions.html#exceptions.AttributeError" title="exceptions.AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a> on the attempt to assign to an attribute not listed in <a class="reference internal" href="../reference/datamodel.html#__slots__" title="__slots__"><tt class="xref py py-attr docutils literal"><span class="pre">__slots__</span></tt></a>.</p> </div> <div class="section" id="related-links"> <span id="sect-rellinks"></span><h3>Related Links<a class="headerlink" href="#related-links" title="Permalink to this headline">¶</a></h3> <p>This section has just been a quick overview of the new features, giving enough of an explanation to start you programming, but many details have been simplified or ignored. Where should you go to get a more complete picture?</p> <p><a class="reference external" href="http://www.python.org/2.2/descrintro.html">http://www.python.org/2.2/descrintro.html</a> is a lengthy tutorial introduction to the descriptor features, written by Guido van Rossum. If my description has whetted your appetite, go read this tutorial next, because it goes into much more detail about the new features while still remaining quite easy to read.</p> <p>Next, there are two relevant PEPs, <span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0252"><strong>PEP 252</strong></a> and <span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0253"><strong>PEP 253</strong></a>. <span class="target" id="index-4"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0252"><strong>PEP 252</strong></a> is titled “Making Types Look More Like Classes”, and covers the descriptor API. <span class="target" id="index-5"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0253"><strong>PEP 253</strong></a> is titled “Subtyping Built-in Types”, and describes the changes to type objects that make it possible to subtype built-in objects. <span class="target" id="index-6"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0253"><strong>PEP 253</strong></a> is the more complicated PEP of the two, and at a few points the necessary explanations of types and meta-types may cause your head to explode. Both PEPs were written and implemented by Guido van Rossum, with substantial assistance from the rest of the Zope Corp. team.</p> <p>Finally, there’s the ultimate authority: the source code. Most of the machinery for the type handling is in <tt class="file docutils literal"><span class="pre">Objects/typeobject.c</span></tt>, but you should only resort to it after all other avenues have been exhausted, including posting a question to python-list or python-dev.</p> </div> </div> <div class="section" id="pep-234-iterators"> <h2>PEP 234: Iterators<a class="headerlink" href="#pep-234-iterators" title="Permalink to this headline">¶</a></h2> <p>Another significant addition to 2.2 is an iteration interface at both the C and Python levels. Objects can define how they can be looped over by callers.</p> <p>In Python versions up to 2.1, the usual way to make <tt class="docutils literal"><span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">obj</span></tt> work is to define a <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> method that looks something like this:</p> <div class="highlight-python"><pre>def __getitem__(self, index): return <next item></pre> </div> <p><a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> is more properly used to define an indexing operation on an object so that you can write <tt class="docutils literal"><span class="pre">obj[5]</span></tt> to retrieve the sixth element. It’s a bit misleading when you’re using this only to support <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> loops. Consider some file-like object that wants to be looped over; the <em>index</em> parameter is essentially meaningless, as the class probably assumes that a series of <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> calls will be made with <em>index</em> incrementing by one each time. In other words, the presence of the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> method doesn’t mean that using <tt class="docutils literal"><span class="pre">file[5]</span></tt> to randomly access the sixth element will work, though it really should.</p> <p>In Python 2.2, iteration can be implemented separately, and <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> methods can be limited to classes that really do support random access. The basic idea of iterators is simple. A new built-in function, <tt class="xref py py-func docutils literal"><span class="pre">iter(obj)()</span></tt> or <tt class="docutils literal"><span class="pre">iter(C,</span> <span class="pre">sentinel)</span></tt>, is used to get an iterator. <tt class="xref py py-func docutils literal"><span class="pre">iter(obj)()</span></tt> returns an iterator for the object <em>obj</em>, while <tt class="docutils literal"><span class="pre">iter(C,</span> <span class="pre">sentinel)</span></tt> returns an iterator that will invoke the callable object <em>C</em> until it returns <em>sentinel</em> to signal that the iterator is done.</p> <p>Python classes can define an <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> method, which should create and return a new iterator for the object; if the object is its own iterator, this method can just return <tt class="docutils literal"><span class="pre">self</span></tt>. In particular, iterators will usually be their own iterators. Extension types implemented in C can implement a <tt class="xref py py-attr docutils literal"><span class="pre">tp_iter</span></tt> function in order to return an iterator, and extension types that want to behave as iterators can define a <tt class="xref py py-attr docutils literal"><span class="pre">tp_iternext</span></tt> function.</p> <p>So, after all this, what do iterators actually do? They have one required method, <a class="reference internal" href="../library/functions.html#next" title="next"><tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt></a>, which takes no arguments and returns the next value. When there are no more values to be returned, calling <a class="reference internal" href="../library/functions.html#next" title="next"><tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt></a> should raise the <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> exception.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span> <span class="gp">>>> </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> <span class="gp">>>> </span><span class="k">print</span> <span class="n">i</span> <span class="go"><iterator object at 0x8116870></span> <span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="go">2</span> <span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="go">3</span> <span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> <span class="gr">StopIteration</span> <span class="go">>>></span> </pre></div> </div> <p>In 2.2, Python’s <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> statement no longer expects a sequence; it expects something for which <a class="reference internal" href="../library/functions.html#iter" title="iter"><tt class="xref py py-func docutils literal"><span class="pre">iter()</span></tt></a> will return an iterator. For backward compatibility and convenience, an iterator is automatically constructed for sequences that don’t implement <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> or a <tt class="xref py py-attr docutils literal"><span class="pre">tp_iter</span></tt> slot, so <tt class="docutils literal"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">[1,2,3]</span></tt> will still work. Wherever the Python interpreter loops over a sequence, it’s been changed to use the iterator protocol. This means you can do things like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span> <span class="gp">>>> </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> <span class="gp">>>> </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> <span class="gp">>>> </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="go">(1, 2, 3)</span> </pre></div> </div> <p>Iterator support has been added to some of Python’s basic types. Calling <a class="reference internal" href="../library/functions.html#iter" title="iter"><tt class="xref py py-func docutils literal"><span class="pre">iter()</span></tt></a> on a dictionary will return an iterator which loops over its keys:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="p">{</span><span class="s">'Jan'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'Feb'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'Mar'</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s">'Apr'</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="s">'May'</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="s">'Jun'</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span> <span class="gp">... </span> <span class="s">'Jul'</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span> <span class="s">'Aug'</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span> <span class="s">'Sep'</span><span class="p">:</span> <span class="mi">9</span><span class="p">,</span> <span class="s">'Oct'</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span> <span class="s">'Nov'</span><span class="p">:</span> <span class="mi">11</span><span class="p">,</span> <span class="s">'Dec'</span><span class="p">:</span> <span class="mi">12</span><span class="p">}</span> <span class="gp">>>> </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="k">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> <span class="gp">...</span> <span class="go">Mar 3</span> <span class="go">Feb 2</span> <span class="go">Aug 8</span> <span class="go">Sep 9</span> <span class="go">May 5</span> <span class="go">Jun 6</span> <span class="go">Jul 7</span> <span class="go">Jan 1</span> <span class="go">Apr 4</span> <span class="go">Nov 11</span> <span class="go">Dec 12</span> <span class="go">Oct 10</span> </pre></div> </div> <p>That’s just the default behaviour. If you want to iterate over keys, values, or key/value pairs, you can explicitly call the <tt class="xref py py-meth docutils literal"><span class="pre">iterkeys()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">itervalues()</span></tt>, or <tt class="xref py py-meth docutils literal"><span class="pre">iteritems()</span></tt> methods to get an appropriate iterator. In a minor related change, the <a class="reference internal" href="../reference/expressions.html#in"><tt class="xref std std-keyword docutils literal"><span class="pre">in</span></tt></a> operator now works on dictionaries, so <tt class="docutils literal"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">dict</span></tt> is now equivalent to <tt class="docutils literal"><span class="pre">dict.has_key(key)</span></tt>.</p> <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)"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> method until there are no more lines in the file. This means you can now read each line of a file using code like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">file</span><span class="p">:</span> <span class="c"># do something for each line</span> <span class="o">...</span> </pre></div> </div> <p>Note that you can only go forward in an iterator; there’s no way to get the previous element, reset the iterator, or make a copy of it. An iterator object could provide such additional capabilities, but the iterator protocol only requires a <a class="reference internal" href="../library/functions.html#next" title="next"><tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt></a> method.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><span class="target" id="index-7"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0234"><strong>PEP 234</strong></a> - Iterators</dt> <dd>Written by Ka-Ping Yee and GvR; implemented by the Python Labs crew, mostly by GvR and Tim Peters.</dd> </dl> </div> </div> <div class="section" id="pep-255-simple-generators"> <h2>PEP 255: Simple Generators<a class="headerlink" href="#pep-255-simple-generators" title="Permalink to this headline">¶</a></h2> <p>Generators are another new feature, one that interacts with the introduction of iterators.</p> <p>You’re doubtless familiar with how function calls work in Python or C. When you call a function, it gets a private namespace where its local variables are created. When the function reaches a <a class="reference internal" href="../reference/simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> statement, the local variables are destroyed and the resulting value is returned to the caller. A later call to the same function will get a fresh new set of local variables. But, what if the local variables weren’t thrown away on exiting a function? What if you could later resume the function where it left off? This is what generators provide; they can be thought of as resumable functions.</p> <p>Here’s the simplest example of a generator function:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">generate_ints</span><span class="p">(</span><span class="n">N</span><span class="p">):</span> <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> <span class="k">yield</span> <span class="n">i</span> </pre></div> </div> <p>A new keyword, <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a>, was introduced for generators. Any function containing a <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> statement is a generator function; this is detected by Python’s bytecode compiler which compiles the function specially as a result. Because a new keyword was introduced, generators must be explicitly enabled in a module by including a <tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">generators</span></tt> statement near the top of the module’s source code. In Python 2.3 this statement will become unnecessary.</p> <p>When you call a generator function, it doesn’t return a single value; instead it returns a generator object that supports the iterator protocol. On executing the <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> statement, the generator outputs the value of <tt class="docutils literal"><span class="pre">i</span></tt>, similar to a <a class="reference internal" href="../reference/simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> statement. The big difference between <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> and a <a class="reference internal" href="../reference/simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> statement is that on reaching a <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> the generator’s state of execution is suspended and local variables are preserved. On the next call to the generator’s <tt class="docutils literal"><span class="pre">next()</span></tt> method, the function will resume executing immediately after the <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> statement. (For complicated reasons, the <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> statement isn’t allowed inside the <a class="reference internal" href="../reference/compound_stmts.html#try"><tt class="xref std std-keyword docutils literal"><span class="pre">try</span></tt></a> block of a <a class="reference internal" href="../reference/compound_stmts.html#try"><tt class="xref std std-keyword docutils literal"><span class="pre">try</span></tt></a>...<a class="reference internal" href="../reference/compound_stmts.html#finally"><tt class="xref std std-keyword docutils literal"><span class="pre">finally</span></tt></a> statement; read <span class="target" id="index-8"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0255"><strong>PEP 255</strong></a> for a full explanation of the interaction between <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> and exceptions.)</p> <p>Here’s a sample usage of the <tt class="xref py py-func docutils literal"><span class="pre">generate_ints()</span></tt> generator:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="gp">>>> </span><span class="n">gen</span> <span class="go"><generator object at 0x8117f90></span> <span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="go">2</span> <span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> File <span class="nb">"<stdin>"</span>, line <span class="m">2</span>, in <span class="n">generate_ints</span> <span class="gr">StopIteration</span> </pre></div> </div> <p>You could equally write <tt class="docutils literal"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">generate_ints(5)</span></tt>, or <tt class="docutils literal"><span class="pre">a,b,c</span> <span class="pre">=</span> <span class="pre">generate_ints(3)</span></tt>.</p> <p>Inside a generator function, the <a class="reference internal" href="../reference/simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> statement can only be used without a value, and signals the end of the procession of values; afterwards the generator cannot return any further values. <a class="reference internal" href="../reference/simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> with a value, such as <tt class="docutils literal"><span class="pre">return</span> <span class="pre">5</span></tt>, is a syntax error inside a generator function. The end of the generator’s results can also be indicated by raising <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> manually, or by just letting the flow of execution fall off the bottom of the function.</p> <p>You could achieve the effect of generators manually by writing your own class and storing all the local variables of the generator as instance variables. For example, returning a list of integers could be done by setting <tt class="docutils literal"><span class="pre">self.count</span></tt> to 0, and having the <a class="reference internal" href="../library/functions.html#next" title="next"><tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt></a> method increment <tt class="docutils literal"><span class="pre">self.count</span></tt> and return it. However, for a moderately complicated generator, writing a corresponding class would be much messier. <tt class="file docutils literal"><span class="pre">Lib/test/test_generators.py</span></tt> contains a number of more interesting examples. The simplest one implements an in-order traversal of a tree using generators recursively.</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># A recursive generator that generates Tree leaves in in-order.</span> <span class="k">def</span> <span class="nf">inorder</span><span class="p">(</span><span class="n">t</span><span class="p">):</span> <span class="k">if</span> <span class="n">t</span><span class="p">:</span> <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> <span class="k">yield</span> <span class="n">x</span> <span class="k">yield</span> <span class="n">t</span><span class="o">.</span><span class="n">label</span> <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> <span class="k">yield</span> <span class="n">x</span> </pre></div> </div> <p>Two other examples in <tt class="file docutils literal"><span class="pre">Lib/test/test_generators.py</span></tt> produce solutions for the N-Queens problem (placing $N$ queens on an $NxN$ chess board so that no queen threatens another) and the Knight’s Tour (a route that takes a knight to every square of an $NxN$ chessboard without visiting any square twice).</p> <p>The idea of generators comes from other programming languages, especially Icon (<a class="reference external" href="http://www.cs.arizona.edu/icon/">http://www.cs.arizona.edu/icon/</a>), where the idea of generators is central. In Icon, every expression and function call behaves like a generator. One example from “An Overview of the Icon Programming Language” at <a class="reference external" href="http://www.cs.arizona.edu/icon/docs/ipd266.htm">http://www.cs.arizona.edu/icon/docs/ipd266.htm</a> gives an idea of what this looks like:</p> <div class="highlight-python"><pre>sentence := "Store it in the neighboring harbor" if (i := find("or", sentence)) > 5 then write(i)</pre> </div> <p>In Icon the <tt class="xref py py-func docutils literal"><span class="pre">find()</span></tt> function returns the indexes at which the substring “or” is found: 3, 23, 33. In the <a class="reference internal" href="../reference/compound_stmts.html#if"><tt class="xref std std-keyword docutils literal"><span class="pre">if</span></tt></a> statement, <tt class="docutils literal"><span class="pre">i</span></tt> is first assigned a value of 3, but 3 is less than 5, so the comparison fails, and Icon retries it with the second value of 23. 23 is greater than 5, so the comparison now succeeds, and the code prints the value 23 to the screen.</p> <p>Python doesn’t go nearly as far as Icon in adopting generators as a central concept. Generators are considered a new part of the core Python language, but learning or using them isn’t compulsory; if they don’t solve any problems that you have, feel free to ignore them. One novel feature of Python’s interface as compared to Icon’s is that a generator’s state is represented as a concrete object (the iterator) that can be passed around to other functions or stored in a data structure.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><span class="target" id="index-9"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0255"><strong>PEP 255</strong></a> - Simple Generators</dt> <dd>Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer and Tim Peters, with other fixes from the Python Labs crew.</dd> </dl> </div> </div> <div class="section" id="pep-237-unifying-long-integers-and-integers"> <h2>PEP 237: Unifying Long Integers and Integers<a class="headerlink" href="#pep-237-unifying-long-integers-and-integers" title="Permalink to this headline">¶</a></h2> <p>In recent versions, the distinction between regular integers, which are 32-bit values on most machines, and long integers, which can be of arbitrary size, was becoming an annoyance. For example, on platforms that support files larger than <tt class="docutils literal"><span class="pre">2**32</span></tt> bytes, the <tt class="xref py py-meth docutils literal"><span class="pre">tell()</span></tt> method of file objects has to return a long integer. However, there were various bits of Python that expected plain integers and would raise an error if a long integer was provided instead. For example, in Python 1.5, only regular integers could be used as a slice index, and <tt class="docutils literal"><span class="pre">'abc'[1L:]</span></tt> would raise a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception with the message ‘slice index must be int’.</p> <p>Python 2.2 will shift values from short to long integers as required. The ‘L’ suffix is no longer needed to indicate a long integer literal, as now the compiler will choose the appropriate type. (Using the ‘L’ suffix will be discouraged in future 2.x versions of Python, triggering a warning in Python 2.4, and probably dropped in Python 3.0.) Many operations that used to raise an <a class="reference internal" href="../library/exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> will now return a long integer as their result. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">1234567890123</span> <span class="go">1234567890123L</span> <span class="gp">>>> </span><span class="mi">2</span> <span class="o">**</span> <span class="mi">64</span> <span class="go">18446744073709551616L</span> </pre></div> </div> <p>In most cases, integers and long integers will now be treated identically. You can still distinguish them with the <a class="reference internal" href="../library/functions.html#type" title="type"><tt class="xref py py-func docutils literal"><span class="pre">type()</span></tt></a> built-in function, but that’s rarely needed.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><span class="target" id="index-10"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0237"><strong>PEP 237</strong></a> - Unifying Long Integers and Integers</dt> <dd>Written by Moshe Zadka and Guido van Rossum. Implemented mostly by Guido van Rossum.</dd> </dl> </div> </div> <div class="section" id="pep-238-changing-the-division-operator"> <h2>PEP 238: Changing the Division Operator<a class="headerlink" href="#pep-238-changing-the-division-operator" title="Permalink to this headline">¶</a></h2> <p>The most controversial change in Python 2.2 heralds the start of an effort to fix an old design flaw that’s been in Python from the beginning. Currently Python’s division operator, <tt class="docutils literal"><span class="pre">/</span></tt>, behaves like C’s division operator when presented with two integer arguments: it returns an integer result that’s truncated down when there would be a fractional part. For example, <tt class="docutils literal"><span class="pre">3/2</span></tt> is 1, not 1.5, and <tt class="docutils literal"><span class="pre">(-1)/2</span></tt> is -1, not -0.5. This means that the results of division can vary unexpectedly depending on the type of the two operands and because Python is dynamically typed, it can be difficult to determine the possible types of the operands.</p> <p>(The controversy is over whether this is <em>really</em> a design flaw, and whether it’s worth breaking existing code to fix this. It’s caused endless discussions on python-dev, and in July 2001 erupted into an storm of acidly sarcastic postings on <em class="newsgroup">comp.lang.python</em>. I won’t argue for either side here 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="http://www.python.org/dev/peps/pep-0238"><strong>PEP 238</strong></a> for a summary of arguments and counter-arguments.)</p> <p>Because this change might break code, it’s being introduced very gradually. Python 2.2 begins the transition, but the switch won’t be complete until Python 3.0.</p> <p>First, I’ll borrow some terminology from <span class="target" id="index-12"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0238"><strong>PEP 238</strong></a>. “True division” is the division that most non-programmers are familiar with: 3/2 is 1.5, 1/4 is 0.25, and so forth. “Floor division” is what Python’s <tt class="docutils literal"><span class="pre">/</span></tt> operator currently does when given integer operands; the result is the floor of the value returned by true division. “Classic division” is the current mixed behaviour of <tt class="docutils literal"><span class="pre">/</span></tt>; it returns the result of floor division when the operands are integers, and returns the result of true division when one of the operands is a floating-point number.</p> <p>Here are the changes 2.2 introduces:</p> <ul> <li><p class="first">A new operator, <tt class="docutils literal"><span class="pre">//</span></tt>, is the floor division operator. (Yes, we know it looks like C++’s comment symbol.) <tt class="docutils literal"><span class="pre">//</span></tt> <em>always</em> performs floor division no matter what the types of its operands are, so <tt class="docutils literal"><span class="pre">1</span> <span class="pre">//</span> <span class="pre">2</span></tt> is 0 and <tt class="docutils literal"><span class="pre">1.0</span> <span class="pre">//</span> <span class="pre">2.0</span></tt> is also 0.0.</p> <p><tt class="docutils literal"><span class="pre">//</span></tt> is always available in Python 2.2; you don’t need to enable it using a <tt class="docutils literal"><span class="pre">__future__</span></tt> statement.</p> </li> <li><p class="first">By including a <tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></tt> in a module, the <tt class="docutils literal"><span class="pre">/</span></tt> operator will be changed to return the result of true division, so <tt class="docutils literal"><span class="pre">1/2</span></tt> is 0.5. Without the <tt class="docutils literal"><span class="pre">__future__</span></tt> statement, <tt class="docutils literal"><span class="pre">/</span></tt> still means classic division. The default meaning of <tt class="docutils literal"><span class="pre">/</span></tt> will not change until Python 3.0.</p> </li> <li><p class="first">Classes can define methods called <a class="reference internal" href="../reference/datamodel.html#object.__truediv__" title="object.__truediv__"><tt class="xref py py-meth docutils literal"><span class="pre">__truediv__()</span></tt></a> and <a class="reference internal" href="../reference/datamodel.html#object.__floordiv__" title="object.__floordiv__"><tt class="xref py py-meth docutils literal"><span class="pre">__floordiv__()</span></tt></a> to overload the two division operators. At the C level, there are also slots in the <a class="reference internal" href="../c-api/typeobj.html#PyNumberMethods" title="PyNumberMethods"><tt class="xref c c-type docutils literal"><span class="pre">PyNumberMethods</span></tt></a> structure so extension types can define the two operators.</p> </li> <li><p class="first">Python 2.2 supports some command-line arguments for testing whether code will works with the changed division semantics. Running python with <em class="xref std std-option">-Q warn</em> will cause a warning to be issued whenever division is applied to two integers. You can use this to find code that’s affected by the change and fix it. By default, Python 2.2 will simply perform classic division without a warning; the warning will be turned on by default in Python 2.3.</p> </li> </ul> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><span class="target" id="index-13"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0238"><strong>PEP 238</strong></a> - Changing the Division Operator</dt> <dd>Written by Moshe Zadka and Guido van Rossum. Implemented by Guido van Rossum..</dd> </dl> </div> </div> <div class="section" id="unicode-changes"> <h2>Unicode Changes<a class="headerlink" href="#unicode-changes" title="Permalink to this headline">¶</a></h2> <p>Python’s Unicode support has been enhanced a bit in 2.2. Unicode strings are usually stored as UCS-2, as 16-bit unsigned integers. Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned integers, as its internal encoding by supplying <em class="xref std std-option">--enable-unicode=ucs4</em> to the configure script. (It’s also possible to specify <em class="xref std std-option">--disable-unicode</em> to completely disable Unicode support.)</p> <p>When built to use UCS-4 (a “wide Python”), the interpreter can natively handle Unicode characters from U+000000 to U+110000, so the range of legal values for the <a class="reference internal" href="../library/functions.html#unichr" title="unichr"><tt class="xref py py-func docutils literal"><span class="pre">unichr()</span></tt></a> function is expanded accordingly. Using an interpreter compiled to use UCS-2 (a “narrow Python”), values greater than 65535 will still cause <a class="reference internal" href="../library/functions.html#unichr" title="unichr"><tt class="xref py py-func docutils literal"><span class="pre">unichr()</span></tt></a> to raise a <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> exception. This is all described in <span class="target" id="index-14"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0261"><strong>PEP 261</strong></a>, “Support for ‘wide’ Unicode characters”; consult it for further details.</p> <p>Another change is simpler to explain. Since their introduction, Unicode strings have supported an <tt class="xref py py-meth docutils literal"><span class="pre">encode()</span></tt> method to convert the string to a selected encoding such as UTF-8 or Latin-1. A symmetric <tt class="xref py py-meth docutils literal"><span class="pre">decode([*encoding*])()</span></tt> method has been added to 8-bit strings (though not to Unicode strings) in 2.2. <tt class="xref py py-meth docutils literal"><span class="pre">decode()</span></tt> assumes that the string is in the specified encoding and decodes it, returning whatever is returned by the codec.</p> <p>Using this new feature, codecs have been added for tasks not directly related to Unicode. For example, codecs have been added for uu-encoding, MIME’s base64 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."><tt class="xref py py-mod docutils literal"><span class="pre">zlib</span></tt></a> module:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s">"""Here is a lengthy piece of redundant, overly verbose,</span> <span class="gp">... </span><span class="s">and repetitive text.</span> <span class="gp">... </span><span class="s">"""</span> <span class="gp">>>> </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="s">'zlib'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">data</span> <span class="go">'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...'</span> <span class="gp">>>> </span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'zlib'</span><span class="p">)</span> <span class="go">'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n'</span> <span class="gp">>>> </span><span class="k">print</span> <span class="n">s</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'uu'</span><span class="p">)</span> <span class="go">begin 666 <data></span> <span class="go">M2&5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=6YD86YT+"!O=F5R;'D@</span> <span class="go">>=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X*</span> <span class="go">end</span> <span class="gp">>>> </span><span class="s">"sheesh"</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'rot-13'</span><span class="p">)</span> <span class="go">'furrfu'</span> </pre></div> </div> <p>To convert a class instance to Unicode, a <a class="reference internal" href="../reference/datamodel.html#object.__unicode__" title="object.__unicode__"><tt class="xref py py-meth docutils literal"><span class="pre">__unicode__()</span></tt></a> method can be defined by a class, analogous to <a class="reference internal" href="../reference/datamodel.html#object.__str__" title="object.__str__"><tt class="xref py py-meth docutils literal"><span class="pre">__str__()</span></tt></a>.</p> <p><tt class="xref py py-meth docutils literal"><span class="pre">encode()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">decode()</span></tt>, and <a class="reference internal" href="../reference/datamodel.html#object.__unicode__" title="object.__unicode__"><tt class="xref py py-meth docutils literal"><span class="pre">__unicode__()</span></tt></a> were implemented by Marc-André Lemburg. The changes to support using UCS-4 internally were implemented by Fredrik Lundh and Martin von Löwis.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><span class="target" id="index-15"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0261"><strong>PEP 261</strong></a> - Support for ‘wide’ Unicode characters</dt> <dd>Written by Paul Prescod.</dd> </dl> </div> </div> <div class="section" id="pep-227-nested-scopes"> <h2>PEP 227: Nested Scopes<a class="headerlink" href="#pep-227-nested-scopes" title="Permalink to this headline">¶</a></h2> <p>In Python 2.1, statically nested scopes were added as an optional feature, to be enabled by a <tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">nested_scopes</span></tt> directive. In 2.2 nested scopes no longer need to be specially enabled, and are now always present. The rest of this section is a copy of the description of nested scopes from my “What’s New in Python 2.1” document; if you read it when 2.1 came out, you can skip the rest of this section.</p> <p>The largest change introduced in Python 2.1, and made complete in 2.2, is to Python’s scoping rules. In Python 2.0, at any given time there are at most three namespaces used to look up variable names: local, module-level, and the built-in namespace. This often surprised people because it didn’t match their intuitive expectations. For example, a nested recursive function definition doesn’t work:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="o">...</span> <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">value</span><span class="p">):</span> <span class="o">...</span> <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> <span class="o">...</span> </pre></div> </div> <p>The function <tt class="xref py py-func docutils literal"><span class="pre">g()</span></tt> will always raise a <a class="reference internal" href="../library/exceptions.html#exceptions.NameError" title="exceptions.NameError"><tt class="xref py py-exc docutils literal"><span class="pre">NameError</span></tt></a> exception, because the binding of the name <tt class="docutils literal"><span class="pre">g</span></tt> isn’t in either its local namespace or in the module-level namespace. This isn’t much of a problem in practice (how often do you recursively define interior functions like this?), but this also made using the <a class="reference internal" href="../reference/expressions.html#lambda"><tt class="xref std std-keyword docutils literal"><span class="pre">lambda</span></tt></a> statement clumsier, and this was a problem in practice. In code which uses <a class="reference internal" href="../reference/expressions.html#lambda"><tt class="xref std std-keyword docutils literal"><span class="pre">lambda</span></tt></a> you can often find local variables being copied by passing them as the default values of arguments.</p> <div class="highlight-python"><div class="highlight"><pre><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> <span class="s">"Return list of any entries equal to 'name'"</span> <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> <span class="bp">self</span><span class="o">.</span><span class="n">list_attribute</span><span class="p">)</span> <span class="k">return</span> <span class="n">L</span> </pre></div> </div> <p>The readability of Python code written in a strongly functional style suffers greatly as a result.</p> <p>The most significant change to Python 2.2 is that static scoping has been added to the language to fix this problem. As a first effect, the <tt class="docutils literal"><span class="pre">name=name</span></tt> default argument is now unnecessary in the above example. Put simply, when a given variable name is not assigned a value within a function (by an assignment, or the <a class="reference internal" href="../reference/compound_stmts.html#def"><tt class="xref std std-keyword docutils literal"><span class="pre">def</span></tt></a>, <a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a>, or <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statements), references to the variable will be looked up in the local namespace of the enclosing scope. A more detailed explanation of the rules, and a dissection of the implementation, can be found in the PEP.</p> <p>This change may cause some compatibility problems for code where the same variable name is used both at the module level and as a local variable within a function that contains further function definitions. This seems rather unlikely though, since such code would have been pretty confusing to read in the first place.</p> <p>One side effect of the change is that the <tt class="docutils literal"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></tt> and <a class="reference internal" href="../reference/simple_stmts.html#exec"><tt class="xref std std-keyword docutils literal"><span class="pre">exec</span></tt></a> statements have been made illegal inside a function scope under certain conditions. The Python reference manual has said all along that <tt class="docutils literal"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></tt> is only legal at the top level of a module, but the CPython interpreter has never enforced this before. As part of the implementation of nested scopes, the compiler which turns Python source into bytecodes has to generate different code to access variables in a containing scope. <tt class="docutils literal"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></tt> and <a class="reference internal" href="../reference/simple_stmts.html#exec"><tt class="xref std std-keyword docutils literal"><span class="pre">exec</span></tt></a> make it impossible for the compiler to figure this out, because they add names to the local namespace that are unknowable at compile time. Therefore, if a function contains function definitions or <a class="reference internal" href="../reference/expressions.html#lambda"><tt class="xref std std-keyword docutils literal"><span class="pre">lambda</span></tt></a> expressions with free variables, the compiler will flag this by raising a <a class="reference internal" href="../library/exceptions.html#exceptions.SyntaxError" title="exceptions.SyntaxError"><tt class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></tt></a> exception.</p> <p>To make the preceding explanation a bit clearer, here’s an example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="c"># The next line is a syntax error</span> <span class="k">exec</span> <span class="s">'x=2'</span> <span class="k">def</span> <span class="nf">g</span><span class="p">():</span> <span class="k">return</span> <span class="n">x</span> </pre></div> </div> <p>Line 4 containing the <a class="reference internal" href="../reference/simple_stmts.html#exec"><tt class="xref std std-keyword docutils literal"><span class="pre">exec</span></tt></a> statement is a syntax error, since <a class="reference internal" href="../reference/simple_stmts.html#exec"><tt class="xref std std-keyword docutils literal"><span class="pre">exec</span></tt></a> would define a new local variable named <tt class="docutils literal"><span class="pre">x</span></tt> whose value should be accessed by <tt class="xref py py-func docutils literal"><span class="pre">g()</span></tt>.</p> <p>This shouldn’t be much of a limitation, since <a class="reference internal" href="../reference/simple_stmts.html#exec"><tt class="xref std std-keyword docutils literal"><span class="pre">exec</span></tt></a> is rarely used in most Python code (and when it is used, it’s often a sign of a poor design anyway).</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><span class="target" id="index-16"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0227"><strong>PEP 227</strong></a> - Statically Nested Scopes</dt> <dd>Written and implemented by Jeremy Hylton.</dd> </dl> </div> </div> <div class="section" id="new-and-improved-modules"> <h2>New and Improved Modules<a class="headerlink" href="#new-and-improved-modules" title="Permalink to this headline">¶</a></h2> <ul> <li><p class="first">The <a class="reference internal" href="../library/xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><tt class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></tt></a> module was contributed to the standard library by Fredrik Lundh, providing support for writing XML-RPC clients. XML-RPC is a simple remote procedure call protocol built on top of HTTP and XML. For example, the following snippet retrieves a list of RSS channels from the O’Reilly Network, and then lists the recent headlines for one channel:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">xmlrpclib</span> <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> <span class="s">'http://www.oreillynet.com/meerkat/xml-rpc/server.php'</span><span class="p">)</span> <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> <span class="c"># channels is a list of dictionaries, like this:</span> <span class="c"># [{'id': 4, 'title': 'Freshmeat Daily News'}</span> <span class="c"># {'id': 190, 'title': '32Bits Online'},</span> <span class="c"># {'id': 4549, 'title': '3DGamers'}, ... ]</span> <span class="c"># Get the items for one channel</span> <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="s">'channel'</span><span class="p">:</span> <span class="mi">4</span><span class="p">}</span> <span class="p">)</span> <span class="c"># 'items' is another list of dictionaries, like this:</span> <span class="c"># [{'link': 'http://freshmeat.net/releases/52719/',</span> <span class="c"># 'description': 'A utility which converts HTML to XSL FO.',</span> <span class="c"># 'title': 'html2fo 0.3 (Default)'}, ... ]</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/simplexmlrpcserver.html#module-SimpleXMLRPCServer" title="SimpleXMLRPCServer: Basic XML-RPC server implementation."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleXMLRPCServer</span></tt></a> module makes it easy to create straightforward XML-RPC servers. See <a class="reference external" href="http://www.xmlrpc.com/">http://www.xmlrpc.com/</a> for more information about XML-RPC.</p> </li> <li><p class="first">The new <a class="reference internal" href="../library/hmac.html#module-hmac" title="hmac: Keyed-Hashing for Message Authentication (HMAC) implementation for Python."><tt class="xref py py-mod docutils literal"><span class="pre">hmac</span></tt></a> module implements the HMAC algorithm described by <span class="target" id="index-17"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2104.html"><strong>RFC 2104</strong></a>. (Contributed by Gerhard Häring.)</p> </li> <li><p class="first">Several functions that originally returned lengthy tuples now return pseudo- sequences that still behave like tuples but also have mnemonic attributes such as memberst_mtime or <tt class="xref py py-attr docutils literal"><span class="pre">tm_year</span></tt>. The enhanced functions include <a class="reference internal" href="../library/stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><tt class="xref py py-func docutils literal"><span class="pre">stat()</span></tt></a>, <tt class="xref py py-func docutils literal"><span class="pre">fstat()</span></tt>, <a class="reference internal" href="../library/statvfs.html#module-statvfs" title="statvfs: Constants for interpreting the result of os.statvfs(). (deprecated)"><tt class="xref py py-func docutils literal"><span class="pre">statvfs()</span></tt></a>, and <tt class="xref py py-func docutils literal"><span class="pre">fstatvfs()</span></tt> in the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><tt class="xref py py-mod docutils literal"><span class="pre">os</span></tt></a> module, and <tt class="xref py py-func docutils literal"><span class="pre">localtime()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">gmtime()</span></tt>, and <tt class="xref py py-func docutils literal"><span class="pre">strptime()</span></tt> in the <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a> module.</p> <p>For example, to obtain a file’s size using the old tuples, you’d end up writing something like <tt class="docutils literal"><span class="pre">file_size</span> <span class="pre">=</span> <span class="pre">os.stat(filename)[stat.ST_SIZE]</span></tt>, but now this can be written more clearly as <tt class="docutils literal"><span class="pre">file_size</span> <span class="pre">=</span> <span class="pre">os.stat(filename).st_size</span></tt>.</p> <p>The original patch for this feature was contributed by Nick Mathewson.</p> </li> <li><p class="first">The Python profiler has been extensively reworked and various errors in its output have been corrected. (Contributed by Fred L. Drake, Jr. and Tim Peters.)</p> </li> <li><p class="first">The <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a> module can be compiled to support IPv6; specify the <em class="xref std std-option">--enable-ipv6</em> option to Python’s configure script. (Contributed by Jun-ichiro “itojun” Hagino.)</p> </li> <li><p class="first">Two new format characters were added to the <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret strings as packed binary data."><tt class="xref py py-mod docutils literal"><span class="pre">struct</span></tt></a> module for 64-bit integers on platforms that support the C <tt class="xref c c-type docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt> type. <tt class="docutils literal"><span class="pre">q</span></tt> is for a signed 64-bit integer, and <tt class="docutils literal"><span class="pre">Q</span></tt> is for an unsigned one. The value is returned in Python’s long integer type. (Contributed by Tim Peters.)</p> </li> <li><p class="first">In the interpreter’s interactive mode, there’s a new built-in function <a class="reference internal" href="../library/functions.html#help" title="help"><tt class="xref py py-func docutils literal"><span class="pre">help()</span></tt></a> that uses the <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><tt class="xref py py-mod docutils literal"><span class="pre">pydoc</span></tt></a> module introduced in Python 2.1 to provide interactive help. <tt class="docutils literal"><span class="pre">help(object)</span></tt> displays any available help text about <em>object</em>. <a class="reference internal" href="../library/functions.html#help" title="help"><tt class="xref py py-func docutils literal"><span class="pre">help()</span></tt></a> with no argument puts you in an online help utility, where you can enter the names of functions, classes, or modules to read their help text. (Contributed by Guido van Rossum, using Ka-Ping Yee’s <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><tt class="xref py py-mod docutils literal"><span class="pre">pydoc</span></tt></a> module.)</p> </li> <li><p class="first">Various bugfixes and performance improvements have been made to the SRE engine underlying the <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a> module. For example, the <a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><tt class="xref py py-func docutils literal"><span class="pre">re.sub()</span></tt></a> and <a class="reference internal" href="../library/re.html#re.split" title="re.split"><tt class="xref py py-func docutils literal"><span class="pre">re.split()</span></tt></a> functions have been rewritten in C. Another contributed patch speeds up certain Unicode character ranges by a factor of two, and a new <tt class="xref py py-meth docutils literal"><span class="pre">finditer()</span></tt> method that returns an iterator over all the non-overlapping matches in a given string. (SRE is maintained by Fredrik Lundh. The BIGCHARSET patch was contributed by Martin von Löwis.)</p> </li> <li><p class="first">The <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">smtplib</span></tt></a> module now supports <span class="target" id="index-18"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2487.html"><strong>RFC 2487</strong></a>, “Secure SMTP over TLS”, so it’s now possible to encrypt the SMTP traffic between a Python program and the mail transport agent being handed a message. <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">smtplib</span></tt></a> also supports SMTP authentication. (Contributed by Gerhard Häring.)</p> </li> <li><p class="first">The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">imaplib</span></tt></a> module, maintained by Piers Lauder, has support for several new extensions: the NAMESPACE extension defined in <span class="target" id="index-19"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2342.html"><strong>RFC 2342</strong></a>, SORT, GETACL and SETACL. (Contributed by Anthony Baxter and Michel Pelletier.)</p> </li> <li><p class="first">The <a class="reference internal" href="../library/rfc822.html#module-rfc822" title="rfc822: Parse 2822 style mail messages. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">rfc822</span></tt></a> module’s parsing of email addresses is now compliant with <span class="target" id="index-20"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a>, an update to <span class="target" id="index-21"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a>. (The module’s name is <em>not</em> going to be changed to <tt class="docutils literal"><span class="pre">rfc2822</span></tt>.) A new package, <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages, including MIME documents."><tt class="xref py py-mod docutils literal"><span class="pre">email</span></tt></a>, has also been added for parsing and generating e-mail messages. (Contributed by Barry Warsaw, and arising out of his work on Mailman.)</p> </li> <li><p class="first">The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><tt class="xref py py-mod docutils literal"><span class="pre">difflib</span></tt></a> module now contains a new <tt class="xref py py-class docutils literal"><span class="pre">Differ</span></tt> class for producing human-readable lists of changes (a “delta”) between two sequences of lines of text. There are also two generator functions, <tt class="xref py py-func docutils literal"><span class="pre">ndiff()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">restore()</span></tt>, which respectively return a delta from two sequences, or one of the original sequences from a delta. (Grunt work contributed by David Goodger, from ndiff.py code by Tim Peters who then did the generatorization.)</p> </li> <li><p class="first">New constants <tt class="xref py py-const docutils literal"><span class="pre">ascii_letters</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ascii_lowercase</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">ascii_uppercase</span></tt> were added to the <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><tt class="xref py py-mod docutils literal"><span class="pre">string</span></tt></a> module. There were several modules in the standard library that used <a class="reference internal" href="../library/string.html#string.letters" title="string.letters"><tt class="xref py py-const docutils literal"><span class="pre">string.letters</span></tt></a> to mean the ranges A-Za-z, but that assumption is incorrect when locales are in use, because <a class="reference internal" href="../library/string.html#string.letters" title="string.letters"><tt class="xref py py-const docutils literal"><span class="pre">string.letters</span></tt></a> varies depending on the set of legal characters defined by the current locale. The buggy modules have all been fixed to use <tt class="xref py py-const docutils literal"><span class="pre">ascii_letters</span></tt> instead. (Reported by an unknown person; fixed by Fred L. Drake, Jr.)</p> </li> <li><p class="first">The <a class="reference internal" href="../library/mimetypes.html#module-mimetypes" title="mimetypes: Mapping of filename extensions to MIME types."><tt class="xref py py-mod docutils literal"><span class="pre">mimetypes</span></tt></a> module now makes it easier to use alternative MIME-type databases by the addition of a <tt class="xref py py-class docutils literal"><span class="pre">MimeTypes</span></tt> class, which takes a list of filenames to be parsed. (Contributed by Fred L. Drake, Jr.)</p> </li> <li><p class="first">A <tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt> class was added to the <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module that allows scheduling an activity to happen at some future time. (Contributed by Itamar Shtull-Trauring.)</p> </li> </ul> </div> <div class="section" id="interpreter-changes-and-fixes"> <h2>Interpreter Changes and Fixes<a class="headerlink" href="#interpreter-changes-and-fixes" title="Permalink to this headline">¶</a></h2> <p>Some of the changes only affect people who deal with the Python interpreter at the C level because they’re writing Python extension modules, embedding the interpreter, or just hacking on the interpreter itself. If you only write Python code, none of the changes described here will affect you very much.</p> <ul> <li><p class="first">Profiling and tracing functions can now be implemented in C, which can operate at much higher speeds than Python-based functions and should reduce the overhead of profiling and tracing. This will be of interest to authors of development environments for Python. Two new C functions were added to Python’s API, <a class="reference internal" href="../c-api/init.html#PyEval_SetProfile" title="PyEval_SetProfile"><tt class="xref c c-func docutils literal"><span class="pre">PyEval_SetProfile()</span></tt></a> and <a class="reference internal" href="../c-api/init.html#PyEval_SetTrace" title="PyEval_SetTrace"><tt class="xref c c-func docutils literal"><span class="pre">PyEval_SetTrace()</span></tt></a>. The existing <a class="reference internal" href="../library/sys.html#sys.setprofile" title="sys.setprofile"><tt class="xref py py-func docutils literal"><span class="pre">sys.setprofile()</span></tt></a> and <a class="reference internal" href="../library/sys.html#sys.settrace" title="sys.settrace"><tt class="xref py py-func docutils literal"><span class="pre">sys.settrace()</span></tt></a> functions still exist, and have simply been changed to use the new C-level interface. (Contributed by Fred L. Drake, Jr.)</p> </li> <li><p class="first">Another low-level API, primarily of interest to implementors of Python debuggers and development tools, was added. <a class="reference internal" href="../c-api/init.html#PyInterpreterState_Head" title="PyInterpreterState_Head"><tt class="xref c c-func docutils literal"><span class="pre">PyInterpreterState_Head()</span></tt></a> and <a class="reference internal" href="../c-api/init.html#PyInterpreterState_Next" title="PyInterpreterState_Next"><tt class="xref c c-func docutils literal"><span class="pre">PyInterpreterState_Next()</span></tt></a> let a caller walk through all the existing interpreter objects; <a class="reference internal" href="../c-api/init.html#PyInterpreterState_ThreadHead" title="PyInterpreterState_ThreadHead"><tt class="xref c c-func docutils literal"><span class="pre">PyInterpreterState_ThreadHead()</span></tt></a> and <a class="reference internal" href="../c-api/init.html#PyThreadState_Next" title="PyThreadState_Next"><tt class="xref c c-func docutils literal"><span class="pre">PyThreadState_Next()</span></tt></a> allow looping over all the thread states for a given interpreter. (Contributed by David Beazley.)</p> </li> <li><p class="first">The C-level interface to the garbage collector has been changed to make it easier to write extension types that support garbage collection and to debug misuses of the functions. Various functions have slightly different semantics, so a bunch of functions had to be renamed. Extensions that use the old API will still compile but will <em>not</em> participate in garbage collection, so updating them for 2.2 should be considered fairly high priority.</p> <p>To upgrade an extension module to the new API, perform the following steps:</p> </li> <li><p class="first">Rename <tt class="xref c c-func docutils literal"><span class="pre">Py_TPFLAGS_GC()</span></tt> to <tt class="xref c c-func docutils literal"><span class="pre">PyTPFLAGS_HAVE_GC()</span></tt>.</p> </li> <li><dl class="first docutils"> <dt>Use <a class="reference internal" href="../c-api/gcsupport.html#PyObject_GC_New" title="PyObject_GC_New"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GC_New()</span></tt></a> or <a class="reference internal" href="../c-api/gcsupport.html#PyObject_GC_NewVar" title="PyObject_GC_NewVar"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GC_NewVar()</span></tt></a> to allocate</dt> <dd><p class="first last">objects, and <a class="reference internal" href="../c-api/gcsupport.html#PyObject_GC_Del" title="PyObject_GC_Del"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GC_Del()</span></tt></a> to deallocate them.</p> </dd> </dl> </li> <li><dl class="first docutils"> <dt>Rename <tt class="xref c c-func docutils literal"><span class="pre">PyObject_GC_Init()</span></tt> to <a class="reference internal" href="../c-api/gcsupport.html#PyObject_GC_Track" title="PyObject_GC_Track"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GC_Track()</span></tt></a> and</dt> <dd><p class="first last"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GC_Fini()</span></tt> to <a class="reference internal" href="../c-api/gcsupport.html#PyObject_GC_UnTrack" title="PyObject_GC_UnTrack"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GC_UnTrack()</span></tt></a>.</p> </dd> </dl> </li> <li><p class="first">Remove <tt class="xref c c-func docutils literal"><span class="pre">PyGC_HEAD_SIZE()</span></tt> from object size calculations.</p> </li> <li><p class="first">Remove calls to <tt class="xref c c-func docutils literal"><span class="pre">PyObject_AS_GC()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">PyObject_FROM_GC()</span></tt>.</p> </li> <li><p class="first">A new <tt class="docutils literal"><span class="pre">et</span></tt> format sequence was added to <a class="reference internal" href="../c-api/arg.html#PyArg_ParseTuple" title="PyArg_ParseTuple"><tt class="xref c c-func docutils literal"><span class="pre">PyArg_ParseTuple()</span></tt></a>; <tt class="docutils literal"><span class="pre">et</span></tt> takes both a parameter and an encoding name, and converts the parameter to the given encoding if the parameter turns out to be a Unicode string, or leaves it alone if it’s an 8-bit string, assuming it to already be in the desired encoding. This differs from the <tt class="docutils literal"><span class="pre">es</span></tt> format character, which assumes that 8-bit strings are in Python’s default ASCII encoding and converts them to the specified new encoding. (Contributed by M.-A. Lemburg, and used for the MBCS support on Windows described in the following section.)</p> </li> <li><p class="first">A different argument parsing function, <a class="reference internal" href="../c-api/arg.html#PyArg_UnpackTuple" title="PyArg_UnpackTuple"><tt class="xref c c-func docutils literal"><span class="pre">PyArg_UnpackTuple()</span></tt></a>, has been added that’s simpler and presumably faster. Instead of specifying a format string, the caller simply gives the minimum and maximum number of arguments expected, and a set of pointers to <a class="reference internal" href="../c-api/structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a> variables that will be filled in with argument values.</p> </li> <li><p class="first">Two new flags <a class="reference internal" href="../c-api/structures.html#METH_NOARGS" title="METH_NOARGS"><tt class="xref py py-const docutils literal"><span class="pre">METH_NOARGS</span></tt></a> and <a class="reference internal" href="../c-api/structures.html#METH_O" title="METH_O"><tt class="xref py py-const docutils literal"><span class="pre">METH_O</span></tt></a> are available in method definition tables to simplify implementation of methods with no arguments or a single untyped argument. Calling such methods is more efficient than calling a corresponding method that uses <a class="reference internal" href="../c-api/structures.html#METH_VARARGS" title="METH_VARARGS"><tt class="xref py py-const docutils literal"><span class="pre">METH_VARARGS</span></tt></a>. Also, the old <a class="reference internal" href="../c-api/structures.html#METH_OLDARGS" title="METH_OLDARGS"><tt class="xref py py-const docutils literal"><span class="pre">METH_OLDARGS</span></tt></a> style of writing C methods is now officially deprecated.</p> </li> <li><p class="first">Two new wrapper functions, <a class="reference internal" href="../c-api/conversion.html#PyOS_snprintf" title="PyOS_snprintf"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_snprintf()</span></tt></a> and <a class="reference internal" href="../c-api/conversion.html#PyOS_vsnprintf" title="PyOS_vsnprintf"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_vsnprintf()</span></tt></a> were added to provide cross-platform implementations for the relatively new <tt class="xref c c-func docutils literal"><span class="pre">snprintf()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">vsnprintf()</span></tt> C lib APIs. In contrast to the standard <tt class="xref c c-func docutils literal"><span class="pre">sprintf()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">vsprintf()</span></tt> functions, the Python versions check the bounds of the buffer used to protect against buffer overruns. (Contributed by M.-A. Lemburg.)</p> </li> <li><p class="first">The <a class="reference internal" href="../c-api/tuple.html#_PyTuple_Resize" title="_PyTuple_Resize"><tt class="xref c c-func docutils literal"><span class="pre">_PyTuple_Resize()</span></tt></a> function has lost an unused parameter, so now it takes 2 parameters instead of 3. The third argument was never used, and can simply be discarded when porting code from earlier versions to Python 2.2.</p> </li> </ul> </div> <div class="section" id="other-changes-and-fixes"> <h2>Other Changes and Fixes<a class="headerlink" href="#other-changes-and-fixes" title="Permalink to this headline">¶</a></h2> <p>As usual there were a bunch of other improvements and bugfixes scattered throughout the source tree. A search through the CVS change logs finds there were 527 patches applied and 683 bugs fixed between Python 2.1 and 2.2; 2.2.1 applied 139 patches and fixed 143 bugs; 2.2.2 applied 106 patches and fixed 82 bugs. These figures are likely to be underestimates.</p> <p>Some of the more notable changes are:</p> <ul> <li><p class="first">The code for the MacOS port for Python, maintained by Jack Jansen, is now kept in the main Python CVS tree, and many changes have been made to support MacOS X.</p> <p>The most significant change is the ability to build Python as a framework, enabled by supplying the <em class="xref std std-option">--enable-framework</em> option to the configure script when compiling Python. According to Jack Jansen, “This installs a self- contained Python installation plus the OS X framework “glue” into <tt class="file docutils literal"><span class="pre">/Library/Frameworks/Python.framework</span></tt> (or another location of choice). For now there is little immediate added benefit to this (actually, there is the disadvantage that you have to change your PATH to be able to find Python), but it is the basis for creating a full-blown Python application, porting the MacPython IDE, possibly using Python as a standard OSA scripting language and much more.”</p> <p>Most of the MacPython toolbox modules, which interface to MacOS APIs such as windowing, QuickTime, scripting, etc. have been ported to OS X, but they’ve been left commented out in <tt class="file docutils literal"><span class="pre">setup.py</span></tt>. People who want to experiment with these modules can uncomment them manually.</p> </li> <li><p class="first">Keyword arguments passed to built-in functions that don’t take them now cause a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception to be raised, with the message “<em>function</em> takes no keyword arguments”.</p> </li> <li><p class="first">Weak references, added in Python 2.1 as an extension module, are now part of the core because they’re used in the implementation of new-style classes. The <a class="reference internal" href="../library/exceptions.html#exceptions.ReferenceError" title="exceptions.ReferenceError"><tt class="xref py py-exc docutils literal"><span class="pre">ReferenceError</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">weakref</span></tt></a> module to become a built-in exception.</p> </li> <li><p class="first">A new script, <tt class="file docutils literal"><span class="pre">Tools/scripts/cleanfuture.py</span></tt> by Tim Peters, automatically removes obsolete <tt class="docutils literal"><span class="pre">__future__</span></tt> statements from Python source code.</p> </li> <li><p class="first">An additional <em>flags</em> argument has been added to the built-in function <a class="reference internal" href="../library/functions.html#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a>, so the behaviour of <tt class="docutils literal"><span class="pre">__future__</span></tt> statements can now be correctly observed in simulated shells, such as those presented by IDLE and other development environments. This is described in <span class="target" id="index-22"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0264"><strong>PEP 264</strong></a>. (Contributed by Michael Hudson.)</p> </li> <li><p class="first">The new license introduced with Python 1.6 wasn’t GPL-compatible. This is fixed by some minor textual changes to the 2.2 license, so it’s now legal to embed Python inside a GPLed program again. Note that Python itself is not GPLed, but instead is under a license that’s essentially equivalent to the BSD license, same as it always was. The license changes were also applied to the Python 2.0.1 and 2.1.1 releases.</p> </li> <li><p class="first">When presented with a Unicode filename on Windows, Python will now convert it to an MBCS encoded string, as used by the Microsoft file APIs. As MBCS is explicitly used by the file APIs, Python’s choice of ASCII as the default encoding turns out to be an annoyance. On Unix, the locale’s character set is used if <tt class="xref py py-func docutils literal"><span class="pre">locale.nl_langinfo(CODESET)()</span></tt> is available. (Windows support was contributed by Mark Hammond with assistance from Marc-André Lemburg. Unix support was added by Martin von Löwis.)</p> </li> <li><p class="first">Large file support is now enabled on Windows. (Contributed by Tim Peters.)</p> </li> <li><p class="first">The <tt class="file docutils literal"><span class="pre">Tools/scripts/ftpmirror.py</span></tt> script now parses a <tt class="file docutils literal"><span class="pre">.netrc</span></tt> file, if you have one. (Contributed by Mike Romberg.)</p> </li> <li><p class="first">Some features of the object returned by the <a class="reference internal" href="../library/functions.html#xrange" title="xrange"><tt class="xref py py-func docutils literal"><span class="pre">xrange()</span></tt></a> function are now deprecated, and trigger warnings when they’re accessed; they’ll disappear in Python 2.3. <a class="reference internal" href="../library/functions.html#xrange" title="xrange"><tt class="xref py py-class docutils literal"><span class="pre">xrange</span></tt></a> objects tried to pretend they were full sequence types by supporting slicing, sequence multiplication, and the <a class="reference internal" href="../reference/expressions.html#in"><tt class="xref std std-keyword docutils literal"><span class="pre">in</span></tt></a> operator, but these features were rarely used and therefore buggy. The <tt class="xref py py-meth docutils literal"><span class="pre">tolist()</span></tt> method and the <tt class="xref py py-attr docutils literal"><span class="pre">start</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">stop</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">step</span></tt> attributes are also being deprecated. At the C level, the fourth argument to the <tt class="xref c c-func docutils literal"><span class="pre">PyRange_New()</span></tt> function, <tt class="docutils literal"><span class="pre">repeat</span></tt>, has also been deprecated.</p> </li> <li><p class="first">There were a bunch of patches to the dictionary implementation, mostly to fix potential core dumps if a dictionary contains objects that sneakily changed their hash value, or mutated the dictionary they were contained in. For a while python-dev fell into a gentle rhythm of Michael Hudson finding a case that dumped core, Tim Peters fixing the bug, Michael finding another case, and round and round it went.</p> </li> <li><p class="first">On Windows, Python can now be compiled with Borland C thanks to a number of patches contributed by Stephen Hansen, though the result isn’t fully functional yet. (But this <em>is</em> progress...)</p> </li> <li><p class="first">Another Windows enhancement: Wise Solutions generously offered PythonLabs use of their InstallerMaster 8.1 system. Earlier PythonLabs Windows installers used Wise 5.0a, which was beginning to show its age. (Packaged up by Tim Peters.)</p> </li> <li><p class="first">Files ending in <tt class="docutils literal"><span class="pre">.pyw</span></tt> can now be imported on Windows. <tt class="docutils literal"><span class="pre">.pyw</span></tt> is a Windows-only thing, used to indicate that a script needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to prevent a DOS console from popping up to display the output. This patch makes it possible to import such scripts, in case they’re also usable as modules. (Implemented by David Bolen.)</p> </li> <li><p class="first">On platforms where Python uses the C <tt class="xref c c-func docutils literal"><span class="pre">dlopen()</span></tt> function to load extension modules, it’s now possible to set the flags used by <tt class="xref c c-func docutils literal"><span class="pre">dlopen()</span></tt> using the <a class="reference internal" href="../library/sys.html#sys.getdlopenflags" title="sys.getdlopenflags"><tt class="xref py py-func docutils literal"><span class="pre">sys.getdlopenflags()</span></tt></a> and <a class="reference internal" href="../library/sys.html#sys.setdlopenflags" title="sys.setdlopenflags"><tt class="xref py py-func docutils literal"><span class="pre">sys.setdlopenflags()</span></tt></a> functions. (Contributed by Bram Stolk.)</p> </li> <li><p class="first">The <a class="reference internal" href="../library/functions.html#pow" title="pow"><tt class="xref py py-func docutils literal"><span class="pre">pow()</span></tt></a> built-in function no longer supports 3 arguments when floating-point numbers are supplied. <tt class="docutils literal"><span class="pre">pow(x,</span> <span class="pre">y,</span> <span class="pre">z)</span></tt> returns <tt class="docutils literal"><span class="pre">(x**y)</span> <span class="pre">%</span> <span class="pre">z</span></tt>, but this is never useful for floating point numbers, and the final result varies unpredictably depending on the platform. A call such as <tt class="docutils literal"><span class="pre">pow(2.0,</span> <span class="pre">8.0,</span> <span class="pre">7.0)</span></tt> will now raise a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception.</p> </li> </ul> </div> <div class="section" id="acknowledgements"> <h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Permalink to this headline">¶</a></h2> <p>The author would like to thank the following people for offering suggestions, corrections and assistance with various drafts of this article: Fred Bremmer, Keith Briggs, Andrew Dalke, Fred L. Drake, Jr., Carel Fellinger, David Goodger, Mark Hammond, Stephen Hansen, Michael Hudson, Jack Jansen, Marc-André Lemburg, Martin von Löwis, Fredrik Lundh, Michael McLay, Nick Mathewson, Paul Moore, Gustavo Niemeyer, Don O’Donnell, Joonas Paalasma, Tim Peters, Jens Quade, Tom Reinhardt, Neil Schemenauer, Guido van Rossum, Greg Ward, Edward Welbourne.</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">What’s New in Python 2.2</a><ul> <li><a class="reference internal" href="#introduction">Introduction</a></li> <li><a class="reference internal" href="#peps-252-and-253-type-and-class-changes">PEPs 252 and 253: Type and Class Changes</a><ul> <li><a class="reference internal" href="#old-and-new-classes">Old and New Classes</a></li> <li><a class="reference internal" href="#descriptors">Descriptors</a></li> <li><a class="reference internal" href="#multiple-inheritance-the-diamond-rule">Multiple Inheritance: The Diamond Rule</a></li> <li><a class="reference internal" href="#attribute-access">Attribute Access</a></li> <li><a class="reference internal" href="#related-links">Related Links</a></li> </ul> </li> <li><a class="reference internal" href="#pep-234-iterators">PEP 234: Iterators</a></li> <li><a class="reference internal" href="#pep-255-simple-generators">PEP 255: Simple Generators</a></li> <li><a class="reference internal" href="#pep-237-unifying-long-integers-and-integers">PEP 237: Unifying Long Integers and Integers</a></li> <li><a class="reference internal" href="#pep-238-changing-the-division-operator">PEP 238: Changing the Division Operator</a></li> <li><a class="reference internal" href="#unicode-changes">Unicode Changes</a></li> <li><a class="reference internal" href="#pep-227-nested-scopes">PEP 227: Nested Scopes</a></li> <li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a></li> <li><a class="reference internal" href="#interpreter-changes-and-fixes">Interpreter Changes and Fixes</a></li> <li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li> <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="2.3.html" title="previous chapter">What’s New in Python 2.3</a></p> <h4>Next topic</h4> <p class="topless"><a href="2.1.html" title="next chapter">What’s New in Python 2.1</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/whatsnew/2.2.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="2.1.html" title="What’s New in Python 2.1" >next</a> |</li> <li class="right" > <a href="2.3.html" title="What’s New in Python 2.3" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >What’s New in Python</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close