Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | \usepackage{distutils} |
| 3 | % $Id$ |
| 4 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 5 | % Don't write extensive text for new sections; I'll do that. |
| 6 | % Feel free to add commented-out reminders of things that need |
| 7 | % to be covered. --amk |
| 8 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 9 | \title{What's New in Python 2.4} |
| 10 | \release{0.0} |
| 11 | \author{A.M.\ Kuchling} |
Fred Drake | b914ef0 | 2004-01-02 06:57:50 +0000 | [diff] [blame] | 12 | \authoraddress{ |
| 13 | \strong{Python Software Foundation}\\ |
| 14 | Email: \email{amk@amk.ca} |
| 15 | } |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 16 | |
| 17 | \begin{document} |
| 18 | \maketitle |
| 19 | \tableofcontents |
| 20 | |
Raymond Hettinger | 6e1fd2f | 2004-05-19 22:30:25 +0000 | [diff] [blame] | 21 | This article explains the new features in Python 2.4. The release |
| 22 | date is expected to be around September 2004. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 23 | |
| 24 | While Python 2.3 was primarily a library development release, Python |
| 25 | 2.4 may extend the core language and interpreter in |
| 26 | as-yet-undetermined ways. |
| 27 | |
| 28 | This article doesn't attempt to provide a complete specification of |
| 29 | the new features, but instead provides a convenient overview. For |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 30 | full details, you should refer to the documentation for Python 2.4, |
| 31 | such as the \citetitle[../lib/lib.html]{Python Library Reference} and |
| 32 | the \citetitle[../ref/ref.html]{Python Reference Manual}. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 33 | If you want to understand the complete implementation and design |
| 34 | rationale, refer to the PEP for a particular new feature. |
| 35 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 36 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 37 | %====================================================================== |
| 38 | \section{PEP 218: Built-In Set Objects} |
| 39 | |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 40 | Two new built-in types, \function{set(\var{iterable})} and |
| 41 | \function{frozenset(\var{iterable})} provide high speed data types for |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 42 | membership testing, for eliminating duplicates from sequences, and |
| 43 | for mathematical operations like unions, intersections, differences, |
| 44 | and symmetric differences. |
| 45 | |
| 46 | \begin{verbatim} |
| 47 | >>> a = set('abracadabra') # form a set from a string |
| 48 | >>> 'z' in a # fast membership testing |
| 49 | False |
| 50 | >>> a # unique letters in a |
| 51 | set(['a', 'r', 'b', 'c', 'd']) |
| 52 | >>> ''.join(a) # convert back into a string |
| 53 | 'arbcd' |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 54 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 55 | >>> b = set('alacazam') # form a second set |
| 56 | >>> a - b # letters in a but not in b |
| 57 | set(['r', 'd', 'b']) |
| 58 | >>> a | b # letters in either a or b |
| 59 | set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) |
| 60 | >>> a & b # letters in both a and b |
| 61 | set(['a', 'c']) |
| 62 | >>> a ^ b # letters in a or b but not both |
| 63 | set(['r', 'd', 'b', 'm', 'z', 'l']) |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 64 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 65 | >>> a.add('z') # add a new element |
| 66 | >>> a.update('wxy') # add multiple new elements |
| 67 | >>> a |
| 68 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z']) |
| 69 | >>> a.remove('x') # take one element out |
| 70 | >>> a |
| 71 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z']) |
| 72 | \end{verbatim} |
| 73 | |
| 74 | The type \function{frozenset()} is an immutable version of \function{set()}. |
| 75 | Since it is immutable and hashable, it may be used as a dictionary key or |
| 76 | as a member of another set. Accordingly, it does not have methods |
| 77 | like \method{add()} and \method{remove()} which could alter its contents. |
| 78 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 79 | % XXX what happens to the sets module? |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 80 | % The current thinking is that the sets module will be left alone. |
| 81 | % That way, existing code will continue to run without alteration. |
| 82 | % Also, the module provides an autoconversion feature not supported by set() |
| 83 | % and frozenset(). |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 84 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 85 | \begin{seealso} |
| 86 | \seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by |
| 87 | Greg Wilson and ultimately implemented by Raymond Hettinger.} |
| 88 | \end{seealso} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 89 | |
| 90 | %====================================================================== |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 91 | \section{PEP 237: Unifying Long Integers and Integers} |
| 92 | |
| 93 | XXX write this. |
| 94 | |
| 95 | %====================================================================== |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 96 | \section{PEP 289: Generator Expressions} |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 97 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 98 | The iterator feature introduced in Python 2.2 makes it easier to write |
| 99 | programs that loop through large data sets without having the entire |
| 100 | data set in memory at one time. Programmers can use iterators and the |
| 101 | \module{itertools} module to write code in a fairly functional style. |
| 102 | |
| 103 | The fly in the ointment has been list comprehensions, because they |
| 104 | produce a Python list object containing all of the items, unavoidably |
| 105 | pulling them all into memory. When trying to write a program using the functional approach, it would be natural to write something like: |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 106 | |
| 107 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 108 | links = [link for link in get_all_links() if not link.followed] |
| 109 | for link in links: |
| 110 | ... |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 111 | \end{verbatim} |
| 112 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 113 | instead of |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 114 | |
| 115 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 116 | for link in get_all_links(): |
| 117 | if link.followed: |
| 118 | continue |
| 119 | ... |
| 120 | \end{verbatim} |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 121 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 122 | The first form is more concise and perhaps more readable, but if |
| 123 | you're dealing with a large number of link objects the second form |
| 124 | would have to be used. |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 125 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 126 | Generator expressions work similarly to list comprehensions but don't |
| 127 | materialize the entire list; instead they create a generator that will |
| 128 | return elements one by one. The above example could be written as: |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 129 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 130 | \begin{verbatim} |
| 131 | links = (link for link in get_all_links() if not link.followed) |
| 132 | for link in links: |
| 133 | ... |
| 134 | \end{verbatim} |
Raymond Hettinger | 170a622 | 2004-05-19 19:45:19 +0000 | [diff] [blame] | 135 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 136 | Generator expressions always have to be written inside parentheses, as |
| 137 | in the above example. The parentheses signalling a function call also |
| 138 | count, so if you want to create a iterator that will be immediately |
| 139 | passed to a function you could write: |
Raymond Hettinger | 170a622 | 2004-05-19 19:45:19 +0000 | [diff] [blame] | 140 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 141 | \begin{verbatim} |
| 142 | print sum(obj.count for obj in list_all_objects()) |
| 143 | \end{verbatim} |
Raymond Hettinger | 170a622 | 2004-05-19 19:45:19 +0000 | [diff] [blame] | 144 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 145 | There are some small differences from list comprehensions. Most |
| 146 | notably, the loop variable (\var{obj} in the above example) is not |
| 147 | accessible outside of the generator expression. List comprehensions |
| 148 | leave the variable assigned to its last value; future versions of |
| 149 | Python will change this, making list comprehensions match generator |
| 150 | expressions in this respect. |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 151 | |
| 152 | \begin{seealso} |
| 153 | \seepep{289}{Generator Expressions}{Proposed by Raymond Hettinger and |
| 154 | implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang.} |
| 155 | \end{seealso} |
| 156 | |
| 157 | %====================================================================== |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 158 | \section{PEP 322: Reverse Iteration} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 159 | |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 160 | A new built-in function, \function{reversed(\var{seq})}, takes a sequence |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 161 | and returns an iterator that returns the elements of the sequence |
| 162 | in reverse order. |
| 163 | |
| 164 | \begin{verbatim} |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 165 | >>> for i in reversed(xrange(1,4)): |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 166 | ... print i |
| 167 | ... |
| 168 | 3 |
| 169 | 2 |
| 170 | 1 |
| 171 | \end{verbatim} |
| 172 | |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 173 | Compared to extended slicing, \code{range(1,4)[::-1]}, \function{reversed()} |
| 174 | is easier to read, runs faster, and uses substantially less memory. |
| 175 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 176 | Note that \function{reversed()} only accepts sequences, not arbitrary |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 177 | iterators. If you want to reverse an iterator, first convert it to |
| 178 | a list with \function{list()}. |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 179 | |
| 180 | \begin{verbatim} |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 181 | >>> input= open('/etc/passwd', 'r') |
| 182 | >>> for line in reversed(list(input)): |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 183 | ... print line |
| 184 | ... |
| 185 | root:*:0:0:System Administrator:/var/root:/bin/tcsh |
| 186 | ... |
| 187 | \end{verbatim} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 188 | |
Andrew M. Kuchling | f7a6b67 | 2003-11-08 16:05:37 +0000 | [diff] [blame] | 189 | \begin{seealso} |
| 190 | \seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.} |
| 191 | |
| 192 | \end{seealso} |
| 193 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 194 | |
| 195 | %====================================================================== |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 196 | \section{PEP 327: Decimal Data Type} |
| 197 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 198 | Python has always supported floating-point (FP) numbers as a data |
| 199 | type, based on the underlying C \ctype{double} type. However, while |
| 200 | most programming languages provide a floating-point type, most people |
| 201 | (even programmers) are unaware that computing with floating-point |
| 202 | numbers entails certain unavoidable inaccuracies. The new decimal |
| 203 | type provides a way to avoid these inaccuracies. |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 204 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 205 | \subsection{Why is Decimal needed?} |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 206 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 207 | The limitations arise from the representation used for floating-point numbers. |
| 208 | FP numbers are made up of three components: |
| 209 | |
| 210 | \begin{itemize} |
| 211 | \item The sign, which is -1 or +1. |
| 212 | \item The mantissa, which is a single-digit binary number |
| 213 | followed by a fractional part. For example, \code{1.01} in base-2 notation |
| 214 | is \code{1 + 0/2 + 1/4}, or 1.25 in decimal notation. |
| 215 | \item The exponent, which tells where the decimal point is located in the number represented. |
| 216 | \end{itemize} |
| 217 | |
| 218 | For example, the number 1.25 has sign +1, mantissa 1.01 (in binary), |
| 219 | and exponent of 0 (the decimal point doesn't need to be shifted). The |
| 220 | number 5 has the same sign and mantissa, but the exponent is 2 |
| 221 | because the mantissa is multiplied by 4 (2 to the power of the exponent 2). |
| 222 | |
| 223 | Modern systems usually provide floating-point support that conforms to |
| 224 | a relevant standard called IEEE 754. C's \ctype{double} type is |
| 225 | usually implemented as a 64-bit IEEE 754 number, which uses 52 bits of |
| 226 | space for the mantissa. This means that numbers can only be specified |
| 227 | to 52 bits of precision. If you're trying to represent numbers whose |
| 228 | expansion repeats endlessly, the expansion is cut off after 52 bits. |
| 229 | Unfortunately, most software needs to produce output in base 10, and |
| 230 | base 10 often gives rise to such repeating decimals. For example, 1.1 |
| 231 | decimal is binary \code{1.0001100110011 ...}; .1 = 1/16 + 1/32 + 1/256 |
| 232 | plus an infinite number of additional terms. IEEE 754 has to chop off |
| 233 | that infinitely repeated decimal after 52 digits, so the |
| 234 | representation is slightly inaccurate. |
| 235 | |
| 236 | Sometimes you can see this inaccuracy when the number is printed: |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 237 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 238 | >>> 1.1 |
| 239 | 1.1000000000000001 |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 240 | \end{verbatim} |
| 241 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 242 | The inaccuracy isn't always visible when you print the number because |
| 243 | the FP-to-decimal-string conversion is provided by the C library, and |
| 244 | most C libraries try to produce sensible output, but the inaccuracy is |
| 245 | still there and subsequent operations can magnify the error. |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 246 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 247 | For many applications this doesn't matter. If I'm plotting points and |
| 248 | displaying them on my monitor, the difference between 1.1 and |
| 249 | 1.1000000000000001 is too small to be visible. Reports often limit |
| 250 | output to a certain number of decimal places, and if you round the |
| 251 | number to two or three or even eight decimal places, the error is |
| 252 | never apparent. However, for applications where it does matter, |
| 253 | it's a lot of work to implement your own custom arithmetic routines. |
| 254 | |
| 255 | \subsection{The \class{Decimal} type} |
| 256 | |
| 257 | A new module, \module{decimal}, was added to Python's standard library. |
| 258 | It contains two classes, \class{Decimal} and \class{Context}. |
| 259 | \class{Decimal} instances represent numbers, and |
| 260 | \class{Context} instances are used to wrap up various settings such as the precision and default rounding mode. |
| 261 | |
| 262 | \class{Decimal} instances, like regular Python integers and FP numbers, are immutable; once they've been created, you can't change the value it represents. |
| 263 | \class{Decimal} instances can be created from integers or strings: |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 264 | |
| 265 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 266 | >>> import decimal |
| 267 | >>> decimal.Decimal(1972) |
| 268 | Decimal("1972") |
| 269 | >>> decimal.Decimal("1.1") |
| 270 | Decimal("1.1") |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 271 | \end{verbatim} |
| 272 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 273 | You can also provide tuples containing the sign, mantissa represented |
| 274 | as a tuple of decimal digits, and exponent: |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 275 | |
| 276 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 277 | >>> decimal.Decimal((1, (1, 4, 7, 5), -2)) |
| 278 | Decimal("-14.75") |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 279 | \end{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 280 | |
| 281 | Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is negative. |
| 282 | |
| 283 | Floating-point numbers posed a bit of a problem: should the FP number |
| 284 | representing 1.1 turn into the decimal number for exactly 1.1, or for |
| 285 | 1.1 plus whatever inaccuracies are introduced? The decision was to |
| 286 | leave such a conversion out of the API. Instead, you should convert |
| 287 | the floating-point number into a string using the desired precision and |
| 288 | pass the string to the \class{Decimal} constructor: |
| 289 | |
| 290 | \begin{verbatim} |
| 291 | >>> f = 1.1 |
| 292 | >>> decimal.Decimal(str(f)) |
| 293 | Decimal("1.1") |
| 294 | >>> decimal.Decimal(repr(f)) |
| 295 | Decimal("1.1000000000000001") |
| 296 | \end{verbatim} |
| 297 | |
| 298 | Once you have \class{Decimal} instances, you can perform the usual |
| 299 | mathematical operations on them. One limitation: exponentiation |
| 300 | requires an integer exponent: |
| 301 | |
| 302 | \begin{verbatim} |
| 303 | >>> a = decimal.Decimal('35.72') |
| 304 | >>> b = decimal.Decimal('1.73') |
| 305 | >>> a+b |
| 306 | Decimal("37.45") |
| 307 | >>> a-b |
| 308 | Decimal("33.99") |
| 309 | >>> a*b |
| 310 | Decimal("61.7956") |
| 311 | >>> a/b |
| 312 | Decimal("20.6473988") |
| 313 | >>> a ** 2 |
| 314 | Decimal("1275.9184") |
| 315 | >>> a ** b |
| 316 | Decimal("NaN") |
| 317 | \end{verbatim} |
| 318 | |
| 319 | You can combine \class{Decimal} instances with integers, but not with |
| 320 | floating-point numbers: |
| 321 | |
| 322 | \begin{verbatim} |
| 323 | >>> a + 4 |
| 324 | Decimal("39.72") |
| 325 | >>> a + 4.5 |
| 326 | Traceback (most recent call last): |
| 327 | ... |
| 328 | TypeError: You can interact Decimal only with int, long or Decimal data types. |
| 329 | >>> |
| 330 | \end{verbatim} |
| 331 | |
| 332 | \class{Decimal} numbers can be used with the \module{math} and |
| 333 | \module{cmath} modules, though you'll get back a regular |
| 334 | floating-point number and not a \class{Decimal}. Instances also have a \method{sqrt()} method: |
| 335 | |
| 336 | \begin{verbatim} |
| 337 | >>> import math, cmath |
| 338 | >>> d = decimal.Decimal('123456789012.345') |
| 339 | >>> math.sqrt(d) |
| 340 | 351364.18288201344 |
| 341 | >>> cmath.sqrt(-d) |
| 342 | 351364.18288201344j |
| 343 | >>> d.sqrt() |
| 344 | Decimal(``351364.1828820134592177245001'') |
| 345 | \end{verbatim} |
| 346 | |
| 347 | |
| 348 | \subsection{The \class{Context} type} |
| 349 | |
| 350 | Instances of the \class{Context} class encapsulate several settings for |
| 351 | decimal operations: |
| 352 | |
| 353 | \begin{itemize} |
| 354 | \item \member{prec} is the precision, the number of decimal places. |
| 355 | \item \member{rounding} specifies the rounding mode. The \module{decimal} |
| 356 | module has constants for the various possibilities: |
| 357 | \constant{ROUND_DOWN}, \constant{ROUND_CEILING}, \constant{ROUND_HALF_EVEN}, and various others. |
| 358 | \item \member{trap_enablers} is a dictionary specifying what happens on |
| 359 | encountering certain error conditions: either an exception is raised or |
| 360 | a value is returned. Some examples of error conditions are |
| 361 | division by zero, loss of precision, and overflow. |
| 362 | \end{itemize} |
| 363 | |
| 364 | There's a thread-local default context available by calling |
| 365 | \function{getcontext()}; you can change the properties of this context |
| 366 | to alter the default precision, rounding, or trap handling. |
| 367 | |
| 368 | \begin{verbatim} |
| 369 | >>> decimal.getcontext().prec |
| 370 | 28 |
| 371 | >>> decimal.Decimal(1) / decimal.Decimal(7) |
| 372 | Decimal(``0.1428571428571428571428571429'') |
| 373 | >>> decimal.getcontext().prec = 9 |
| 374 | >>> decimal.Decimal(1) / decimal.Decimal(7) |
| 375 | Decimal(``0.142857143'') |
| 376 | \end{verbatim} |
| 377 | |
| 378 | The default action for error conditions is to return a special value |
| 379 | such as infinity or not-a-number, but you can request that exceptions |
| 380 | be raised: |
| 381 | |
| 382 | \begin{verbatim} |
| 383 | >>> decimal.Decimal(1) / decimal.Decimal(0) |
| 384 | Decimal(``Infinity'') |
| 385 | >>> decimal.getcontext().trap_enablers[decimal.DivisionByZero] = True |
| 386 | >>> decimal.Decimal(1) / decimal.Decimal(0) |
| 387 | Traceback (most recent call last): |
| 388 | ... |
| 389 | decimal.DivisionByZero: x / 0 |
| 390 | >>> |
| 391 | \end{verbatim} |
| 392 | |
| 393 | The \class{Context} instance also has various methods for formatting |
| 394 | numbers such as \method{to_eng_string()} and \method{to_sci_string()}. |
| 395 | |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 396 | |
| 397 | \begin{seealso} |
| 398 | \seepep{327}{Decimal Data Type}{Written by Facundo Batista and implemented |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame^] | 399 | by Facundo Batista, Eric Price, Raymond Hettinger, Aahz, and Tim Peters.} |
| 400 | |
| 401 | \seeurl{http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html} |
| 402 | {A more detailed overview of the IEEE-754 representation.} |
| 403 | |
| 404 | \seeurl{http://www.lahey.com/float.htm} |
| 405 | {The article uses Fortran code to illustrate many of the problems |
| 406 | that floating-point inaccuracy can cause.} |
| 407 | |
| 408 | \seeurl{http://www2.hursley.ibm.com/decimal/} |
| 409 | {A description of a decimal-based representation. This representation |
| 410 | is being proposed as a standard, and underlies the new Python decimal |
| 411 | type. Much of this material was written by Mike Cowlishaw, designer of the |
| 412 | REXX language.} |
| 413 | |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 414 | \end{seealso} |
| 415 | |
| 416 | |
| 417 | %====================================================================== |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 418 | \section{Other Language Changes} |
| 419 | |
| 420 | Here are all of the changes that Python 2.4 makes to the core Python |
| 421 | language. |
| 422 | |
| 423 | \begin{itemize} |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 424 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 425 | \item The \method{dict.update()} method now accepts the same |
| 426 | argument forms as the \class{dict} constructor. This includes any |
| 427 | mapping, any iterable of key/value pairs, and/or keyword arguments. |
| 428 | |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 429 | \item The string methods, \method{ljust()}, \method{rjust()}, and |
Andrew M. Kuchling | 6708756 | 2003-11-26 18:03:48 +0000 | [diff] [blame] | 430 | \method{center()} now take an optional argument for specifying a |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 431 | fill character other than a space. |
| 432 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 433 | \item Strings also gained an \method{rsplit()} method that |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 434 | works like the \method{split()} method but splits from the end of |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 435 | the string. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 436 | |
| 437 | \begin{verbatim} |
Raymond Hettinger | 7a6d297 | 2004-02-13 19:00:07 +0000 | [diff] [blame] | 438 | >>> 'www.python.org'.split('.', 1) |
| 439 | ['www', 'python.org'] |
| 440 | 'www.python.org'.rsplit('.', 1) |
| 441 | ['www.python', 'org'] |
| 442 | \end{verbatim} |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 443 | |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 444 | \item The \method{sort()} method of lists gained three keyword |
| 445 | arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments |
| 446 | make some common usages of \method{sort()} simpler. All are optional. |
| 447 | |
| 448 | \var{cmp} is the same as the previous single argument to |
| 449 | \method{sort()}; if provided, the value should be a comparison |
| 450 | function that takes two arguments and returns -1, 0, or +1 depending |
| 451 | on how the arguments compare. |
| 452 | |
| 453 | \var{key} should be a single-argument function that takes a list |
| 454 | element and returns a comparison key for the element. The list is |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 455 | then sorted using the comparison keys. The following example sorts a |
| 456 | list case-insensitively: |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 457 | |
| 458 | \begin{verbatim} |
| 459 | >>> L = ['A', 'b', 'c', 'D'] |
| 460 | >>> L.sort() # Case-sensitive sort |
| 461 | >>> L |
| 462 | ['A', 'D', 'b', 'c'] |
| 463 | >>> L.sort(key=lambda x: x.lower()) |
| 464 | >>> L |
| 465 | ['A', 'b', 'c', 'D'] |
| 466 | >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) |
| 467 | >>> L |
| 468 | ['A', 'b', 'c', 'D'] |
| 469 | \end{verbatim} |
| 470 | |
| 471 | The last example, which uses the \var{cmp} parameter, is the old way |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 472 | to perform a case-insensitive sort. It works but is slower than |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 473 | using a \var{key} parameter. Using \var{key} results in calling the |
| 474 | \method{lower()} method once for each element in the list while using |
| 475 | \var{cmp} will call the method twice for each comparison. |
| 476 | |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 477 | For simple key functions and comparison functions, it is often |
| 478 | possible to avoid a \keyword{lambda} expression by using an unbound |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 479 | method instead. For example, the above case-insensitive sort is best |
| 480 | coded as: |
| 481 | |
| 482 | \begin{verbatim} |
| 483 | >>> L.sort(key=str.lower) |
| 484 | >>> L |
| 485 | ['A', 'b', 'c', 'D'] |
| 486 | \end{verbatim} |
| 487 | |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 488 | The \var{reverse} parameter should have a Boolean value. If the value is |
| 489 | \constant{True}, the list will be sorted into reverse order. Instead |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 490 | of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write: |
| 491 | \code{L.sort(key = lambda x: x.score, reverse=True)}. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 492 | |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 493 | The results of sorting are now guaranteed to be stable. This means |
| 494 | that two entries with equal keys will be returned in the same order as |
| 495 | they were input. For example, you can sort a list of people by name, |
| 496 | and then sort the list by age, resulting in a list sorted by age where |
| 497 | people with the same age are in name-sorted order. |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 498 | |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 499 | \item There is a new built-in function |
| 500 | \function{sorted(\var{iterable})} that works like the in-place |
| 501 | \method{list.sort()} method but has been made suitable for use in |
| 502 | expressions. The differences are: |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 503 | \begin{itemize} |
Raymond Hettinger | 7d1dd04 | 2003-11-12 16:42:10 +0000 | [diff] [blame] | 504 | \item the input may be any iterable; |
| 505 | \item a newly formed copy is sorted, leaving the original intact; and |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 506 | \item the expression returns the new sorted copy |
| 507 | \end{itemize} |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 508 | |
| 509 | \begin{verbatim} |
| 510 | >>> L = [9,7,8,3,2,4,1,6,5] |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 511 | >>> [10+i for i in sorted(L)] # usable in a list comprehension |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 512 | [11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 513 | >>> L = [9,7,8,3,2,4,1,6,5] # original is left unchanged |
| 514 | [9,7,8,3,2,4,1,6,5] |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 515 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 516 | >>> sorted('Monte Python') # any iterable may be an input |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 517 | [' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y'] |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 518 | |
| 519 | >>> # List the contents of a dict sorted by key values |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 520 | >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5) |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 521 | >>> for k, v in sorted(colormap.iteritems()): |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 522 | ... print k, v |
| 523 | ... |
| 524 | black 4 |
| 525 | blue 2 |
| 526 | green 3 |
| 527 | red 1 |
| 528 | yellow 5 |
| 529 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 530 | \end{verbatim} |
| 531 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 532 | \item The \function{zip()} built-in function and \function{itertools.izip()} |
Andrew M. Kuchling | 6708756 | 2003-11-26 18:03:48 +0000 | [diff] [blame] | 533 | now return an empty list instead of raising a \exception{TypeError} |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 534 | exception if called with no arguments. This makes them more |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 535 | suitable for use with variable length argument lists: |
| 536 | |
| 537 | \begin{verbatim} |
| 538 | >>> def transpose(array): |
| 539 | ... return zip(*array) |
| 540 | ... |
| 541 | >>> transpose([(1,2,3), (4,5,6)]) |
| 542 | [(1, 4), (2, 5), (3, 6)] |
| 543 | >>> transpose([]) |
| 544 | [] |
| 545 | \end{verbatim} |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 546 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 547 | \end{itemize} |
| 548 | |
| 549 | |
| 550 | %====================================================================== |
| 551 | \subsection{Optimizations} |
| 552 | |
| 553 | \begin{itemize} |
| 554 | |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 555 | \item The inner loops for \class{list} and \class{tuple} slicing |
Raymond Hettinger | ade08ea | 2004-03-18 09:48:12 +0000 | [diff] [blame] | 556 | were optimized and now run about one-third faster. The inner |
| 557 | loops were also optimized for \class{dict} with performance |
| 558 | boosts to \method{keys()}, \method{values()}, \method{items()}, |
Fred Drake | 9de0a2b | 2004-03-20 08:13:32 +0000 | [diff] [blame] | 559 | \method{iterkeys()}, \method{itervalues()}, and \method{iteritems()}. |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 560 | |
Raymond Hettinger | 7a6d297 | 2004-02-13 19:00:07 +0000 | [diff] [blame] | 561 | \item The machinery for growing and shrinking lists was optimized |
Raymond Hettinger | ab517d2 | 2004-02-14 18:34:46 +0000 | [diff] [blame] | 562 | for speed and for space efficiency. Small lists (under eight elements) |
| 563 | never over-allocate by more than three elements. Large lists do not |
Raymond Hettinger | 7a6d297 | 2004-02-13 19:00:07 +0000 | [diff] [blame] | 564 | over-allocate by more than 1/8th. Appending and popping from lists |
| 565 | now runs faster due to more efficient code paths and less frequent |
| 566 | use of the underlying system realloc(). List comprehensions also |
| 567 | benefit. The amount of improvement varies between systems and shows |
| 568 | the greatest improvement on systems with poor realloc() implementations. |
Raymond Hettinger | 79b5cf1 | 2004-02-17 10:46:32 +0000 | [diff] [blame] | 569 | \method{list.extend()} was also optimized and no longer converts its |
| 570 | argument into a temporary list prior to extending the base list. |
Raymond Hettinger | 7a6d297 | 2004-02-13 19:00:07 +0000 | [diff] [blame] | 571 | |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 572 | \item \function{list()}, \function{tuple()}, \function{map()}, |
| 573 | \function{filter()}, and \function{zip()} now run several times |
| 574 | faster with non-sequence arguments that supply a \method{__len__()} |
| 575 | method. Previously, the pre-sizing optimization only applied to |
| 576 | sequence arguments. |
| 577 | |
Raymond Hettinger | 23a0f4e | 2004-01-05 08:15:20 +0000 | [diff] [blame] | 578 | \item The methods \method{list.__getitem__()}, |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 579 | \method{dict.__getitem__()}, and \method{dict.__contains__()} are |
| 580 | are now implemented as \class{method_descriptor} objects rather |
| 581 | than \class{wrapper_descriptor} objects. This form of optimized |
| 582 | access doubles their performance and makes them more suitable for |
Raymond Hettinger | 23a0f4e | 2004-01-05 08:15:20 +0000 | [diff] [blame] | 583 | use as arguments to functionals: |
| 584 | \samp{map(mydict.__getitem__, keylist)}. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 585 | |
Fred Drake | d6d35d9 | 2004-06-03 13:31:22 +0000 | [diff] [blame] | 586 | \item Added a new opcode, \code{LIST_APPEND}, that simplifies |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 587 | the generated bytecode for list comprehensions and speeds them up |
| 588 | by about a third. |
| 589 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 590 | \end{itemize} |
| 591 | |
| 592 | The net result of the 2.4 optimizations is that Python 2.4 runs the |
| 593 | pystone benchmark around XX\% faster than Python 2.3 and YY\% faster |
| 594 | than Python 2.2. |
| 595 | |
| 596 | |
| 597 | %====================================================================== |
| 598 | \section{New, Improved, and Deprecated Modules} |
| 599 | |
| 600 | As usual, Python's standard library received a number of enhancements and |
| 601 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 602 | alphabetically by module name. Consult the |
| 603 | \file{Misc/NEWS} file in the source tree for a more |
| 604 | complete list of changes, or look through the CVS logs for all the |
| 605 | details. |
| 606 | |
| 607 | \begin{itemize} |
| 608 | |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 609 | \item The \module{curses} modules now supports the ncurses extension |
Fred Drake | d6d35d9 | 2004-06-03 13:31:22 +0000 | [diff] [blame] | 610 | \function{use_default_colors()}. On platforms where the terminal |
| 611 | supports transparency, this makes it possible to use a transparent |
| 612 | background. (Contributed by J\"org Lehmann.) |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 613 | |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 614 | \item The \module{bisect} module now has an underlying C implementation |
| 615 | for improved performance. |
| 616 | (Contributed by Dmitry Vasiliev.) |
| 617 | |
Andrew M. Kuchling | 5303a96 | 2004-01-18 15:55:51 +0000 | [diff] [blame] | 618 | \item The CJKCodecs collections of East Asian codecs, maintained |
| 619 | by Hye-Shik Chang, was integrated into 2.4. |
| 620 | The new encodings are: |
| 621 | |
| 622 | \begin{itemize} |
| 623 | \item Chinese (PRC): gb2312, gbk, gb18030, hz |
| 624 | \item Chinese (ROC): big5, cp950 |
| 625 | \item Japanese: cp932, shift-jis, shift-jisx0213, euc-jp, |
| 626 | euc-jisx0213, iso-2022-jp, iso-2022-jp-1, iso-2022-jp-2, |
| 627 | iso-2022-jp-3, iso-2022-jp-ext |
| 628 | \item Korean: cp949, euc-kr, johab, iso-2022-kr |
| 629 | \end{itemize} |
| 630 | |
Andrew M. Kuchling | fd0e494 | 2004-02-09 13:23:34 +0000 | [diff] [blame] | 631 | \item There is a new \module{collections} module for |
| 632 | various specialized collection datatypes. |
| 633 | Currently it contains just one type, \class{deque}, |
| 634 | a double-ended queue that supports efficiently adding and removing |
| 635 | elements from either end. |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 636 | |
| 637 | \begin{verbatim} |
| 638 | >>> from collections import deque |
| 639 | >>> d = deque('ghi') # make a new deque with three items |
| 640 | >>> d.append('j') # add a new entry to the right side |
| 641 | >>> d.appendleft('f') # add a new entry to the left side |
| 642 | >>> d # show the representation of the deque |
| 643 | deque(['f', 'g', 'h', 'i', 'j']) |
| 644 | >>> d.pop() # return and remove the rightmost item |
| 645 | 'j' |
| 646 | >>> d.popleft() # return and remove the leftmost item |
| 647 | 'f' |
| 648 | >>> list(d) # list the contents of the deque |
| 649 | ['g', 'h', 'i'] |
| 650 | >>> 'h' in d # search the deque |
| 651 | True |
| 652 | \end{verbatim} |
| 653 | |
Andrew M. Kuchling | fd0e494 | 2004-02-09 13:23:34 +0000 | [diff] [blame] | 654 | Several modules now take advantage of \class{collections.deque} for |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 655 | improved performance: \module{Queue}, \module{mutex}, \module{shlex} |
| 656 | \module{threading}, and \module{pydoc}. |
Andrew M. Kuchling | 5303a96 | 2004-01-18 15:55:51 +0000 | [diff] [blame] | 657 | |
Fred Drake | 9f15b5c | 2004-05-18 04:30:00 +0000 | [diff] [blame] | 658 | \item The \module{ConfigParser} classes have been enhanced slightly. |
| 659 | The \method{read()} method now returns a list of the files that |
| 660 | were successfully parsed, and the \method{set()} method raises |
| 661 | \exception{TypeError} if passed a \var{value} argument that isn't a |
| 662 | string. |
| 663 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 664 | \item The \module{heapq} module has been converted to C. The resulting |
Andrew M. Kuchling | fd0e494 | 2004-02-09 13:23:34 +0000 | [diff] [blame] | 665 | tenfold improvement in speed makes the module suitable for handling |
Raymond Hettinger | 33ecffb | 2004-06-10 05:03:17 +0000 | [diff] [blame] | 666 | high volumes of data. In addition, the module has two new functions |
| 667 | \function{nlargest()} and \function{nsmallest()} that use heaps to |
| 668 | find the largest or smallest n values in a dataset without the |
| 669 | expense of a full sort. |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 670 | |
Andrew M. Kuchling | dff9dbd | 2003-11-20 22:22:19 +0000 | [diff] [blame] | 671 | \item The \module{imaplib} module now supports IMAP's THREAD command. |
| 672 | (Contributed by Yves Dionne.) |
| 673 | |
Andrew M. Kuchling | ad80955 | 2003-12-06 23:19:23 +0000 | [diff] [blame] | 674 | \item The \module{itertools} module gained a |
| 675 | \function{groupby(\var{iterable}\optional{, \var{func}})} function, |
| 676 | inspired by the GROUP BY clause from SQL. |
| 677 | \var{iterable} returns a succession of elements, and the optional |
| 678 | \var{func} is a function that takes an element and returns a key |
| 679 | value; if omitted, the key is simply the element itself. |
| 680 | \function{groupby()} then groups the elements into subsequences |
| 681 | which have matching values of the key, and returns a series of 2-tuples |
| 682 | containing the key value and an iterator over the subsequence. |
| 683 | |
| 684 | Here's an example. The \var{key} function simply returns whether a |
| 685 | number is even or odd, so the result of \function{groupby()} is to |
| 686 | return consecutive runs of odd or even numbers. |
| 687 | |
| 688 | \begin{verbatim} |
| 689 | >>> import itertools |
| 690 | >>> L = [2,4,6, 7,8,9,11, 12, 14] |
| 691 | >>> for key_val, it in itertools.groupby(L, lambda x: x % 2): |
| 692 | ... print key_val, list(it) |
| 693 | ... |
| 694 | 0 [2, 4, 6] |
| 695 | 1 [7] |
| 696 | 0 [8] |
| 697 | 1 [9, 11] |
| 698 | 0 [12, 14] |
| 699 | >>> |
| 700 | \end{verbatim} |
| 701 | |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 702 | Like its SQL counterpart, \function{groupby()} is typically used with |
| 703 | sorted input. The logic for \function{groupby()} is similar to the |
| 704 | \UNIX{} \code{uniq} filter which makes it handy for eliminating, |
| 705 | counting, or identifying duplicate elements: |
| 706 | |
| 707 | \begin{verbatim} |
| 708 | >>> word = 'abracadabra' |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 709 | >>> letters = sorted(word) # Turn string into a sorted list of letters |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 710 | >>> letters |
Andrew M. Kuchling | 4612bc5 | 2003-12-16 20:59:37 +0000 | [diff] [blame] | 711 | ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r'] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 712 | >>> [k for k, g in groupby(letters)] # List unique letters |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 713 | ['a', 'b', 'c', 'd', 'r'] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 714 | >>> [(k, len(list(g))) for k, g in groupby(letters)] # Count letter occurences |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 715 | [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 716 | >>> [k for k, g in groupby(letters) if len(list(g)) > 1] # List duplicated letters |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 717 | ['a', 'b', 'r'] |
| 718 | \end{verbatim} |
| 719 | |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 720 | \item \module{itertools} also gained a function named |
| 721 | \function{tee(\var{iterator}, \var{N})} that returns \var{N} independent |
| 722 | iterators that replicate \var{iterator}. If \var{N} is omitted, the |
| 723 | default is 2. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 724 | |
| 725 | \begin{verbatim} |
| 726 | >>> L = [1,2,3] |
| 727 | >>> i1, i2 = itertools.tee(L) |
| 728 | >>> i1,i2 |
| 729 | (<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>) |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 730 | >>> list(i1) # Run the first iterator to exhaustion |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 731 | [1, 2, 3] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 732 | >>> list(i2) # Run the second iterator to exhaustion |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 733 | [1, 2, 3] |
| 734 | >\end{verbatim} |
| 735 | |
| 736 | Note that \function{tee()} has to keep copies of the values returned |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 737 | by the iterator; in the worst case, it may need to keep all of them. |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 738 | This should therefore be used carefully if the leading iterator |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 739 | can run far ahead of the trailing iterator in a long stream of inputs. |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 740 | If the separation is large, then it becomes preferable to use |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 741 | \function{list()} instead. When the iterators track closely with one |
| 742 | another, \function{tee()} is ideal. Possible applications include |
| 743 | bookmarking, windowing, or lookahead iterators. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 744 | |
Andrew M. Kuchling | dff9dbd | 2003-11-20 22:22:19 +0000 | [diff] [blame] | 745 | \item A new \function{getsid()} function was added to the |
| 746 | \module{posix} module that underlies the \module{os} module. |
| 747 | (Contributed by J. Raynor.) |
| 748 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 749 | \item The \module{operator} module gained two new functions, |
| 750 | \function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}. |
| 751 | Both functions return callables that take a single argument and return |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 752 | the corresponding attribute or item; these callables make excellent |
| 753 | data extractors when used with \function{map()} or \function{sorted()}. |
| 754 | For example: |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 755 | |
| 756 | \begin{verbatim} |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 757 | >>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)] |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 758 | >>> map(operator.itemgetter(0), L) |
| 759 | ['c', 'd', 'a', 'b'] |
| 760 | >>> map(operator.itemgetter(1), L) |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 761 | [2, 1, 4, 3] |
| 762 | >>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item |
| 763 | [('d', 1), ('c', 2), ('b', 3), ('a', 4)] |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 764 | \end{verbatim} |
| 765 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 766 | \item The \module{random} module has a new method called \method{getrandbits(N)} |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 767 | which returns an N-bit long integer. This method supports the existing |
| 768 | \method{randrange()} method, making it possible to efficiently generate |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 769 | arbitrarily large random numbers. |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 770 | |
| 771 | \item The regular expression language accepted by the \module{re} module |
| 772 | was extended with simple conditional expressions, written as |
| 773 | \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a |
| 774 | numeric group ID or a group name defined with \code{(?P<group>...)} |
| 775 | earlier in the expression. If the specified group matched, the |
| 776 | regular expression pattern \var{A} will be tested against the string; if |
| 777 | the group didn't match, the pattern \var{B} will be used instead. |
Raymond Hettinger | 874ebd5 | 2004-05-31 03:15:02 +0000 | [diff] [blame] | 778 | |
| 779 | \item The \module{weakref} module now supports a wider variety of objects |
| 780 | including Python functions, class instances, sets, frozensets, deques, |
| 781 | arrays, files, sockets, and regular expression pattern objects. |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 782 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 783 | \end{itemize} |
| 784 | |
| 785 | |
| 786 | %====================================================================== |
| 787 | % whole new modules get described in \subsections here |
| 788 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 789 | \subsection{cookielib} |
| 790 | |
| 791 | The \module{cookielib} library supports client-side handling for HTTP |
| 792 | cookies, just as the \module{Cookie} provides server-side cookie |
| 793 | support in CGI scripts. This library manages cookies in a way similar |
| 794 | to web browsers. Cookies are stored in cookie jars; the library |
| 795 | transparently stores cookies offered by the web server in the cookie |
| 796 | jar, and fetches the cookie from the jar when connecting to the |
| 797 | server. Similar to web browsers, policy objects control whether |
| 798 | cookies are accepted or not. |
| 799 | |
| 800 | In order to store cookies across sessions, two implementations of |
| 801 | cookie jars are provided: one that stores cookies in the Netscape |
| 802 | format, so applications can use the Mozilla or Lynx cookie jars, and |
| 803 | one that stores cookies in the same format as the Perl libwww libary. |
| 804 | |
| 805 | \module{urllib2} has been changed to interact with \module{cookielib}: |
| 806 | \class{HTTPCookieProcessor} manages a cookie jar that is used when |
| 807 | accessing URLs. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 808 | |
| 809 | % ====================================================================== |
| 810 | \section{Build and C API Changes} |
| 811 | |
| 812 | Changes to Python's build process and to the C API include: |
| 813 | |
| 814 | \begin{itemize} |
| 815 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 816 | \item Three new convenience macros were added for common return |
| 817 | values from extension functions: \csimplemacro{Py_RETURN_NONE}, |
| 818 | \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}. |
| 819 | |
Fred Drake | ce3caf2 | 2004-02-12 18:13:12 +0000 | [diff] [blame] | 820 | \item A new function, \cfunction{PyTuple_Pack(\var{N}, \var{obj1}, |
| 821 | \var{obj2}, ..., \var{objN})}, constructs tuples from a variable |
| 822 | length argument list of Python objects. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 823 | |
Fred Drake | ce3caf2 | 2004-02-12 18:13:12 +0000 | [diff] [blame] | 824 | \item A new function, \cfunction{PyDict_Contains(\var{d}, \var{k})}, |
| 825 | implements fast dictionary lookups without masking exceptions raised |
| 826 | during the look-up process. |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 827 | |
Fred Drake | ce3caf2 | 2004-02-12 18:13:12 +0000 | [diff] [blame] | 828 | \item A new method flag, \constant{METH_COEXISTS}, allows a function |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 829 | defined in slots to co-exist with a PyCFunction having the same name. |
| 830 | This can halve the access to time to a method such as |
| 831 | \method{set.__contains__()} |
| 832 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 833 | \end{itemize} |
| 834 | |
| 835 | |
| 836 | %====================================================================== |
| 837 | \subsection{Port-Specific Changes} |
| 838 | |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 839 | \begin{itemize} |
| 840 | |
| 841 | \item The Windows port now builds under MSVC++ 7.1 as well as version 6. |
| 842 | |
| 843 | \end{itemize} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 844 | |
| 845 | |
| 846 | %====================================================================== |
| 847 | \section{Other Changes and Fixes \label{section-other}} |
| 848 | |
| 849 | As usual, there were a bunch of other improvements and bugfixes |
| 850 | scattered throughout the source tree. A search through the CVS change |
| 851 | logs finds there were XXX patches applied and YYY bugs fixed between |
| 852 | Python 2.3 and 2.4. Both figures are likely to be underestimates. |
| 853 | |
| 854 | Some of the more notable changes are: |
| 855 | |
| 856 | \begin{itemize} |
| 857 | |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 858 | \item The \module{timeit} module now automatically disables periodic |
| 859 | garbarge collection during the timing loop. This change makes |
| 860 | consecutive timings more comparable. |
| 861 | |
| 862 | \item The \module{base64} module now has more complete RFC 3548 support |
| 863 | for Base64, Base32, and Base16 encoding and decoding, including |
| 864 | optional case folding and optional alternative alphabets. |
| 865 | (Contributed by Barry Warsaw.) |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 866 | |
| 867 | \end{itemize} |
| 868 | |
| 869 | |
| 870 | %====================================================================== |
| 871 | \section{Porting to Python 2.4} |
| 872 | |
| 873 | This section lists previously described changes that may require |
| 874 | changes to your code: |
| 875 | |
| 876 | \begin{itemize} |
| 877 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 878 | \item The \function{zip()} built-in function and \function{itertools.izip()} |
| 879 | now return an empty list instead of raising a \exception{TypeError} |
| 880 | exception if called with no arguments. |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 881 | |
| 882 | \item \function{dircache.listdir()} now passes exceptions to the caller |
| 883 | instead of returning empty lists. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 884 | |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 885 | \item \function{LexicalHandler.startDTD()} used to receive public and |
| 886 | system ID in the wrong order. This has been corrected; applications |
| 887 | relying on the wrong order need to be fixed. |
Martin v. Löwis | 456ab1d | 2004-05-06 01:54:36 +0000 | [diff] [blame] | 888 | |
Michael W. Hudson | 3151e18 | 2004-06-03 13:36:42 +0000 | [diff] [blame] | 889 | \item \function{fcntl.ioctl} now warns if the mutate arg is omitted |
Guido van Rossum | 6dfed6c | 2004-06-03 13:56:05 +0000 | [diff] [blame] | 890 | and relevant. |
Martin v. Löwis | 77ca6c4 | 2004-06-03 12:47:26 +0000 | [diff] [blame] | 891 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 892 | \end{itemize} |
| 893 | |
| 894 | |
| 895 | %====================================================================== |
| 896 | \section{Acknowledgements \label{acks}} |
| 897 | |
| 898 | The author would like to thank the following people for offering |
| 899 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 900 | article: Raymond Hettinger. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 901 | |
| 902 | \end{document} |