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