| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="whatsnew24.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="whatsnew24.html" title='What's New in Python 2.4' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='last' href='about.html' title='About this document...' /> |
| 10 | <link rel='help' href='about.html' title='About this document...' /> |
| 11 | <link rel="next" href="node13.html" /> |
| 12 | <link rel="prev" href="node11.html" /> |
| 13 | <link rel="parent" href="whatsnew24.html" /> |
| 14 | <link rel="next" href="node13.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>11 Other Language Changes</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="10 PEP 331: Locale-Independent" |
| 24 | href="node11.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="What's New in Python" |
| 27 | href="whatsnew24.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="12 New, Improved, and" |
| 30 | href="node13.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">What's New in Python 2.4</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><img src='../icons/blank.png' |
| 37 | border='0' height='32' alt='' width='32' /></td> |
| 38 | <td class='online-navigation'><img src='../icons/blank.png' |
| 39 | border='0' height='32' alt='' width='32' /></td> |
| 40 | </tr></table> |
| 41 | <div class='online-navigation'> |
| 42 | <b class="navlabel">Previous:</b> |
| 43 | <a class="sectref" rel="prev" href="node11.html">10 PEP 331: Locale-Independent</A> |
| 44 | <b class="navlabel">Up:</b> |
| 45 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> |
| 46 | <b class="navlabel">Next:</b> |
| 47 | <a class="sectref" rel="next" href="node13.html">12 New, Improved, and</A> |
| 48 | </div> |
| 49 | <hr /></div> |
| 50 | </DIV> |
| 51 | <!--End of Navigation Panel--> |
| 52 | <div class='online-navigation'> |
| 53 | <!--Table of Child-Links--> |
| 54 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 55 | |
| 56 | <UL CLASS="ChildLinks"> |
| 57 | <LI><A href="node12.html#SECTION0001210000000000000000">11.1 Optimizations</a> |
| 58 | </ul> |
| 59 | <!--End of Table of Child-Links--> |
| 60 | </div> |
| 61 | <HR> |
| 62 | |
| 63 | <H1><A NAME="SECTION0001200000000000000000"> |
| 64 | 11 Other Language Changes</A> |
| 65 | </H1> |
| 66 | |
| 67 | <P> |
| 68 | Here are all of the changes that Python 2.4 makes to the core Python |
| 69 | language. |
| 70 | |
| 71 | <P> |
| 72 | |
| 73 | <UL> |
| 74 | <LI>Decorators for functions and methods were added (<a class="rfc" id='rfcref-1456' xml:id='rfcref-1456' |
| 75 | href="http://www.python.org/peps/pep-0318.html">PEP 318</a>). |
| 76 | |
| 77 | <P> |
| 78 | </LI> |
| 79 | <LI>Built-in <tt class="function">set</tt> and <tt class="function">frozenset</tt> types were |
| 80 | added (<a class="rfc" id='rfcref-1458' xml:id='rfcref-1458' |
| 81 | href="http://www.python.org/peps/pep-0218.html">PEP 218</a>). Other new built-ins include the <tt class="function">reversed(<var>seq</var>)</tt> function (<a class="rfc" id='rfcref-1460' xml:id='rfcref-1460' |
| 82 | href="http://www.python.org/peps/pep-0322.html">PEP 322</a>). |
| 83 | |
| 84 | <P> |
| 85 | </LI> |
| 86 | <LI>Generator expressions were added (<a class="rfc" id='rfcref-1462' xml:id='rfcref-1462' |
| 87 | href="http://www.python.org/peps/pep-0289.html">PEP 289</a>). |
| 88 | |
| 89 | <P> |
| 90 | </LI> |
| 91 | <LI>Certain numeric expressions no longer return values restricted to 32 or 64 bits (<a class="rfc" id='rfcref-1464' xml:id='rfcref-1464' |
| 92 | href="http://www.python.org/peps/pep-0237.html">PEP 237</a>). |
| 93 | |
| 94 | <P> |
| 95 | </LI> |
| 96 | <LI>You can now put parentheses around the list of names in a |
| 97 | <code>from <var>module</var> import <var>names</var></code> statement (<a class="rfc" id='rfcref-1466' xml:id='rfcref-1466' |
| 98 | href="http://www.python.org/peps/pep-0328.html">PEP 328</a>). |
| 99 | |
| 100 | <P> |
| 101 | </LI> |
| 102 | <LI>The <tt class="method">dict.update()</tt> method now accepts the same |
| 103 | argument forms as the <tt class="class">dict</tt> constructor. This includes any |
| 104 | mapping, any iterable of key/value pairs, and keyword arguments. |
| 105 | (Contributed by Raymond Hettinger.) |
| 106 | |
| 107 | <P> |
| 108 | </LI> |
| 109 | <LI>The string methods <tt class="method">ljust()</tt>, <tt class="method">rjust()</tt>, and |
| 110 | <tt class="method">center()</tt> now take an optional argument for specifying a |
| 111 | fill character other than a space. |
| 112 | (Contributed by Raymond Hettinger.) |
| 113 | |
| 114 | <P> |
| 115 | </LI> |
| 116 | <LI>Strings also gained an <tt class="method">rsplit()</tt> method that |
| 117 | works like the <tt class="method">split()</tt> method but splits from the end of |
| 118 | the string. |
| 119 | |
| 120 | <P> |
| 121 | <div class="verbatim"><pre> |
| 122 | >>> 'www.python.org'.split('.', 1) |
| 123 | ['www', 'python.org'] |
| 124 | 'www.python.org'.rsplit('.', 1) |
| 125 | ['www.python', 'org'] |
| 126 | </pre></div> |
| 127 | |
| 128 | <P> |
| 129 | </LI> |
| 130 | <LI>Three keyword parameters, <var>cmp</var>, <var>key</var>, and |
| 131 | <var>reverse</var>, were added to the <tt class="method">sort()</tt> method of lists. |
| 132 | These parameters make some common usages of <tt class="method">sort()</tt> simpler. |
| 133 | All of these parameters are optional. |
| 134 | |
| 135 | <P> |
| 136 | For the <var>cmp</var> parameter, the value should be a comparison function |
| 137 | that takes two parameters and returns -1, 0, or +1 depending on how |
| 138 | the parameters compare. This function will then be used to sort the |
| 139 | list. Previously this was the only parameter that could be provided |
| 140 | to <tt class="method">sort()</tt>. |
| 141 | |
| 142 | <P> |
| 143 | <var>key</var> should be a single-parameter function that takes a list |
| 144 | element and returns a comparison key for the element. The list is |
| 145 | then sorted using the comparison keys. The following example sorts a |
| 146 | list case-insensitively: |
| 147 | |
| 148 | <P> |
| 149 | <div class="verbatim"><pre> |
| 150 | >>> L = ['A', 'b', 'c', 'D'] |
| 151 | >>> L.sort() # Case-sensitive sort |
| 152 | >>> L |
| 153 | ['A', 'D', 'b', 'c'] |
| 154 | >>> # Using 'key' parameter to sort list |
| 155 | >>> L.sort(key=lambda x: x.lower()) |
| 156 | >>> L |
| 157 | ['A', 'b', 'c', 'D'] |
| 158 | >>> # Old-fashioned way |
| 159 | >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) |
| 160 | >>> L |
| 161 | ['A', 'b', 'c', 'D'] |
| 162 | </pre></div> |
| 163 | |
| 164 | <P> |
| 165 | The last example, which uses the <var>cmp</var> parameter, is the old way |
| 166 | to perform a case-insensitive sort. It works but is slower than using |
| 167 | a <var>key</var> parameter. Using <var>key</var> calls <tt class="method">lower()</tt> method |
| 168 | once for each element in the list while using <var>cmp</var> will call it |
| 169 | twice for each comparison, so using <var>key</var> saves on invocations of |
| 170 | the <tt class="method">lower()</tt> method. |
| 171 | |
| 172 | <P> |
| 173 | For simple key functions and comparison functions, it is often |
| 174 | possible to avoid a <tt class="keyword">lambda</tt> expression by using an unbound |
| 175 | method instead. For example, the above case-insensitive sort is best |
| 176 | written as: |
| 177 | |
| 178 | <P> |
| 179 | <div class="verbatim"><pre> |
| 180 | >>> L.sort(key=str.lower) |
| 181 | >>> L |
| 182 | ['A', 'b', 'c', 'D'] |
| 183 | </pre></div> |
| 184 | |
| 185 | <P> |
| 186 | Finally, the <var>reverse</var> parameter takes a Boolean value. If the |
| 187 | value is true, the list will be sorted into reverse order. |
| 188 | Instead of <code>L.sort() ; L.reverse()</code>, you can now write |
| 189 | <code>L.sort(reverse=True)</code>. |
| 190 | |
| 191 | <P> |
| 192 | The results of sorting are now guaranteed to be stable. This means |
| 193 | that two entries with equal keys will be returned in the same order as |
| 194 | they were input. For example, you can sort a list of people by name, |
| 195 | and then sort the list by age, resulting in a list sorted by age where |
| 196 | people with the same age are in name-sorted order. |
| 197 | |
| 198 | <P> |
| 199 | (All changes to <tt class="method">sort()</tt> contributed by Raymond Hettinger.) |
| 200 | |
| 201 | <P> |
| 202 | </LI> |
| 203 | <LI>There is a new built-in function |
| 204 | <tt class="function">sorted(<var>iterable</var>)</tt> that works like the in-place |
| 205 | <tt class="method">list.sort()</tt> method but can be used in |
| 206 | expressions. The differences are: |
| 207 | |
| 208 | <UL> |
| 209 | <LI>the input may be any iterable; |
| 210 | </LI> |
| 211 | <LI>a newly formed copy is sorted, leaving the original intact; and |
| 212 | </LI> |
| 213 | <LI>the expression returns the new sorted copy |
| 214 | |
| 215 | </LI> |
| 216 | </UL> |
| 217 | |
| 218 | <P> |
| 219 | <div class="verbatim"><pre> |
| 220 | >>> L = [9,7,8,3,2,4,1,6,5] |
| 221 | >>> [10+i for i in sorted(L)] # usable in a list comprehension |
| 222 | [11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 223 | >>> L # original is left unchanged |
| 224 | [9,7,8,3,2,4,1,6,5] |
| 225 | >>> sorted('Monty Python') # any iterable may be an input |
| 226 | [' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y'] |
| 227 | |
| 228 | >>> # List the contents of a dict sorted by key values |
| 229 | >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5) |
| 230 | >>> for k, v in sorted(colormap.iteritems()): |
| 231 | ... print k, v |
| 232 | ... |
| 233 | black 4 |
| 234 | blue 2 |
| 235 | green 3 |
| 236 | red 1 |
| 237 | yellow 5 |
| 238 | </pre></div> |
| 239 | |
| 240 | <P> |
| 241 | (Contributed by Raymond Hettinger.) |
| 242 | |
| 243 | <P> |
| 244 | </LI> |
| 245 | <LI>Integer operations will no longer trigger an <tt class="exception">OverflowWarning</tt>. |
| 246 | The <tt class="exception">OverflowWarning</tt> warning will disappear in Python 2.5. |
| 247 | |
| 248 | <P> |
| 249 | </LI> |
| 250 | <LI>The interpreter gained a new switch, <b class="programopt">-m</b>, that |
| 251 | takes a name, searches for the corresponding module on <code>sys.path</code>, |
| 252 | and runs the module as a script. For example, |
| 253 | you can now run the Python profiler with <code>python -m profile</code>. |
| 254 | (Contributed by Nick Coghlan.) |
| 255 | |
| 256 | <P> |
| 257 | </LI> |
| 258 | <LI>The <tt class="function">eval(<var>expr</var>, <var>globals</var>, <var>locals</var>)</tt> |
| 259 | and <tt class="function">execfile(<var>filename</var>, <var>globals</var>, <var>locals</var>)</tt> |
| 260 | functions and the <tt class="keyword">exec</tt> statement now accept any mapping type |
| 261 | for the <var>locals</var> parameter. Previously this had to be a regular |
| 262 | Python dictionary. (Contributed by Raymond Hettinger.) |
| 263 | |
| 264 | <P> |
| 265 | </LI> |
| 266 | <LI>The <tt class="function">zip()</tt> built-in function and <tt class="function">itertools.izip()</tt> |
| 267 | now return an empty list if called with no arguments. |
| 268 | Previously they raised a <tt class="exception">TypeError</tt> |
| 269 | exception. This makes them more |
| 270 | suitable for use with variable length argument lists: |
| 271 | |
| 272 | <P> |
| 273 | <div class="verbatim"><pre> |
| 274 | >>> def transpose(array): |
| 275 | ... return zip(*array) |
| 276 | ... |
| 277 | >>> transpose([(1,2,3), (4,5,6)]) |
| 278 | [(1, 4), (2, 5), (3, 6)] |
| 279 | >>> transpose([]) |
| 280 | [] |
| 281 | </pre></div> |
| 282 | (Contributed by Raymond Hettinger.) |
| 283 | |
| 284 | <P> |
| 285 | </LI> |
| 286 | <LI>Encountering a failure while importing a module no longer leaves |
| 287 | a partially-initialized module object in <code>sys.modules</code>. The |
| 288 | incomplete module object left behind would fool further imports of the |
| 289 | same module into succeeding, leading to confusing errors. |
| 290 | (Fixed by Tim Peters.) |
| 291 | |
| 292 | <P> |
| 293 | </LI> |
| 294 | <LI><tt class="constant">None</tt> is now a constant; code that binds a new value to |
| 295 | the name "<tt class="samp">None</tt>" is now a syntax error. |
| 296 | (Contributed by Raymond Hettinger.) |
| 297 | |
| 298 | <P> |
| 299 | </LI> |
| 300 | </UL> |
| 301 | |
| 302 | <P> |
| 303 | |
| 304 | <H2><A NAME="SECTION0001210000000000000000"> |
| 305 | 11.1 Optimizations</A> |
| 306 | </H2> |
| 307 | |
| 308 | <P> |
| 309 | |
| 310 | <UL> |
| 311 | <LI>The inner loops for list and tuple slicing |
| 312 | were optimized and now run about one-third faster. The inner loops |
| 313 | for dictionaries were also optimized, resulting in performance boosts for |
| 314 | <tt class="method">keys()</tt>, <tt class="method">values()</tt>, <tt class="method">items()</tt>, |
| 315 | <tt class="method">iterkeys()</tt>, <tt class="method">itervalues()</tt>, and <tt class="method">iteritems()</tt>. |
| 316 | (Contributed by Raymond Hettinger.) |
| 317 | |
| 318 | <P> |
| 319 | </LI> |
| 320 | <LI>The machinery for growing and shrinking lists was optimized for |
| 321 | speed and for space efficiency. Appending and popping from lists now |
| 322 | runs faster due to more efficient code paths and less frequent use of |
| 323 | the underlying system <tt class="cfunction">realloc()</tt>. List comprehensions |
| 324 | also benefit. <tt class="method">list.extend()</tt> was also optimized and no |
| 325 | longer converts its argument into a temporary list before extending |
| 326 | the base list. (Contributed by Raymond Hettinger.) |
| 327 | |
| 328 | <P> |
| 329 | </LI> |
| 330 | <LI><tt class="function">list()</tt>, <tt class="function">tuple()</tt>, <tt class="function">map()</tt>, |
| 331 | <tt class="function">filter()</tt>, and <tt class="function">zip()</tt> now run several times |
| 332 | faster with non-sequence arguments that supply a <tt class="method">__len__()</tt> |
| 333 | method. (Contributed by Raymond Hettinger.) |
| 334 | |
| 335 | <P> |
| 336 | </LI> |
| 337 | <LI>The methods <tt class="method">list.__getitem__()</tt>, |
| 338 | <tt class="method">dict.__getitem__()</tt>, and <tt class="method">dict.__contains__()</tt> are |
| 339 | are now implemented as <tt class="class">method_descriptor</tt> objects rather |
| 340 | than <tt class="class">wrapper_descriptor</tt> objects. This form of |
| 341 | access doubles their performance and makes them more suitable for |
| 342 | use as arguments to functionals: |
| 343 | "<tt class="samp">map(mydict.__getitem__, keylist)</tt>". |
| 344 | (Contributed by Raymond Hettinger.) |
| 345 | |
| 346 | <P> |
| 347 | </LI> |
| 348 | <LI>Added a new opcode, <code>LIST_APPEND</code>, that simplifies |
| 349 | the generated bytecode for list comprehensions and speeds them up |
| 350 | by about a third. (Contributed by Raymond Hettinger.) |
| 351 | |
| 352 | <P> |
| 353 | </LI> |
| 354 | <LI>The peephole bytecode optimizer has been improved to |
| 355 | produce shorter, faster bytecode; remarkably, the resulting bytecode is |
| 356 | more readable. (Enhanced by Raymond Hettinger.) |
| 357 | |
| 358 | <P> |
| 359 | </LI> |
| 360 | <LI>String concatenations in statements of the form <code>s = s + |
| 361 | "abc"</code> and <code>s += "abc"</code> are now performed more efficiently in |
| 362 | certain circumstances. This optimization won't be present in other |
| 363 | Python implementations such as Jython, so you shouldn't rely on it; |
| 364 | using the <tt class="method">join()</tt> method of strings is still recommended when |
| 365 | you want to efficiently glue a large number of strings together. |
| 366 | (Contributed by Armin Rigo.) |
| 367 | |
| 368 | <P> |
| 369 | </LI> |
| 370 | </UL> |
| 371 | |
| 372 | <P> |
| 373 | The net result of the 2.4 optimizations is that Python 2.4 runs the |
| 374 | pystone benchmark around 5% faster than Python 2.3 and 35% faster |
| 375 | than Python 2.2. (pystone is not a particularly good benchmark, but |
| 376 | it's the most commonly used measurement of Python's performance. Your |
| 377 | own applications may show greater or smaller benefits from Python 2.4.) |
| 378 | |
| 379 | <P> |
| 380 | |
| 381 | <DIV CLASS="navigation"> |
| 382 | <div class='online-navigation'> |
| 383 | <p></p><hr /> |
| 384 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 385 | <tr> |
| 386 | <td class='online-navigation'><a rel="prev" title="10 PEP 331: Locale-Independent" |
| 387 | href="node11.html"><img src='../icons/previous.png' |
| 388 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 389 | <td class='online-navigation'><a rel="parent" title="What's New in Python" |
| 390 | href="whatsnew24.html"><img src='../icons/up.png' |
| 391 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 392 | <td class='online-navigation'><a rel="next" title="12 New, Improved, and" |
| 393 | href="node13.html"><img src='../icons/next.png' |
| 394 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 395 | <td align="center" width="100%">What's New in Python 2.4</td> |
| 396 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 397 | href="contents.html"><img src='../icons/contents.png' |
| 398 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 399 | <td class='online-navigation'><img src='../icons/blank.png' |
| 400 | border='0' height='32' alt='' width='32' /></td> |
| 401 | <td class='online-navigation'><img src='../icons/blank.png' |
| 402 | border='0' height='32' alt='' width='32' /></td> |
| 403 | </tr></table> |
| 404 | <div class='online-navigation'> |
| 405 | <b class="navlabel">Previous:</b> |
| 406 | <a class="sectref" rel="prev" href="node11.html">10 PEP 331: Locale-Independent</A> |
| 407 | <b class="navlabel">Up:</b> |
| 408 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> |
| 409 | <b class="navlabel">Next:</b> |
| 410 | <a class="sectref" rel="next" href="node13.html">12 New, Improved, and</A> |
| 411 | </div> |
| 412 | </div> |
| 413 | <hr /> |
| 414 | <span class="release-info">Release 1.01.</span> |
| 415 | </DIV> |
| 416 | <!--End of Navigation Panel--> |
| 417 | <ADDRESS> |
| 418 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 419 | </ADDRESS> |
| 420 | </BODY> |
| 421 | </HTML> |