|
20 | 20 |
|
21 | 21 | This article doesn't attempt to provide a complete specification of |
22 | 22 | the new features, but instead provides a convenient overview. For |
23 | | -full details, you should refer to the documentation for Python 2.4. |
24 | | -% add hyperlink when the documentation becomes available online. |
| 23 | +full details, you should refer to the documentation for Python 2.4, |
| 24 | +such as the \citetitle[../lib/lib.html]{Python Library Reference} and |
| 25 | +the \citetitle[../ref/ref.html]{Python Reference Manual}. |
25 | 26 | If you want to understand the complete implementation and design |
26 | 27 | rationale, refer to the PEP for a particular new feature. |
27 | 28 |
|
| 29 | + |
28 | 30 | %====================================================================== |
29 | 31 | \section{PEP 218: Built-In Set Objects} |
30 | 32 |
|
@@ -67,11 +69,18 @@ \section{PEP 218: Built-In Set Objects} |
67 | 69 | as a member of another set. Accordingly, it does not have methods |
68 | 70 | like \method{add()} and \method{remove()} which could alter its contents. |
69 | 71 |
|
| 72 | +% XXX what happens to the sets module? |
| 73 | + |
70 | 74 | \begin{seealso} |
71 | 75 | \seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by |
72 | 76 | Greg Wilson and ultimately implemented by Raymond Hettinger.} |
73 | 77 | \end{seealso} |
74 | 78 |
|
| 79 | +%====================================================================== |
| 80 | +\section{PEP 237: Unifying Long Integers and Integers} |
| 81 | + |
| 82 | +XXX write this. |
| 83 | + |
75 | 84 | %====================================================================== |
76 | 85 | \section{PEP 322: Reverse Iteration} |
77 | 86 |
|
@@ -122,6 +131,16 @@ \section{Other Language Changes} |
122 | 131 | \method{center()} now take an optional argument for specifying a |
123 | 132 | fill character other than a space. |
124 | 133 |
|
| 134 | +\item Strings also gained an \method{rsplit()} method that |
| 135 | +works like the \method{split()} method but splits from the end of the string. |
| 136 | + |
| 137 | +\begin{verbatim} |
| 138 | +>>> 'a b c'.split(None, 1) |
| 139 | +['a', 'b c'] |
| 140 | +>>> 'a b c'.rsplit(None, 1) |
| 141 | +['a b', 'c'] |
| 142 | +\end{verbatim} |
| 143 | + |
125 | 144 | \item The \method{sort()} method of lists gained three keyword |
126 | 145 | arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments |
127 | 146 | make some common usages of \method{sort()} simpler. All are optional. |
@@ -177,7 +196,7 @@ \section{Other Language Changes} |
177 | 196 | and then sort the list by age, resulting in a list sorted by age where |
178 | 197 | people with the same age are in name-sorted order. |
179 | 198 |
|
180 | | -\item There is a new builtin function \function{sorted(iterable)} that works |
| 199 | +\item There is a new built-in function \function{sorted(iterable)} that works |
181 | 200 | like the in-place \method{list.sort()} method but has been made suitable |
182 | 201 | for use in expressions. The differences are: |
183 | 202 | \begin{itemize} |
@@ -209,7 +228,6 @@ \section{Other Language Changes} |
209 | 228 |
|
210 | 229 | \end{verbatim} |
211 | 230 |
|
212 | | - |
213 | 231 | \item The \function{zip()} built-in function and \function{itertools.izip()} |
214 | 232 | now return an empty list instead of raising a \exception{TypeError} |
215 | 233 | exception if called with no arguments. This makes the functions more |
@@ -312,10 +330,48 @@ \section{New, Improved, and Deprecated Modules} |
312 | 330 | ['a', 'b', 'r'] |
313 | 331 | \end{verbatim} |
314 | 332 |
|
| 333 | +\item \module{itertools} also gained a function named \function{tee(\var{iterator}, \var{N})} that returns \var{N} independent iterators |
| 334 | +that replicate \var{iterator}. If \var{N} is omitted, the default is |
| 335 | +2. |
| 336 | + |
| 337 | +\begin{verbatim} |
| 338 | +>>> L = [1,2,3] |
| 339 | +>>> i1, i2 = itertools.tee(L) |
| 340 | +>>> i1,i2 |
| 341 | +(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>) |
| 342 | +>>> list(i1) |
| 343 | +[1, 2, 3] |
| 344 | +>>> list(i2) |
| 345 | +[1, 2, 3] |
| 346 | +>\end{verbatim} |
| 347 | + |
| 348 | +Note that \function{tee()} has to keep copies of the values returned |
| 349 | +by the iterator; in the worst case it may need to keep all of them. |
| 350 | +This should therefore be used carefully if \var{iterator} |
| 351 | +returns a very large stream of results. |
| 352 | + |
315 | 353 | \item A new \function{getsid()} function was added to the |
316 | 354 | \module{posix} module that underlies the \module{os} module. |
317 | 355 | (Contributed by J. Raynor.) |
318 | 356 |
|
| 357 | +\item The \module{operator} module gained two new functions, |
| 358 | +\function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}. |
| 359 | +Both functions return callables that take a single argument and return |
| 360 | +the corresponding attribute or item; these callables are handy for use |
| 361 | +with \function{map()} or \function{list.sort()}. For example, here's a simple |
| 362 | +us |
| 363 | + |
| 364 | +\begin{verbatim} |
| 365 | +>>> L = [('c', 2), ('d', 1), ('a', '4'), ('b', 3)] |
| 366 | +>>> map(operator.itemgetter(0), L) |
| 367 | +['c', 'd', 'a', 'b'] |
| 368 | +>>> map(operator.itemgetter(1), L) |
| 369 | +[2, 1, '4', 3] |
| 370 | +>>> L.sort(key=operator.itemgetter(1)) # Sort list by second item in tuples |
| 371 | +>>> L |
| 372 | +[('d', 1), ('c', 2), ('b', 3), ('a', '4')] |
| 373 | +\end{verbatim} |
| 374 | + |
319 | 375 | \item The \module{random} module has a new method called \method{getrandbits(N)} |
320 | 376 | which returns an N-bit long integer. This method supports the existing |
321 | 377 | \method{randrange()} method, making it possible to efficiently generate |
|
0 commit comments