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 | 2cc0c30 | 2004-09-10 12:38:36 +0000 | [diff] [blame] | 13 | \release{0.4} |
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 | 9fa544c | 2004-09-23 20:17:26 +0000 | [diff] [blame] | 24 | This article explains the new features in Python 2.4 beta1, scheduled |
| 25 | for release in mid-October. The final version of Python 2.4 is |
Andrew M. Kuchling | 3294e9d | 2004-08-31 11:26:23 +0000 | [diff] [blame] | 26 | expected to be released around December 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 |
Andrew M. Kuchling | 3294e9d | 2004-08-31 11:26:23 +0000 | [diff] [blame] | 31 | features (as of this writing) are function decorators and generator |
| 32 | expressions; most other changes are to the standard library. |
Andrew M. Kuchling | a331e86 | 2004-09-10 13:05:22 +0000 | [diff] [blame] | 33 | % XXX update these figures as we go |
| 34 | According to the CVS change logs, there were 421 patches applied and |
| 35 | 413 bugs fixed between Python 2.3 and 2.4. Both figures are likely to |
| 36 | be underestimates. |
| 37 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 38 | |
| 39 | This article doesn't attempt to provide a complete specification of |
Andrew M. Kuchling | 3b79091 | 2004-07-04 16:39:40 +0000 | [diff] [blame] | 40 | every single new feature, but instead provides a convenient overview. |
| 41 | For full details, you should refer to the documentation for Python |
| 42 | 2.4, such as the \citetitle[../lib/lib.html]{Python Library Reference} |
| 43 | and the \citetitle[../ref/ref.html]{Python Reference Manual}. If you |
| 44 | want to understand the complete implementation and design rationale, |
| 45 | refer to the PEP for a particular new feature or to the module |
| 46 | documentation. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 47 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 48 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 49 | %====================================================================== |
| 50 | \section{PEP 218: Built-In Set Objects} |
| 51 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 52 | Python 2.3 introduced the \module{sets} module. C implementations of |
| 53 | set data types have now been added to the Python core as two new |
| 54 | built-in types, \function{set(\var{iterable})} and |
| 55 | \function{frozenset(\var{iterable})}. They provide high speed |
| 56 | operations for membership testing, for eliminating duplicates from |
| 57 | sequences, and for mathematical operations like unions, intersections, |
| 58 | differences, and symmetric differences. |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 59 | |
| 60 | \begin{verbatim} |
| 61 | >>> a = set('abracadabra') # form a set from a string |
| 62 | >>> 'z' in a # fast membership testing |
| 63 | False |
| 64 | >>> a # unique letters in a |
| 65 | set(['a', 'r', 'b', 'c', 'd']) |
| 66 | >>> ''.join(a) # convert back into a string |
| 67 | 'arbcd' |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 68 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 69 | >>> b = set('alacazam') # form a second set |
| 70 | >>> a - b # letters in a but not in b |
| 71 | set(['r', 'd', 'b']) |
| 72 | >>> a | b # letters in either a or b |
| 73 | set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) |
| 74 | >>> a & b # letters in both a and b |
| 75 | set(['a', 'c']) |
| 76 | >>> a ^ b # letters in a or b but not both |
| 77 | set(['r', 'd', 'b', 'm', 'z', 'l']) |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 78 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 79 | >>> a.add('z') # add a new element |
| 80 | >>> a.update('wxy') # add multiple new elements |
| 81 | >>> a |
| 82 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z']) |
| 83 | >>> a.remove('x') # take one element out |
| 84 | >>> a |
| 85 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z']) |
| 86 | \end{verbatim} |
| 87 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 88 | The \function{frozenset} type is an immutable version of \function{set}. |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 89 | 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] | 90 | as a member of another set. |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 91 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 92 | The \module{sets} module remains in the standard library, and may be |
| 93 | useful if you wish to subclass the \class{Set} or \class{ImmutableSet} |
| 94 | classes. There are currently no plans to deprecate the module. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 95 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 96 | \begin{seealso} |
| 97 | \seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by |
| 98 | Greg Wilson and ultimately implemented by Raymond Hettinger.} |
| 99 | \end{seealso} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 100 | |
| 101 | %====================================================================== |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 102 | \section{PEP 237: Unifying Long Integers and Integers} |
| 103 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 104 | 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] | 105 | takes another step forward in Python 2.4. In 2.3, certain integer |
| 106 | operations that would behave differently after int/long unification |
| 107 | triggered \exception{FutureWarning} warnings and returned values |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 108 | limited to 32 or 64 bits (depending on your platform). In 2.4, these |
| 109 | expressions no longer produce a warning and instead produce a |
| 110 | different result that's usually a long integer. |
Andrew M. Kuchling | d4be86c | 2004-07-04 01:44:04 +0000 | [diff] [blame] | 111 | |
| 112 | The problematic expressions are primarily left shifts and lengthy |
Raymond Hettinger | ca1a775 | 2004-07-12 13:00:45 +0000 | [diff] [blame] | 113 | hexadecimal and octal constants. For example, |
| 114 | \code{2 \textless{}\textless{} 32} results |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 115 | in a warning in 2.3, evaluating to 0 on 32-bit platforms. In Python |
| 116 | 2.4, this expression now returns the correct answer, 8589934592. |
Andrew M. Kuchling | d4be86c | 2004-07-04 01:44:04 +0000 | [diff] [blame] | 117 | |
| 118 | \begin{seealso} |
| 119 | \seepep{237}{Unifying Long Integers and Integers}{Original PEP |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 120 | 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] | 121 | Kalle Svensson.} |
| 122 | \end{seealso} |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 123 | |
| 124 | %====================================================================== |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 125 | \section{PEP 289: Generator Expressions} |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 126 | |
Andrew M. Kuchling | 38dc2a6 | 2004-08-07 13:24:12 +0000 | [diff] [blame] | 127 | The iterator feature introduced in Python 2.2 and the |
| 128 | \module{itertools} module make it easier to write programs that loop |
| 129 | through large data sets without having the entire data set in memory |
| 130 | at one time. List comprehensions don't fit into this picture very |
| 131 | well because they produce a Python list object containing all of the |
| 132 | items, unavoidably pulling them all into memory. When trying to write |
| 133 | a functionally-styled program, it would be natural to write something |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 134 | like: |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 135 | |
| 136 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 137 | links = [link for link in get_all_links() if not link.followed] |
| 138 | for link in links: |
| 139 | ... |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 140 | \end{verbatim} |
| 141 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 142 | instead of |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 143 | |
| 144 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 145 | for link in get_all_links(): |
| 146 | if link.followed: |
| 147 | continue |
| 148 | ... |
| 149 | \end{verbatim} |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 150 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 151 | The first form is more concise and perhaps more readable, but if |
| 152 | you're dealing with a large number of link objects the second form |
Andrew M. Kuchling | 38dc2a6 | 2004-08-07 13:24:12 +0000 | [diff] [blame] | 153 | would have to be used to avoid having all link objects in memory at |
| 154 | the same time. |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 155 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 156 | Generator expressions work similarly to list comprehensions but don't |
| 157 | materialize the entire list; instead they create a generator that will |
| 158 | return elements one by one. The above example could be written as: |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 159 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 160 | \begin{verbatim} |
| 161 | links = (link for link in get_all_links() if not link.followed) |
| 162 | for link in links: |
| 163 | ... |
| 164 | \end{verbatim} |
Raymond Hettinger | 170a622 | 2004-05-19 19:45:19 +0000 | [diff] [blame] | 165 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 166 | Generator expressions always have to be written inside parentheses, as |
| 167 | in the above example. The parentheses signalling a function call also |
| 168 | count, so if you want to create a iterator that will be immediately |
| 169 | passed to a function you could write: |
Raymond Hettinger | 170a622 | 2004-05-19 19:45:19 +0000 | [diff] [blame] | 170 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 171 | \begin{verbatim} |
| 172 | print sum(obj.count for obj in list_all_objects()) |
| 173 | \end{verbatim} |
Raymond Hettinger | 170a622 | 2004-05-19 19:45:19 +0000 | [diff] [blame] | 174 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 175 | Generator expressions differ from list comprehensions in various small |
| 176 | ways. Most notably, the loop variable (\var{obj} in the above |
| 177 | example) is not accessible outside of the generator expression. List |
| 178 | comprehensions leave the variable assigned to its last value; future |
| 179 | versions of Python will change this, making list comprehensions match |
| 180 | generator expressions in this respect. |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 181 | |
| 182 | \begin{seealso} |
| 183 | \seepep{289}{Generator Expressions}{Proposed by Raymond Hettinger and |
| 184 | implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang.} |
| 185 | \end{seealso} |
| 186 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 187 | |
| 188 | %====================================================================== |
| 189 | \section{PEP 292: Simpler String Substitutions} |
| 190 | |
| 191 | Some new classes in the standard library provide a |
| 192 | alternative mechanism for substituting variables into strings that's |
| 193 | better-suited for applications where untrained users need to edit templates. |
| 194 | |
| 195 | The usual way of substituting variables by name is the \code{\%} |
| 196 | operator: |
| 197 | |
| 198 | \begin{verbatim} |
| 199 | >>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'} |
| 200 | '2: The Best of Times' |
| 201 | \end{verbatim} |
| 202 | |
| 203 | When writing the template string, it can be easy to forget the |
| 204 | \samp{i} or \samp{s} after the closing parenthesis. This isn't a big |
| 205 | problem if the template is in a Python module, because you run the |
| 206 | code, get an ``Unsupported format character'' \exception{ValueError}, |
| 207 | and fix the problem. However, consider an application such as Mailman |
| 208 | where template strings or translations are being edited by users who |
| 209 | aren't aware of the Python language; the syntax is complicated to |
| 210 | explain to such users, and if they make a mistake, it's difficult to |
| 211 | provide helpful feedback to them. |
| 212 | |
| 213 | PEP 292 adds a \class{Template} class to the \module{string} module |
| 214 | that uses \samp{\$} to indicate a substitution. \class{Template} is a |
| 215 | subclass of the built-in Unicode type, so the result is always a |
| 216 | Unicode string: |
| 217 | |
| 218 | \begin{verbatim} |
| 219 | >>> import string |
| 220 | >>> t = string.Template('$page: $title') |
Andrew M. Kuchling | a79ec22 | 2004-09-10 11:34:39 +0000 | [diff] [blame] | 221 | >>> t.substitute({'page':2, 'title': 'The Best of Times'}) |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 222 | u'2: The Best of Times' |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 223 | \end{verbatim} |
| 224 | |
| 225 | % $ Terminate $-mode for Emacs |
| 226 | |
Andrew M. Kuchling | a79ec22 | 2004-09-10 11:34:39 +0000 | [diff] [blame] | 227 | If a key is missing from the dictionary, the \method{substitute} method |
| 228 | will raise a \exception{KeyError}. There's also a \method{safe_substitute} |
| 229 | method that ignores missing keys: |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 230 | |
| 231 | \begin{verbatim} |
| 232 | >>> t = string.SafeTemplate('$page: $title') |
Andrew M. Kuchling | a79ec22 | 2004-09-10 11:34:39 +0000 | [diff] [blame] | 233 | >>> t.safe_substitute({'page':3}) |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 234 | u'3: $title' |
| 235 | \end{verbatim} |
| 236 | |
Andrew M. Kuchling | a331e86 | 2004-09-10 13:05:22 +0000 | [diff] [blame] | 237 | % $ Terminate math-mode for Emacs |
| 238 | |
| 239 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 240 | \begin{seealso} |
| 241 | \seepep{292}{Simpler String Substitutions}{Written and implemented |
| 242 | by Barry Warsaw.} |
| 243 | \end{seealso} |
| 244 | |
| 245 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 246 | %====================================================================== |
Andrew M. Kuchling | d91fcbe | 2004-08-02 12:44:28 +0000 | [diff] [blame] | 247 | \section{PEP 318: Decorators for Functions, Methods and Classes} |
| 248 | |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 249 | Python 2.2 extended Python's object model by adding static methods and |
| 250 | class methods, but it didn't extend Python's syntax to provide any new |
| 251 | way of defining static or class methods. Instead, you had to write a |
| 252 | \keyword{def} statement in the usual way, and pass the resulting |
| 253 | method to a \function{staticmethod()} or \function{classmethod()} |
| 254 | function that would wrap up the function as a method of the new type. |
| 255 | Your code would look like this: |
| 256 | |
| 257 | \begin{verbatim} |
| 258 | class C: |
| 259 | def meth (cls): |
| 260 | ... |
| 261 | |
| 262 | meth = classmethod(meth) # Rebind name to wrapped-up class method |
| 263 | \end{verbatim} |
| 264 | |
| 265 | If the method was very long, it would be easy to miss or forget the |
| 266 | \function{classmethod()} invocation after the function body. |
| 267 | |
| 268 | The intention was always to add some syntax to make such definitions |
| 269 | more readable, but at the time of 2.2's release a good syntax was not |
| 270 | obvious. Years later, when Python 2.4 is coming out, a good syntax |
| 271 | \emph{still} isn't obvious but users are asking for easier access to |
| 272 | the feature, so a new syntactic feature has been added. |
| 273 | |
| 274 | The feature is called ``function decorators''. The name comes from |
| 275 | the idea that \function{classmethod}, \function{staticmethod}, and |
| 276 | friends are storing additional information on a function object; they're |
| 277 | \emph{decorating} functions with more details. |
| 278 | |
Fred Drake | 3f5c654 | 2004-08-06 03:34:20 +0000 | [diff] [blame] | 279 | The notation borrows from Java and uses the \character{@} character as an |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 280 | indicator. Using the new syntax, the example above would be written: |
| 281 | |
| 282 | \begin{verbatim} |
| 283 | class C: |
| 284 | |
| 285 | @classmethod |
| 286 | def meth (cls): |
| 287 | ... |
| 288 | |
| 289 | \end{verbatim} |
| 290 | |
| 291 | The \code{@classmethod} is shorthand for the |
Fred Drake | 3f5c654 | 2004-08-06 03:34:20 +0000 | [diff] [blame] | 292 | \code{meth=classmethod(meth)} assignment. More generally, if you have |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 293 | the following: |
| 294 | |
| 295 | \begin{verbatim} |
| 296 | @A @B @C |
| 297 | def f (): |
| 298 | ... |
| 299 | \end{verbatim} |
| 300 | |
| 301 | It's equivalent to: |
| 302 | |
| 303 | \begin{verbatim} |
| 304 | def f(): ... |
Andrew M. Kuchling | cebdd3c | 2004-10-08 18:29:29 +0000 | [diff] [blame] | 305 | f = A(B(C(f))) |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 306 | \end{verbatim} |
| 307 | |
| 308 | Decorators must come on the line before a function definition, and |
| 309 | can't be on the same line, meaning that \code{@A def f(): ...} is |
| 310 | illegal. You can only decorate function definitions, either at the |
| 311 | module-level or inside a class; you can't decorate class definitions. |
| 312 | |
| 313 | A decorator is just a function that takes the function to be decorated |
| 314 | as an argument and returns either the same function or some new |
| 315 | callable thing. It's easy to write your own decorators. The |
| 316 | following simple example just sets an attribute on the function |
| 317 | object: |
| 318 | |
| 319 | \begin{verbatim} |
| 320 | >>> def deco(func): |
| 321 | ... func.attr = 'decorated' |
| 322 | ... return func |
| 323 | ... |
| 324 | >>> @deco |
| 325 | ... def f(): pass |
| 326 | ... |
| 327 | >>> f |
| 328 | <function f at 0x402ef0d4> |
| 329 | >>> f.attr |
| 330 | 'decorated' |
| 331 | >>> |
| 332 | \end{verbatim} |
| 333 | |
| 334 | As a slightly more realistic example, the following decorator checks |
| 335 | that the supplied argument is an integer: |
| 336 | |
| 337 | \begin{verbatim} |
| 338 | def require_int (func): |
| 339 | def wrapper (arg): |
| 340 | assert isinstance(arg, int) |
| 341 | return func(arg) |
| 342 | |
| 343 | return wrapper |
| 344 | |
| 345 | @require_int |
| 346 | def p1 (arg): |
| 347 | print arg |
| 348 | |
| 349 | @require_int |
| 350 | def p2(arg): |
| 351 | print arg*2 |
| 352 | \end{verbatim} |
| 353 | |
| 354 | An example in \pep{318} contains a fancier version of this idea that |
| 355 | lets you specify the required type and check the returned type as |
| 356 | well. |
| 357 | |
| 358 | Decorator functions can take arguments. If arguments are supplied, |
| 359 | the decorator function is called with only those arguments and must |
| 360 | return a new decorator function; this new function must take a single |
| 361 | function and return a function, as previously described. In other |
| 362 | words, \code{@A @B @C(args)} becomes: |
| 363 | |
| 364 | \begin{verbatim} |
| 365 | def f(): ... |
| 366 | _deco = C(args) |
Andrew M. Kuchling | cebdd3c | 2004-10-08 18:29:29 +0000 | [diff] [blame] | 367 | f = A(B(_deco(f))) |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 368 | \end{verbatim} |
| 369 | |
| 370 | Getting this right can be slightly brain-bending, but it's not too |
| 371 | difficult. |
| 372 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 373 | A small related change makes the \member{func_name} attribute of |
| 374 | functions writable. This attribute is used to display function names |
| 375 | in tracebacks, so decorators should change the name of any new |
| 376 | function that's constructed and returned. |
| 377 | |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 378 | The new syntax was provisionally added in 2.4alpha2, and is subject to |
Andrew M. Kuchling | 9fa544c | 2004-09-23 20:17:26 +0000 | [diff] [blame] | 379 | change during the 2.4beta release cycle depending on the Python |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 380 | community's reaction. Post-2.4 versions of Python will preserve |
| 381 | compatibility with whatever syntax is used in 2.4final. |
Andrew M. Kuchling | d91fcbe | 2004-08-02 12:44:28 +0000 | [diff] [blame] | 382 | |
| 383 | \begin{seealso} |
| 384 | \seepep{318}{Decorators for Functions, Methods and Classes}{Written |
Andrew M. Kuchling | 77a602f | 2004-08-02 13:48:18 +0000 | [diff] [blame] | 385 | by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people |
| 386 | wrote patches implementing function decorators, but the one that was |
Fred Drake | e72bd4d | 2004-08-02 21:50:26 +0000 | [diff] [blame] | 387 | actually checked in was patch \#979728, written by Mark Russell.} |
Andrew M. Kuchling | d91fcbe | 2004-08-02 12:44:28 +0000 | [diff] [blame] | 388 | \end{seealso} |
| 389 | |
| 390 | %====================================================================== |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 391 | \section{PEP 322: Reverse Iteration} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 392 | |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 393 | 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] | 394 | 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] | 395 | in reverse order. |
| 396 | |
| 397 | \begin{verbatim} |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 398 | >>> for i in reversed(xrange(1,4)): |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 399 | ... print i |
| 400 | ... |
| 401 | 3 |
| 402 | 2 |
| 403 | 1 |
| 404 | \end{verbatim} |
| 405 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 406 | Compared to extended slicing, such as \code{range(1,4)[::-1]}, |
| 407 | \function{reversed()} is easier to read, runs faster, and uses |
| 408 | substantially less memory. |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 409 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 410 | Note that \function{reversed()} only accepts sequences, not arbitrary |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 411 | iterators. If you want to reverse an iterator, first convert it to |
| 412 | a list with \function{list()}. |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 413 | |
| 414 | \begin{verbatim} |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 415 | >>> input= open('/etc/passwd', 'r') |
| 416 | >>> for line in reversed(list(input)): |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 417 | ... print line |
| 418 | ... |
| 419 | root:*:0:0:System Administrator:/var/root:/bin/tcsh |
| 420 | ... |
| 421 | \end{verbatim} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 422 | |
Andrew M. Kuchling | f7a6b67 | 2003-11-08 16:05:37 +0000 | [diff] [blame] | 423 | \begin{seealso} |
| 424 | \seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.} |
| 425 | |
| 426 | \end{seealso} |
| 427 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 428 | |
| 429 | %====================================================================== |
Andrew M. Kuchling | c9e7d77 | 2004-10-12 15:58:02 +0000 | [diff] [blame] | 430 | \section{PEP 324: New subprocess Module} |
| 431 | |
| 432 | The standard library provides a number of ways to |
| 433 | execute a subprocess, each of which offers different features and |
| 434 | levels of difficulty. \function{os.system(\var{command})} is easy, but |
| 435 | slow -- it runs a shell process which executes the command -- |
| 436 | and dangerous -- you have to be careful about escaping metacharacters. |
| 437 | The \module{popen2} module offers classes that can capture |
| 438 | standard output and standard error from the subprocess, but the naming |
| 439 | is confusing. |
| 440 | |
| 441 | The \module{subprocess} module cleans all this up, providing a unified |
| 442 | interface that offers all the features you might need. |
Andrew M. Kuchling | b6ffc27 | 2004-10-12 16:36:57 +0000 | [diff] [blame^] | 443 | Instead of \module{popen2}'s collection of classes, |
| 444 | \module{subprocess} contains a single class called \class{Popen} |
| 445 | whose constructor supports a number of different keyword arguments. |
Andrew M. Kuchling | c9e7d77 | 2004-10-12 15:58:02 +0000 | [diff] [blame] | 446 | |
Andrew M. Kuchling | b6ffc27 | 2004-10-12 16:36:57 +0000 | [diff] [blame^] | 447 | \begin{verbatim} |
| 448 | class Popen(args, bufsize=0, executable=None, |
| 449 | stdin=None, stdout=None, stderr=None, |
| 450 | preexec_fn=None, close_fds=False, shell=False, |
| 451 | cwd=None, env=None, universal_newlines=False, |
| 452 | startupinfo=None, creationflags=0): |
| 453 | \end{verbatim} |
Andrew M. Kuchling | c9e7d77 | 2004-10-12 15:58:02 +0000 | [diff] [blame] | 454 | |
Andrew M. Kuchling | b6ffc27 | 2004-10-12 16:36:57 +0000 | [diff] [blame^] | 455 | \var{args} is commonly a sequence of strings that will be the arguments to |
| 456 | the program executed as the subprocess. (If the \var{shell} argument is true, |
| 457 | \var{args} can be a string which will then be passed on to the shell for interpretation.) |
| 458 | |
| 459 | \var{stdin}, \var{stdout}, and \var{stderr} specify what the |
| 460 | subprocess's input, output, and error streams will be. You can |
| 461 | provide a file object or a file descriptor, or you can |
| 462 | use \code{subprocess.PIPE} to create a pipe between the subprocess |
| 463 | and the parent. |
| 464 | |
| 465 | The constructor has a number of handy options: |
| 466 | |
| 467 | \begin{itemize} |
| 468 | \item \var{close_fds} requests that all file descriptors be closed before running the subprocess. |
| 469 | \item \var{cwd} specifies the working directory in which the subprocess will be executed (defaulting to whatever the parent's working directory is). |
| 470 | \item \var{env} is a dictionary specifying environment variables. |
| 471 | \item \var{preexec_fn} is a function that gets called before the child is started. |
| 472 | \item \var{universal_newlines} opens the child's input and output using |
| 473 | Python's universal newline feature. |
| 474 | \end{itemize} |
| 475 | |
| 476 | Once you've created the \class{Popen} instance, |
| 477 | you can call \method{wait()} to pause until the subprocess has exited, |
| 478 | \method{poll()} to check if it's exited without pausing, |
| 479 | or \method{communicate(\var{data})} to send the string \var{data} to |
| 480 | the subprocess's standard input. \method{communicate(\var{data})} |
| 481 | then reads any data that the subprocess has sent to its standard output or error, returning a tuple \code{(\var{stdout_data}, \var{stderr_data})}. |
| 482 | |
| 483 | \function{call()} is a shortcut that passes its arguments along to |
| 484 | the \class{Popen} constructor, waits for the command to complete, and |
| 485 | returns the status code of the subprocess. It can serve as an analog |
| 486 | to |
| 487 | \function{os.system()}: |
| 488 | |
| 489 | \begin{verbatim} |
| 490 | sts = subprocess.call(['dpkg', '-i', '/tmp/new-package.deb']) |
| 491 | if sts == 0: |
| 492 | # Success |
| 493 | ... |
| 494 | else: |
| 495 | # dpkg returned an error |
| 496 | ... |
| 497 | \end{verbatim} |
| 498 | |
| 499 | The command is invoked without use of the shell. If you really do want to |
| 500 | use the shell, you can add \code{shell=True} as a keyword argument and provide |
| 501 | a string instead of a sequence: |
| 502 | |
| 503 | \begin{verbatim} |
| 504 | sts = subprocess.call('dpkg -i /tmp/new-package.deb', shell=True) |
| 505 | \end{verbatim} |
| 506 | |
| 507 | The PEP takes various examples of shell and Python code and shows how |
| 508 | they'd be translated into Python code that uses \module{subprocess}. |
| 509 | Reading this section of the PEP is highly recommended. |
Andrew M. Kuchling | c9e7d77 | 2004-10-12 15:58:02 +0000 | [diff] [blame] | 510 | |
| 511 | \begin{seealso} |
Andrew M. Kuchling | b6ffc27 | 2004-10-12 16:36:57 +0000 | [diff] [blame^] | 512 | \seepep{324}{subprocess - New process module}{Written and implemented by Peter {\AA}strand, with assistance from Fredrik Lundh and others.} |
Andrew M. Kuchling | c9e7d77 | 2004-10-12 15:58:02 +0000 | [diff] [blame] | 513 | \end{seealso} |
| 514 | |
Andrew M. Kuchling | b6ffc27 | 2004-10-12 16:36:57 +0000 | [diff] [blame^] | 515 | |
Andrew M. Kuchling | c9e7d77 | 2004-10-12 15:58:02 +0000 | [diff] [blame] | 516 | %====================================================================== |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 517 | \section{PEP 327: Decimal Data Type} |
| 518 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 519 | Python has always supported floating-point (FP) numbers as a data |
| 520 | type, based on the underlying C \ctype{double} type. However, while |
| 521 | most programming languages provide a floating-point type, most people |
| 522 | (even programmers) are unaware that computing with floating-point |
| 523 | numbers entails certain unavoidable inaccuracies. The new decimal |
| 524 | type provides a way to avoid these inaccuracies. |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 525 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 526 | \subsection{Why is Decimal needed?} |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 527 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 528 | The limitations arise from the representation used for floating-point numbers. |
| 529 | FP numbers are made up of three components: |
| 530 | |
| 531 | \begin{itemize} |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 532 | \item The sign, which is positive or negative. |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 533 | \item The mantissa, which is a single-digit binary number |
| 534 | followed by a fractional part. For example, \code{1.01} in base-2 notation |
| 535 | is \code{1 + 0/2 + 1/4}, or 1.25 in decimal notation. |
| 536 | \item The exponent, which tells where the decimal point is located in the number represented. |
| 537 | \end{itemize} |
| 538 | |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 539 | For example, the number 1.25 has positive sign, a mantissa value of |
| 540 | 1.01 (in binary), and an exponent of 0 (the decimal point doesn't need |
| 541 | to be shifted). The number 5 has the same sign and mantissa, but the |
| 542 | exponent is 2 because the mantissa is multiplied by 4 (2 to the power |
| 543 | of the exponent 2). |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 544 | |
| 545 | Modern systems usually provide floating-point support that conforms to |
| 546 | a relevant standard called IEEE 754. C's \ctype{double} type is |
| 547 | usually implemented as a 64-bit IEEE 754 number, which uses 52 bits of |
| 548 | space for the mantissa. This means that numbers can only be specified |
| 549 | to 52 bits of precision. If you're trying to represent numbers whose |
| 550 | expansion repeats endlessly, the expansion is cut off after 52 bits. |
| 551 | Unfortunately, most software needs to produce output in base 10, and |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 552 | base 10 often gives rise to such repeating decimals in the binary |
| 553 | expansion. For example, 1.1 decimal is binary \code{1.0001100110011 |
| 554 | ...}; .1 = 1/16 + 1/32 + 1/256 plus an infinite number of additional |
| 555 | terms. IEEE 754 has to chop off that infinitely repeated decimal |
| 556 | after 52 digits, so the representation is slightly inaccurate. |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 557 | |
| 558 | Sometimes you can see this inaccuracy when the number is printed: |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 559 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 560 | >>> 1.1 |
| 561 | 1.1000000000000001 |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 562 | \end{verbatim} |
| 563 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 564 | The inaccuracy isn't always visible when you print the number because |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 565 | the FP-to-decimal-string conversion is provided by the C library, and |
| 566 | most C libraries try to produce sensible output. Even if it's not |
| 567 | displayed, however, the inaccuracy is still there and subsequent |
| 568 | operations can magnify the error. |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 569 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 570 | For many applications this doesn't matter. If I'm plotting points and |
| 571 | displaying them on my monitor, the difference between 1.1 and |
| 572 | 1.1000000000000001 is too small to be visible. Reports often limit |
| 573 | output to a certain number of decimal places, and if you round the |
| 574 | number to two or three or even eight decimal places, the error is |
| 575 | never apparent. However, for applications where it does matter, |
| 576 | it's a lot of work to implement your own custom arithmetic routines. |
| 577 | |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 578 | Hence, the \class{Decimal} type was created. |
| 579 | |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 580 | \subsection{The \class{Decimal} type} |
| 581 | |
| 582 | A new module, \module{decimal}, was added to Python's standard library. |
| 583 | It contains two classes, \class{Decimal} and \class{Context}. |
| 584 | \class{Decimal} instances represent numbers, and |
| 585 | \class{Context} instances are used to wrap up various settings such as the precision and default rounding mode. |
| 586 | |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 587 | \class{Decimal} instances, like regular Python integers and FP |
| 588 | numbers, are immutable; once they've been created, you can't change |
| 589 | the value it represents. \class{Decimal} instances can be created |
| 590 | from integers or strings: |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 591 | |
| 592 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 593 | >>> import decimal |
| 594 | >>> decimal.Decimal(1972) |
| 595 | Decimal("1972") |
| 596 | >>> decimal.Decimal("1.1") |
| 597 | Decimal("1.1") |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 598 | \end{verbatim} |
| 599 | |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 600 | You can also provide tuples containing the sign, the mantissa represented |
| 601 | as a tuple of decimal digits, and the exponent: |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 602 | |
| 603 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 604 | >>> decimal.Decimal((1, (1, 4, 7, 5), -2)) |
| 605 | Decimal("-14.75") |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 606 | \end{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 607 | |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 608 | Cautionary note: the sign bit is a Boolean value, so 0 is positive and |
| 609 | 1 is negative. |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 610 | |
Andrew M. Kuchling | e34c3bd | 2004-08-31 12:21:44 +0000 | [diff] [blame] | 611 | Converting from floating-point numbers poses a bit of a problem: |
| 612 | should the FP number representing 1.1 turn into the decimal number for |
| 613 | exactly 1.1, or for 1.1 plus whatever inaccuracies are introduced? |
| 614 | The decision was to leave such a conversion out of the API. Instead, |
| 615 | you should convert the floating-point number into a string using the |
| 616 | desired precision and pass the string to the \class{Decimal} |
| 617 | constructor: |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 618 | |
| 619 | \begin{verbatim} |
| 620 | >>> f = 1.1 |
| 621 | >>> decimal.Decimal(str(f)) |
| 622 | Decimal("1.1") |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 623 | >>> decimal.Decimal('%.12f' % f) |
| 624 | Decimal("1.100000000000") |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 625 | \end{verbatim} |
| 626 | |
| 627 | Once you have \class{Decimal} instances, you can perform the usual |
| 628 | mathematical operations on them. One limitation: exponentiation |
| 629 | requires an integer exponent: |
| 630 | |
| 631 | \begin{verbatim} |
| 632 | >>> a = decimal.Decimal('35.72') |
| 633 | >>> b = decimal.Decimal('1.73') |
| 634 | >>> a+b |
| 635 | Decimal("37.45") |
| 636 | >>> a-b |
| 637 | Decimal("33.99") |
| 638 | >>> a*b |
| 639 | Decimal("61.7956") |
| 640 | >>> a/b |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 641 | Decimal("20.64739884393063583815028902") |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 642 | >>> a ** 2 |
| 643 | Decimal("1275.9184") |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 644 | >>> a**b |
| 645 | Traceback (most recent call last): |
| 646 | ... |
| 647 | decimal.InvalidOperation: x ** (non-integer) |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 648 | \end{verbatim} |
| 649 | |
| 650 | You can combine \class{Decimal} instances with integers, but not with |
| 651 | floating-point numbers: |
| 652 | |
| 653 | \begin{verbatim} |
| 654 | >>> a + 4 |
| 655 | Decimal("39.72") |
| 656 | >>> a + 4.5 |
| 657 | Traceback (most recent call last): |
| 658 | ... |
| 659 | TypeError: You can interact Decimal only with int, long or Decimal data types. |
| 660 | >>> |
| 661 | \end{verbatim} |
| 662 | |
| 663 | \class{Decimal} numbers can be used with the \module{math} and |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 664 | \module{cmath} modules, but note that they'll be immediately converted to |
| 665 | floating-point numbers before the operation is performed, resulting in |
| 666 | a possible loss of precision and accuracy. You'll also get back a |
| 667 | regular floating-point number and not a \class{Decimal}. |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 668 | |
| 669 | \begin{verbatim} |
| 670 | >>> import math, cmath |
| 671 | >>> d = decimal.Decimal('123456789012.345') |
| 672 | >>> math.sqrt(d) |
| 673 | 351364.18288201344 |
| 674 | >>> cmath.sqrt(-d) |
| 675 | 351364.18288201344j |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 676 | \end{verbatim} |
| 677 | |
| 678 | Instances also have a \method{sqrt()} method that returns a |
| 679 | \class{Decimal}, but if you need other things such as trigonometric |
| 680 | functions you'll have to implement them. |
| 681 | |
| 682 | \begin{verbatim} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 683 | >>> d.sqrt() |
Raymond Hettinger | ca1a775 | 2004-07-12 13:00:45 +0000 | [diff] [blame] | 684 | Decimal("351364.1828820134592177245001") |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 685 | \end{verbatim} |
| 686 | |
| 687 | |
| 688 | \subsection{The \class{Context} type} |
| 689 | |
| 690 | Instances of the \class{Context} class encapsulate several settings for |
| 691 | decimal operations: |
| 692 | |
| 693 | \begin{itemize} |
| 694 | \item \member{prec} is the precision, the number of decimal places. |
| 695 | \item \member{rounding} specifies the rounding mode. The \module{decimal} |
| 696 | module has constants for the various possibilities: |
| 697 | \constant{ROUND_DOWN}, \constant{ROUND_CEILING}, \constant{ROUND_HALF_EVEN}, and various others. |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 698 | \item \member{traps} is a dictionary specifying what happens on |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 699 | encountering certain error conditions: either an exception is raised or |
| 700 | a value is returned. Some examples of error conditions are |
| 701 | division by zero, loss of precision, and overflow. |
| 702 | \end{itemize} |
| 703 | |
| 704 | There's a thread-local default context available by calling |
| 705 | \function{getcontext()}; you can change the properties of this context |
| 706 | to alter the default precision, rounding, or trap handling. |
| 707 | |
| 708 | \begin{verbatim} |
| 709 | >>> decimal.getcontext().prec |
| 710 | 28 |
| 711 | >>> decimal.Decimal(1) / decimal.Decimal(7) |
Raymond Hettinger | ca1a775 | 2004-07-12 13:00:45 +0000 | [diff] [blame] | 712 | Decimal("0.1428571428571428571428571429") |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 713 | >>> decimal.getcontext().prec = 9 |
| 714 | >>> decimal.Decimal(1) / decimal.Decimal(7) |
Raymond Hettinger | ca1a775 | 2004-07-12 13:00:45 +0000 | [diff] [blame] | 715 | Decimal("0.142857143") |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 716 | \end{verbatim} |
| 717 | |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 718 | The default action for error conditions is selectable; the module can |
| 719 | either return a special value such as infinity or not-a-number, or |
| 720 | exceptions can be raised: |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 721 | |
| 722 | \begin{verbatim} |
| 723 | >>> decimal.Decimal(1) / decimal.Decimal(0) |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 724 | Traceback (most recent call last): |
| 725 | ... |
| 726 | decimal.DivisionByZero: x / 0 |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 727 | >>> decimal.getcontext().traps[decimal.DivisionByZero] = False |
| 728 | >>> decimal.Decimal(1) / decimal.Decimal(0) |
| 729 | Decimal("Infinity") |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 730 | >>> |
| 731 | \end{verbatim} |
| 732 | |
| 733 | The \class{Context} instance also has various methods for formatting |
| 734 | numbers such as \method{to_eng_string()} and \method{to_sci_string()}. |
| 735 | |
Andrew M. Kuchling | 0ad20f1 | 2004-07-21 13:00:06 +0000 | [diff] [blame] | 736 | For more information, see the documentation for the \module{decimal} |
| 737 | module, which includes a quick-start tutorial and a reference. |
| 738 | |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 739 | \begin{seealso} |
| 740 | \seepep{327}{Decimal Data Type}{Written by Facundo Batista and implemented |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 741 | by Facundo Batista, Eric Price, Raymond Hettinger, Aahz, and Tim Peters.} |
| 742 | |
Raymond Hettinger | ca1a775 | 2004-07-12 13:00:45 +0000 | [diff] [blame] | 743 | \seeurl{http://research.microsoft.com/\textasciitilde hollasch/cgindex/coding/ieeefloat.html} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 744 | {A more detailed overview of the IEEE-754 representation.} |
| 745 | |
| 746 | \seeurl{http://www.lahey.com/float.htm} |
| 747 | {The article uses Fortran code to illustrate many of the problems |
| 748 | that floating-point inaccuracy can cause.} |
| 749 | |
| 750 | \seeurl{http://www2.hursley.ibm.com/decimal/} |
| 751 | {A description of a decimal-based representation. This representation |
| 752 | is being proposed as a standard, and underlies the new Python decimal |
| 753 | 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] | 754 | Rexx language.} |
Andrew M. Kuchling | c8f8a81 | 2004-07-04 01:26:42 +0000 | [diff] [blame] | 755 | |
Raymond Hettinger | 0fff62f | 2004-07-01 11:52:15 +0000 | [diff] [blame] | 756 | \end{seealso} |
| 757 | |
| 758 | |
| 759 | %====================================================================== |
Andrew M. Kuchling | 3294e9d | 2004-08-31 11:26:23 +0000 | [diff] [blame] | 760 | \section{PEP 328: Multi-line Imports} |
| 761 | |
| 762 | One language change is a small syntactic tweak aimed at making it |
| 763 | easier to import many names from a module. In a |
| 764 | \code{from \var{module} import \var{names}} statement, |
| 765 | \var{names} is a sequence of names separated by commas. If the sequence is |
| 766 | very long, you can either write multiple imports from the same module, |
| 767 | or you can use backslashes to escape the line endings: |
| 768 | |
| 769 | \begin{verbatim} |
| 770 | from SimpleXMLRPCServer import SimpleXMLRPCServer,\ |
| 771 | SimpleXMLRPCRequestHandler,\ |
| 772 | CGIXMLRPCRequestHandler,\ |
| 773 | resolve_dotted_attribute |
| 774 | \end{verbatim} |
| 775 | |
| 776 | The syntactic change simply allows putting the names within |
| 777 | parentheses. Python ignores newlines within a parenthesized |
| 778 | expression, so the backslashes are no longer needed: |
| 779 | |
| 780 | \begin{verbatim} |
| 781 | from SimpleXMLRPCServer import (SimpleXMLRPCServer, |
| 782 | SimpleXMLRPCRequestHandler, |
| 783 | CGIXMLRPCRequestHandler, |
| 784 | resolve_dotted_attribute) |
| 785 | \end{verbatim} |
| 786 | |
| 787 | The PEP also proposes that all \keyword{import} statements be |
| 788 | absolute imports, with a leading \samp{.} character to indicate a |
| 789 | relative import. This part of the PEP is not yet implemented. |
| 790 | |
| 791 | \begin{seealso} |
Fred Drake | 410eb84 | 2004-09-01 04:05:08 +0000 | [diff] [blame] | 792 | \seepep{328}{Imports: Multi-Line and Absolute/Relative} |
| 793 | {Written by Aahz. Multi-line imports were implemented by |
| 794 | Dima Dorfman.} |
| 795 | \end{seealso} |
Andrew M. Kuchling | 3294e9d | 2004-08-31 11:26:23 +0000 | [diff] [blame] | 796 | |
| 797 | |
| 798 | %====================================================================== |
Andrew M. Kuchling | 65a3332 | 2004-07-21 12:41:38 +0000 | [diff] [blame] | 799 | \section{PEP 331: Locale-Independent Float/String Conversions} |
| 800 | |
| 801 | The \module{locale} modules lets Python software select various |
| 802 | conversions and display conventions that are localized to a particular |
| 803 | country or language. However, the module was careful to not change |
| 804 | the numeric locale because various functions in Python's |
| 805 | implementation required that the numeric locale remain set to the |
| 806 | \code{'C'} locale. Often this was because the code was using the C library's |
| 807 | \cfunction{atof()} function. |
| 808 | |
| 809 | Not setting the numeric locale caused trouble for extensions that used |
| 810 | third-party C libraries, however, because they wouldn't have the |
| 811 | correct locale set. The motivating example was GTK+, whose user |
| 812 | interface widgets weren't displaying numbers in the current locale. |
| 813 | |
| 814 | The solution described in the PEP is to add three new functions to the |
| 815 | Python API that perform ASCII-only conversions, ignoring the locale |
| 816 | setting: |
| 817 | |
| 818 | \begin{itemize} |
| 819 | \item \cfunction{PyOS_ascii_strtod(\var{str}, \var{ptr})} |
| 820 | and \cfunction{PyOS_ascii_atof(\var{str}, \var{ptr})} |
| 821 | both convert a string to a C \ctype{double}. |
| 822 | \item \cfunction{PyOS_ascii_formatd(\var{buffer}, \var{buf_len}, \var{format}, \var{d})} converts a \ctype{double} to an ASCII string. |
| 823 | \end{itemize} |
| 824 | |
| 825 | The code for these functions came from the GLib library |
| 826 | (\url{http://developer.gnome.org/arch/gtk/glib.html}), whose |
| 827 | developers kindly relicensed the relevant functions and donated them |
| 828 | to the Python Software Foundation. The \module{locale} module |
| 829 | can now change the numeric locale, letting extensions such as GTK+ |
| 830 | produce the correct results. |
| 831 | |
| 832 | \begin{seealso} |
| 833 | \seepep{331}{Locale-Independent Float/String Conversions}{Written by Christian R. Reis, and implemented by Gustavo Carneiro.} |
| 834 | \end{seealso} |
| 835 | |
| 836 | %====================================================================== |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 837 | \section{Other Language Changes} |
| 838 | |
| 839 | Here are all of the changes that Python 2.4 makes to the core Python |
| 840 | language. |
| 841 | |
| 842 | \begin{itemize} |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 843 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 844 | \item The \method{dict.update()} method now accepts the same |
| 845 | argument forms as the \class{dict} constructor. This includes any |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 846 | mapping, any iterable of key/value pairs, and keyword arguments. |
| 847 | (Contributed by Raymond Hettinger.) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 848 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 849 | \item The string methods \method{ljust()}, \method{rjust()}, and |
Andrew M. Kuchling | 6708756 | 2003-11-26 18:03:48 +0000 | [diff] [blame] | 850 | \method{center()} now take an optional argument for specifying a |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 851 | fill character other than a space. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 852 | (Contributed by Raymond Hettinger.) |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 853 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 854 | \item Strings also gained an \method{rsplit()} method that |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 855 | 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] | 856 | the string. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 857 | |
| 858 | \begin{verbatim} |
Raymond Hettinger | 7a6d297 | 2004-02-13 19:00:07 +0000 | [diff] [blame] | 859 | >>> 'www.python.org'.split('.', 1) |
| 860 | ['www', 'python.org'] |
| 861 | 'www.python.org'.rsplit('.', 1) |
| 862 | ['www.python', 'org'] |
| 863 | \end{verbatim} |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 864 | |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 865 | \item The \method{sort()} method of lists gained three keyword |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 866 | arguments: \var{cmp}, \var{key}, and \var{reverse}. These arguments |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 867 | make some common usages of \method{sort()} simpler. All are optional. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 868 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 869 | |
| 870 | \var{cmp} is the same as the previous single argument to |
| 871 | \method{sort()}; if provided, the value should be a comparison |
| 872 | function that takes two arguments and returns -1, 0, or +1 depending |
| 873 | on how the arguments compare. |
| 874 | |
| 875 | \var{key} should be a single-argument function that takes a list |
| 876 | element and returns a comparison key for the element. The list is |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 877 | then sorted using the comparison keys. The following example sorts a |
| 878 | list case-insensitively: |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 879 | |
| 880 | \begin{verbatim} |
| 881 | >>> L = ['A', 'b', 'c', 'D'] |
| 882 | >>> L.sort() # Case-sensitive sort |
| 883 | >>> L |
| 884 | ['A', 'D', 'b', 'c'] |
| 885 | >>> L.sort(key=lambda x: x.lower()) |
| 886 | >>> L |
| 887 | ['A', 'b', 'c', 'D'] |
| 888 | >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) |
| 889 | >>> L |
| 890 | ['A', 'b', 'c', 'D'] |
| 891 | \end{verbatim} |
| 892 | |
| 893 | 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] | 894 | 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] | 895 | using a \var{key} parameter. Using \var{key} results in calling the |
| 896 | \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] | 897 | \var{cmp} will call it twice for each comparison. |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 898 | |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 899 | For simple key functions and comparison functions, it is often |
| 900 | possible to avoid a \keyword{lambda} expression by using an unbound |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 901 | method instead. For example, the above case-insensitive sort is best |
| 902 | coded as: |
| 903 | |
| 904 | \begin{verbatim} |
| 905 | >>> L.sort(key=str.lower) |
| 906 | >>> L |
| 907 | ['A', 'b', 'c', 'D'] |
| 908 | \end{verbatim} |
| 909 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 910 | The \var{reverse} parameter should have a Boolean value. If the value |
| 911 | is \constant{True}, the list will be sorted into reverse order. |
| 912 | Instead of \code{L.sort(lambda x,y: cmp(x.score, y.score)) ; |
| 913 | L.reverse()}, you can now write: \code{L.sort(key = lambda x: x.score, |
| 914 | reverse=True)}. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 915 | |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 916 | The results of sorting are now guaranteed to be stable. This means |
| 917 | that two entries with equal keys will be returned in the same order as |
| 918 | they were input. For example, you can sort a list of people by name, |
| 919 | and then sort the list by age, resulting in a list sorted by age where |
| 920 | people with the same age are in name-sorted order. |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 921 | |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 922 | \item There is a new built-in function |
| 923 | \function{sorted(\var{iterable})} that works like the in-place |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 924 | \method{list.sort()} method but can be used in |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 925 | expressions. The differences are: |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 926 | \begin{itemize} |
Raymond Hettinger | 7d1dd04 | 2003-11-12 16:42:10 +0000 | [diff] [blame] | 927 | \item the input may be any iterable; |
| 928 | \item a newly formed copy is sorted, leaving the original intact; and |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 929 | \item the expression returns the new sorted copy |
| 930 | \end{itemize} |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 931 | |
| 932 | \begin{verbatim} |
| 933 | >>> L = [9,7,8,3,2,4,1,6,5] |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 934 | >>> [10+i for i in sorted(L)] # usable in a list comprehension |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 935 | [11, 12, 13, 14, 15, 16, 17, 18, 19] |
Hye-Shik Chang | 2b05248 | 2004-07-17 13:53:48 +0000 | [diff] [blame] | 936 | >>> L # original is left unchanged |
Andrew M. Kuchling | e3e1eca | 2004-07-26 18:52:48 +0000 | [diff] [blame] | 937 | [9,7,8,3,2,4,1,6,5] |
| 938 | >>> sorted('Monty Python') # any iterable may be an input |
| 939 | [' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y'] |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 940 | |
| 941 | >>> # List the contents of a dict sorted by key values |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 942 | >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5) |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 943 | >>> for k, v in sorted(colormap.iteritems()): |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 944 | ... print k, v |
| 945 | ... |
| 946 | black 4 |
| 947 | blue 2 |
| 948 | green 3 |
| 949 | red 1 |
| 950 | yellow 5 |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 951 | \end{verbatim} |
| 952 | |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 953 | (Contributed by Raymond Hettinger.) |
| 954 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 955 | \item Integer operations will no longer trigger an \exception{OverflowWarning}. |
| 956 | The \exception{OverflowWarning} warning will disappear in Python 2.5. |
| 957 | |
Andrew M. Kuchling | 5e3f923 | 2004-10-07 12:00:33 +0000 | [diff] [blame] | 958 | \item The interpreter gained a new switch, \programopt{-m}, that |
| 959 | takes a name, searches for the corresponding module on \code{sys.path}, |
| 960 | and runs the module as a script. For example, |
| 961 | you can now run the Python profiler with \code{python -m profile}. |
| 962 | (Contributed by Nick Coghlan.) |
| 963 | |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 964 | \item The \function{eval(\var{expr}, \var{globals}, \var{locals})} |
Andrew M. Kuchling | 1455f79 | 2004-08-02 12:09:58 +0000 | [diff] [blame] | 965 | and \function{execfile(\var{filename}, \var{globals}, \var{locals})} |
| 966 | functions and the \keyword{exec} statement now accept any mapping type |
| 967 | for the \var{locals} argument. Previously this had to be a regular |
| 968 | Python dictionary. (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 969 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 970 | \item The \function{zip()} built-in function and \function{itertools.izip()} |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 971 | now return an empty list if called with no arguments. |
| 972 | Previously they raised a \exception{TypeError} |
| 973 | exception. This makes them more |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 974 | suitable for use with variable length argument lists: |
| 975 | |
| 976 | \begin{verbatim} |
| 977 | >>> def transpose(array): |
| 978 | ... return zip(*array) |
| 979 | ... |
| 980 | >>> transpose([(1,2,3), (4,5,6)]) |
| 981 | [(1, 4), (2, 5), (3, 6)] |
| 982 | >>> transpose([]) |
| 983 | [] |
| 984 | \end{verbatim} |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 985 | (Contributed by Raymond Hettinger.) |
| 986 | |
Andrew M. Kuchling | d91fcbe | 2004-08-02 12:44:28 +0000 | [diff] [blame] | 987 | \item Encountering a failure while importing a module no longer leaves |
| 988 | a partially-initialized module object in \code{sys.modules}. The |
| 989 | incomplete module object left behind would fool further imports of the |
| 990 | same module into succeeding, leading to confusing errors. |
| 991 | |
Andrew M. Kuchling | 65a3332 | 2004-07-21 12:41:38 +0000 | [diff] [blame] | 992 | \item \constant{None} is now a constant; code that binds a new value to |
| 993 | the name \samp{None} is now a syntax error. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 994 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 65a3332 | 2004-07-21 12:41:38 +0000 | [diff] [blame] | 995 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 996 | \end{itemize} |
| 997 | |
| 998 | |
| 999 | %====================================================================== |
| 1000 | \subsection{Optimizations} |
| 1001 | |
| 1002 | \begin{itemize} |
| 1003 | |
Raymond Hettinger | ca1a775 | 2004-07-12 13:00:45 +0000 | [diff] [blame] | 1004 | \item The inner loops for list and tuple slicing |
Andrew M. Kuchling | 65a3332 | 2004-07-21 12:41:38 +0000 | [diff] [blame] | 1005 | were optimized and now run about one-third faster. The inner loops |
| 1006 | were also optimized for dictionaries, resulting in performance boosts for |
| 1007 | \method{keys()}, \method{values()}, \method{items()}, |
| 1008 | \method{iterkeys()}, \method{itervalues()}, and \method{iteritems()}. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1009 | (Contributed by Raymond Hettinger.) |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 1010 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 1011 | \item The machinery for growing and shrinking lists was optimized for |
| 1012 | speed and for space efficiency. Appending and popping from lists now |
| 1013 | runs faster due to more efficient code paths and less frequent use of |
| 1014 | the underlying system \cfunction{realloc()}. List comprehensions |
| 1015 | also benefit. \method{list.extend()} was also optimized and no |
| 1016 | longer converts its argument into a temporary list before extending |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1017 | the base list. (Contributed by Raymond Hettinger.) |
Raymond Hettinger | 7a6d297 | 2004-02-13 19:00:07 +0000 | [diff] [blame] | 1018 | |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 1019 | \item \function{list()}, \function{tuple()}, \function{map()}, |
| 1020 | \function{filter()}, and \function{zip()} now run several times |
| 1021 | faster with non-sequence arguments that supply a \method{__len__()} |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1022 | method. (Contributed by Raymond Hettinger.) |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 1023 | |
Raymond Hettinger | 23a0f4e | 2004-01-05 08:15:20 +0000 | [diff] [blame] | 1024 | \item The methods \method{list.__getitem__()}, |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 1025 | \method{dict.__getitem__()}, and \method{dict.__contains__()} are |
| 1026 | are now implemented as \class{method_descriptor} objects rather |
| 1027 | than \class{wrapper_descriptor} objects. This form of optimized |
| 1028 | access doubles their performance and makes them more suitable for |
Raymond Hettinger | 23a0f4e | 2004-01-05 08:15:20 +0000 | [diff] [blame] | 1029 | use as arguments to functionals: |
| 1030 | \samp{map(mydict.__getitem__, keylist)}. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1031 | (Contributed by Raymond Hettinger.) |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1032 | |
Fred Drake | d6d35d9 | 2004-06-03 13:31:22 +0000 | [diff] [blame] | 1033 | \item Added a new opcode, \code{LIST_APPEND}, that simplifies |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1034 | the generated bytecode for list comprehensions and speeds them up |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1035 | by about a third. (Contributed by Raymond Hettinger.) |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1036 | |
Andrew M. Kuchling | 0c78956 | 2004-09-23 20:15:41 +0000 | [diff] [blame] | 1037 | \item The peephole bytecode optimizer has been improved to |
| 1038 | produce shorter, faster bytecode; remarkably the resulting bytecode is |
| 1039 | more readable. (Enhanced by Raymond Hettinger.) |
| 1040 | |
Andrew M. Kuchling | ac64287 | 2004-08-07 13:13:31 +0000 | [diff] [blame] | 1041 | \item String concatenations in statements of the form \code{s = s + |
| 1042 | "abc"} and \code{s += "abc"} are now performed more efficiently in |
| 1043 | certain circumstances. This optimization won't be present in other |
| 1044 | Python implementations such as Jython, so you shouldn't rely on it; |
| 1045 | using the \method{join()} method of strings is still recommended when |
| 1046 | you want to efficiently glue a large number of strings together. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1047 | (Contributed by Armin Rigo.) |
Andrew M. Kuchling | ac64287 | 2004-08-07 13:13:31 +0000 | [diff] [blame] | 1048 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1049 | \end{itemize} |
| 1050 | |
| 1051 | The net result of the 2.4 optimizations is that Python 2.4 runs the |
| 1052 | pystone benchmark around XX\% faster than Python 2.3 and YY\% faster |
| 1053 | than Python 2.2. |
| 1054 | |
| 1055 | |
| 1056 | %====================================================================== |
| 1057 | \section{New, Improved, and Deprecated Modules} |
| 1058 | |
| 1059 | As usual, Python's standard library received a number of enhancements and |
| 1060 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 1061 | alphabetically by module name. Consult the |
| 1062 | \file{Misc/NEWS} file in the source tree for a more |
| 1063 | complete list of changes, or look through the CVS logs for all the |
| 1064 | details. |
| 1065 | |
| 1066 | \begin{itemize} |
| 1067 | |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 1068 | \item The \module{asyncore} module's \function{loop()} now has a |
| 1069 | \var{count} parameter that lets you perform a limited number |
| 1070 | of passes through the polling loop. The default is still to loop |
| 1071 | forever. |
| 1072 | |
Andrew M. Kuchling | a331e86 | 2004-09-10 13:05:22 +0000 | [diff] [blame] | 1073 | \item The \module{base64} module now has more complete RFC 3548 support |
| 1074 | for Base64, Base32, and Base16 encoding and decoding, including |
| 1075 | optional case folding and optional alternative alphabets. |
| 1076 | (Contributed by Barry Warsaw.) |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 1077 | |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 1078 | \item The \module{bisect} module now has an underlying C implementation |
| 1079 | for improved performance. |
| 1080 | (Contributed by Dmitry Vasiliev.) |
| 1081 | |
Andrew M. Kuchling | 5303a96 | 2004-01-18 15:55:51 +0000 | [diff] [blame] | 1082 | \item The CJKCodecs collections of East Asian codecs, maintained |
| 1083 | by Hye-Shik Chang, was integrated into 2.4. |
| 1084 | The new encodings are: |
| 1085 | |
| 1086 | \begin{itemize} |
Andrew M. Kuchling | 671c506 | 2004-07-28 15:29:39 +0000 | [diff] [blame] | 1087 | \item Chinese (PRC): gb2312, gbk, gb18030, big5hkscs, hz |
Andrew M. Kuchling | 5303a96 | 2004-01-18 15:55:51 +0000 | [diff] [blame] | 1088 | \item Chinese (ROC): big5, cp950 |
Andrew M. Kuchling | 671c506 | 2004-07-28 15:29:39 +0000 | [diff] [blame] | 1089 | \item Japanese: cp932, euc-jis-2004, euc-jp, |
Andrew M. Kuchling | 5303a96 | 2004-01-18 15:55:51 +0000 | [diff] [blame] | 1090 | euc-jisx0213, iso-2022-jp, iso-2022-jp-1, iso-2022-jp-2, |
Andrew M. Kuchling | 671c506 | 2004-07-28 15:29:39 +0000 | [diff] [blame] | 1091 | iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004, |
| 1092 | shift-jis, shift-jisx0213, shift-jis-2004 |
Andrew M. Kuchling | 5303a96 | 2004-01-18 15:55:51 +0000 | [diff] [blame] | 1093 | \item Korean: cp949, euc-kr, johab, iso-2022-kr |
| 1094 | \end{itemize} |
| 1095 | |
Andrew M. Kuchling | 579b3e2 | 2004-10-05 20:23:34 +0000 | [diff] [blame] | 1096 | \item The UTF-8 and UTF-16 codecs now cope better with receiving partial input. |
| 1097 | Previously the \class{StreamReader} class would try to read more data, |
| 1098 | which made it impossible to resume decoding from the stream. The |
| 1099 | \method{read()} method will now return as much data as it can and future |
| 1100 | calls will resume decoding where previous ones left off. |
| 1101 | (Implemented by Walter D\"orwald.) |
| 1102 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 1103 | \item Some other new encodings were added: HP Roman8, |
Andrew M. Kuchling | 579b3e2 | 2004-10-05 20:23:34 +0000 | [diff] [blame] | 1104 | ISO_8859-11, ISO_8859-16, PCTP-154, and TIS-620. |
Andrew M. Kuchling | e30c4d4 | 2004-08-07 13:58:02 +0000 | [diff] [blame] | 1105 | |
Andrew M. Kuchling | fd0e494 | 2004-02-09 13:23:34 +0000 | [diff] [blame] | 1106 | \item There is a new \module{collections} module for |
| 1107 | various specialized collection datatypes. |
| 1108 | Currently it contains just one type, \class{deque}, |
| 1109 | a double-ended queue that supports efficiently adding and removing |
| 1110 | elements from either end. |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1111 | |
| 1112 | \begin{verbatim} |
| 1113 | >>> from collections import deque |
| 1114 | >>> d = deque('ghi') # make a new deque with three items |
| 1115 | >>> d.append('j') # add a new entry to the right side |
| 1116 | >>> d.appendleft('f') # add a new entry to the left side |
| 1117 | >>> d # show the representation of the deque |
| 1118 | deque(['f', 'g', 'h', 'i', 'j']) |
| 1119 | >>> d.pop() # return and remove the rightmost item |
| 1120 | 'j' |
| 1121 | >>> d.popleft() # return and remove the leftmost item |
| 1122 | 'f' |
| 1123 | >>> list(d) # list the contents of the deque |
| 1124 | ['g', 'h', 'i'] |
| 1125 | >>> 'h' in d # search the deque |
| 1126 | True |
| 1127 | \end{verbatim} |
| 1128 | |
Andrew M. Kuchling | fd0e494 | 2004-02-09 13:23:34 +0000 | [diff] [blame] | 1129 | Several modules now take advantage of \class{collections.deque} for |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 1130 | improved performance, such as the \module{Queue} and |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1131 | \module{threading} modules. (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 5303a96 | 2004-01-18 15:55:51 +0000 | [diff] [blame] | 1132 | |
Fred Drake | 9f15b5c | 2004-05-18 04:30:00 +0000 | [diff] [blame] | 1133 | \item The \module{ConfigParser} classes have been enhanced slightly. |
| 1134 | The \method{read()} method now returns a list of the files that |
| 1135 | were successfully parsed, and the \method{set()} method raises |
| 1136 | \exception{TypeError} if passed a \var{value} argument that isn't a |
| 1137 | string. |
| 1138 | |
Andrew M. Kuchling | a331e86 | 2004-09-10 13:05:22 +0000 | [diff] [blame] | 1139 | \item The \module{curses} module now supports the ncurses extension |
| 1140 | \function{use_default_colors()}. On platforms where the terminal |
| 1141 | supports transparency, this makes it possible to use a transparent |
| 1142 | background. (Contributed by J\"org Lehmann.) |
| 1143 | |
| 1144 | \item The \module{difflib} module now includes an \class{HtmlDiff} class |
| 1145 | that creates an HTML table showing a side by side comparison |
| 1146 | of two versions of a text. (Contributed by Dan Gass.) |
| 1147 | |
Andrew M. Kuchling | 579b3e2 | 2004-10-05 20:23:34 +0000 | [diff] [blame] | 1148 | \item The \module{email} package was updated to version 3.0, |
| 1149 | which dropped various deprecated APIs and removes support for Python |
| 1150 | versions earlier than 2.3. The 3.0 version of the package uses a new |
| 1151 | incremental parser for MIME message, available in the |
| 1152 | \module{email.FeedParser} module. The new parser doesn't require |
| 1153 | reading the entire message into memory, and doesn't throw exceptions |
| 1154 | if a message is malformed; instead it records any problems as a |
| 1155 | \member{defect} attribute of the message. (Developed by Anthony |
| 1156 | Baxter, Barry Warsaw, Thomas Wouters, and others.) |
Andrew M. Kuchling | a331e86 | 2004-09-10 13:05:22 +0000 | [diff] [blame] | 1157 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 1158 | \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] | 1159 | tenfold improvement in speed makes the module suitable for handling |
Raymond Hettinger | 33ecffb | 2004-06-10 05:03:17 +0000 | [diff] [blame] | 1160 | high volumes of data. In addition, the module has two new functions |
| 1161 | \function{nlargest()} and \function{nsmallest()} that use heaps to |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 1162 | find the N largest or smallest values in a dataset without the |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1163 | expense of a full sort. (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 1164 | |
Andrew M. Kuchling | 0c78956 | 2004-09-23 20:15:41 +0000 | [diff] [blame] | 1165 | \item The \module{httplib} module now contains constants for HTTP |
| 1166 | status codes defined in various HTTP-related RFC documents. Constants |
| 1167 | have names such as \constant{OK}, \constant{CREATED}, |
| 1168 | \constant{CONTINUE}, and \constant{MOVED_PERMANENTLY}; use pydoc to |
| 1169 | get a full list. (Contributed by Andrew Eland.) |
| 1170 | |
Andrew M. Kuchling | ce4bae6 | 2004-07-27 12:13:25 +0000 | [diff] [blame] | 1171 | \item The \module{imaplib} module now supports IMAP's THREAD command |
| 1172 | (contributed by Yves Dionne) and new \method{deleteacl()} and |
| 1173 | \method{myrights()} methods (contributed by Arnaud Mazin). |
Andrew M. Kuchling | dff9dbd | 2003-11-20 22:22:19 +0000 | [diff] [blame] | 1174 | |
Andrew M. Kuchling | ad80955 | 2003-12-06 23:19:23 +0000 | [diff] [blame] | 1175 | \item The \module{itertools} module gained a |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 1176 | \function{groupby(\var{iterable}\optional{, \var{func}})} function. |
Andrew M. Kuchling | ad80955 | 2003-12-06 23:19:23 +0000 | [diff] [blame] | 1177 | \var{iterable} returns a succession of elements, and the optional |
| 1178 | \var{func} is a function that takes an element and returns a key |
| 1179 | value; if omitted, the key is simply the element itself. |
| 1180 | \function{groupby()} then groups the elements into subsequences |
| 1181 | which have matching values of the key, and returns a series of 2-tuples |
| 1182 | containing the key value and an iterator over the subsequence. |
| 1183 | |
| 1184 | Here's an example. The \var{key} function simply returns whether a |
| 1185 | number is even or odd, so the result of \function{groupby()} is to |
| 1186 | return consecutive runs of odd or even numbers. |
| 1187 | |
| 1188 | \begin{verbatim} |
| 1189 | >>> import itertools |
| 1190 | >>> L = [2,4,6, 7,8,9,11, 12, 14] |
| 1191 | >>> for key_val, it in itertools.groupby(L, lambda x: x % 2): |
| 1192 | ... print key_val, list(it) |
| 1193 | ... |
| 1194 | 0 [2, 4, 6] |
| 1195 | 1 [7] |
| 1196 | 0 [8] |
| 1197 | 1 [9, 11] |
| 1198 | 0 [12, 14] |
| 1199 | >>> |
| 1200 | \end{verbatim} |
| 1201 | |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 1202 | \function{groupby()} is typically used with sorted input. The logic |
| 1203 | for \function{groupby()} is similar to the \UNIX{} \code{uniq} filter |
| 1204 | which makes it handy for eliminating, counting, or identifying |
| 1205 | duplicate elements: |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 1206 | |
| 1207 | \begin{verbatim} |
| 1208 | >>> word = 'abracadabra' |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1209 | >>> letters = sorted(word) # Turn string into a sorted list of letters |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1210 | >>> letters |
Andrew M. Kuchling | 4612bc5 | 2003-12-16 20:59:37 +0000 | [diff] [blame] | 1211 | ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r'] |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 1212 | >>> for k, g in itertools.groupby(letters): |
| 1213 | ... print k, list(g) |
| 1214 | ... |
| 1215 | a ['a', 'a', 'a', 'a', 'a'] |
| 1216 | b ['b', 'b'] |
| 1217 | c ['c'] |
| 1218 | d ['d'] |
| 1219 | r ['r', 'r'] |
| 1220 | >>> # List unique letters |
| 1221 | >>> [k for k, g in groupby(letters)] |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 1222 | ['a', 'b', 'c', 'd', 'r'] |
Johannes Gijsbers | d345225 | 2004-09-11 16:50:06 +0000 | [diff] [blame] | 1223 | >>> # Count letter occurrences |
Andrew M. Kuchling | 3bf85f1 | 2004-07-05 01:37:07 +0000 | [diff] [blame] | 1224 | >>> [(k, len(list(g))) for k, g in groupby(letters)] |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 1225 | [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)] |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 1226 | \end{verbatim} |
| 1227 | |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1228 | (Contributed by Hye-Shik Chang.) |
| 1229 | |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1230 | \item \module{itertools} also gained a function named |
| 1231 | \function{tee(\var{iterator}, \var{N})} that returns \var{N} independent |
| 1232 | iterators that replicate \var{iterator}. If \var{N} is omitted, the |
| 1233 | default is 2. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1234 | |
| 1235 | \begin{verbatim} |
| 1236 | >>> L = [1,2,3] |
| 1237 | >>> i1, i2 = itertools.tee(L) |
| 1238 | >>> i1,i2 |
| 1239 | (<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>) |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1240 | >>> list(i1) # Run the first iterator to exhaustion |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1241 | [1, 2, 3] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1242 | >>> list(i2) # Run the second iterator to exhaustion |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1243 | [1, 2, 3] |
| 1244 | >\end{verbatim} |
| 1245 | |
| 1246 | Note that \function{tee()} has to keep copies of the values returned |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1247 | 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] | 1248 | This should therefore be used carefully if the leading iterator |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1249 | 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] | 1250 | If the separation is large, then you might as well use |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1251 | \function{list()} instead. When the iterators track closely with one |
| 1252 | another, \function{tee()} is ideal. Possible applications include |
| 1253 | bookmarking, windowing, or lookahead iterators. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1254 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1255 | |
Andrew M. Kuchling | 5785a13 | 2004-07-26 19:28:46 +0000 | [diff] [blame] | 1256 | \item A number of functions were added to the \module{locale} |
| 1257 | module, such as \function{bind_textdomain_codeset()} to specify a |
| 1258 | particular encoding, and a family of \function{l*gettext()} functions |
| 1259 | that return messages in the chosen encoding. |
| 1260 | (Contributed by Gustavo Niemeyer.) |
| 1261 | |
Andrew M. Kuchling | 2340689 | 2004-07-15 11:44:42 +0000 | [diff] [blame] | 1262 | \item The \module{logging} package's \function{basicConfig} function |
| 1263 | gained some keyword arguments to simplify log configuration. The |
| 1264 | default behavior is to log messages to standard error, but |
| 1265 | various keyword arguments can be specified to log to a particular file, |
| 1266 | change the logging format, or set the logging level. For example: |
Andrew M. Kuchling | bcefe69 | 2004-07-07 13:01:53 +0000 | [diff] [blame] | 1267 | |
| 1268 | \begin{verbatim} |
| 1269 | import logging |
| 1270 | logging.basicConfig(filename = '/var/log/application.log', |
| 1271 | level=0, # Log all messages, including debugging, |
| 1272 | format='%(levelname):%(process):%(thread):%(message)') |
| 1273 | \end{verbatim} |
| 1274 | |
Andrew M. Kuchling | 579b3e2 | 2004-10-05 20:23:34 +0000 | [diff] [blame] | 1275 | Other additions to \module{logging} include a \method{log(\var{level}, |
| 1276 | \var{msg})} convenience method, and a |
| 1277 | \class{TimedRotatingFileHandler} class that rotates its log files at |
Andrew M. Kuchling | bcefe69 | 2004-07-07 13:01:53 +0000 | [diff] [blame] | 1278 | a timed interval. The module already had \class{RotatingFileHandler}, |
| 1279 | which rotated logs once the file exceeded a certain size. Both |
| 1280 | classes derive from a new \class{BaseRotatingHandler} class that can |
| 1281 | be used to implement other rotating handlers. |
| 1282 | |
Andrew M. Kuchling | 579b3e2 | 2004-10-05 20:23:34 +0000 | [diff] [blame] | 1283 | (Changes implemented by Vinay Sajip.) |
| 1284 | |
Andrew M. Kuchling | 0c78956 | 2004-09-23 20:15:41 +0000 | [diff] [blame] | 1285 | \item The \module{marshal} module now shares interned strings on unpacking a |
| 1286 | data structure. This may shrink the size of certain pickle strings, |
| 1287 | but the primary effect is to make \file{.pyc} files significantly smaller. |
| 1288 | (Contributed by Martin von Loewis.) |
| 1289 | |
Andrew M. Kuchling | 5785a13 | 2004-07-26 19:28:46 +0000 | [diff] [blame] | 1290 | \item The \module{nntplib} module's \class{NNTP} class gained |
| 1291 | \method{description()} and \method{descriptions()} methods to retrieve |
| 1292 | newsgroup descriptions for a single group or for a range of groups. |
| 1293 | (Contributed by J\"urgen A. Erhard.) |
| 1294 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1295 | \item The \module{operator} module gained two new functions, |
| 1296 | \function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}. |
| 1297 | Both functions return callables that take a single argument and return |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1298 | the corresponding attribute or item; these callables make excellent |
Andrew M. Kuchling | bcefe69 | 2004-07-07 13:01:53 +0000 | [diff] [blame] | 1299 | data extractors when used with \function{map()} or |
| 1300 | \function{sorted()}. For example: |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1301 | |
| 1302 | \begin{verbatim} |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1303 | >>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)] |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1304 | >>> map(operator.itemgetter(0), L) |
| 1305 | ['c', 'd', 'a', 'b'] |
| 1306 | >>> map(operator.itemgetter(1), L) |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 1307 | [2, 1, 4, 3] |
| 1308 | >>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item |
| 1309 | [('d', 1), ('c', 2), ('b', 3), ('a', 4)] |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 1310 | \end{verbatim} |
| 1311 | |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1312 | (Contributed by Raymond Hettinger.) |
| 1313 | |
Andrew M. Kuchling | e30c4d4 | 2004-08-07 13:58:02 +0000 | [diff] [blame] | 1314 | \item The \module{optparse} module was updated. The module now passes |
| 1315 | its messages through \function{gettext.gettext()}, making it possible |
| 1316 | to internationalize Optik's help and error messages. Help messages |
Fred Drake | 9bae19e | 2004-08-07 14:28:37 +0000 | [diff] [blame] | 1317 | for options can now include the string \code{'\%default'}, which will |
Andrew M. Kuchling | e30c4d4 | 2004-08-07 13:58:02 +0000 | [diff] [blame] | 1318 | be replaced by the option's default value. |
| 1319 | |
Andrew M. Kuchling | f3958f1 | 2004-10-11 19:20:06 +0000 | [diff] [blame] | 1320 | \item The long-term plan is to deprecate the \module{rfc822} module |
| 1321 | in some future Python release in favor of the \module{email} package. |
| 1322 | To this end, the \function{email.Utils.formatdate()} function has been |
| 1323 | changed to make it usable as a replacement for |
| 1324 | \function{rfc822.formatdate()}. You may want to write new e-mail |
| 1325 | processing code with this in mind. (Change implemented by Anthony |
| 1326 | Baxter.) |
| 1327 | |
Andrew M. Kuchling | cb7b3f3 | 2004-08-30 11:58:04 +0000 | [diff] [blame] | 1328 | \item A new \function{urandom(\var{n})} function |
| 1329 | was added to the \module{os} module, providing access to |
| 1330 | platform-specific sources of randomness such as |
Johannes Gijsbers | ed04748 | 2004-08-30 15:03:23 +0000 | [diff] [blame] | 1331 | \file{/dev/urandom} on Linux or the Windows CryptoAPI. The |
Andrew M. Kuchling | cb7b3f3 | 2004-08-30 11:58:04 +0000 | [diff] [blame] | 1332 | function returns a string containing \var{n} bytes of random data. |
| 1333 | (Contributed by Trevor Perrin.) |
| 1334 | |
| 1335 | \item Another new function: \function{os.path.lexists(\var{path})} |
| 1336 | returns true if the file specified by \var{path} exists, whether or |
| 1337 | not it's a symbolic link. This differs from the existing |
| 1338 | \function{os.path.exists(\var{path})} function, which returns false if |
| 1339 | \var{path} is a symlink that points to a destination that doesn't exist. |
| 1340 | (Contributed by Beni Cherniavsky.) |
| 1341 | |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 1342 | \item A new \function{getsid()} function was added to the |
| 1343 | \module{posix} module that underlies the \module{os} module. |
| 1344 | (Contributed by J. Raynor.) |
| 1345 | |
| 1346 | \item The \module{poplib} module now supports POP over SSL. |
| 1347 | |
| 1348 | \item The \module{profile} module can now profile C extension functions. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1349 | % XXX more to say about this? |
| 1350 | (Contributed by Nick Bastin.) |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 1351 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 1352 | \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] | 1353 | which returns an N-bit long integer. This method supports the existing |
| 1354 | \method{randrange()} method, making it possible to efficiently generate |
Andrew M. Kuchling | 44a31e1 | 2004-01-01 18:33:34 +0000 | [diff] [blame] | 1355 | arbitrarily large random numbers. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1356 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 1357 | |
| 1358 | \item The regular expression language accepted by the \module{re} module |
| 1359 | was extended with simple conditional expressions, written as |
Andrew M. Kuchling | ab77822 | 2004-08-31 12:07:43 +0000 | [diff] [blame] | 1360 | \regexp{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a |
| 1361 | numeric group ID or a group name defined with \regexp{(?P<group>...)} |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 1362 | earlier in the expression. If the specified group matched, the |
| 1363 | regular expression pattern \var{A} will be tested against the string; if |
| 1364 | 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] | 1365 | |
Andrew M. Kuchling | ab77822 | 2004-08-31 12:07:43 +0000 | [diff] [blame] | 1366 | \item The \module{re} module is also no longer recursive, thanks |
| 1367 | to a massive amount of work by Gustavo Niemeyer. In a recursive |
| 1368 | regular expression engine, certain patterns result in a large amount |
| 1369 | of C stack space being consumed, and it was possible to overflow the |
| 1370 | stack. For example, if you matched a 30000-byte string of \samp{a} |
| 1371 | characters against the expression \regexp{(a|b)+}, one stack frame was |
| 1372 | consumed per character. Python 2.3 tried to check for stack overflow |
| 1373 | and raise a \exception{RuntimeError} exception, but if you were |
| 1374 | unlucky Python could dump core. Python 2.4's regular expression |
| 1375 | engine can match this pattern without problems. |
| 1376 | |
Andrew M. Kuchling | 7f203b8 | 2004-08-09 14:48:28 +0000 | [diff] [blame] | 1377 | \item A new \function{socketpair()} function was added to the |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 1378 | \module{socket} module, returning a pair of connected sockets. |
| 1379 | (Contributed by Dave Cole.) |
Andrew M. Kuchling | 7f203b8 | 2004-08-09 14:48:28 +0000 | [diff] [blame] | 1380 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 1381 | \item The \function{sys.exitfunc()} function has been deprecated. Code |
| 1382 | should be using the existing \module{atexit} module, which correctly |
| 1383 | handles calling multiple exit functions. Eventually |
| 1384 | \function{sys.exitfunc()} will become a purely internal interface, |
| 1385 | accessed only by \module{atexit}. |
| 1386 | |
| 1387 | \item The \module{tarfile} module now generates GNU-format tar files |
| 1388 | by default. |
| 1389 | |
Andrew M. Kuchling | 0045717 | 2004-07-15 11:52:40 +0000 | [diff] [blame] | 1390 | \item The \module{threading} module now has an elegantly simple way to support |
| 1391 | thread-local data. The module contains a \class{local} class whose |
| 1392 | attribute values are local to different threads. |
| 1393 | |
| 1394 | \begin{verbatim} |
| 1395 | import threading |
| 1396 | |
| 1397 | data = threading.local() |
| 1398 | data.number = 42 |
| 1399 | data.url = ('www.python.org', 80) |
| 1400 | \end{verbatim} |
| 1401 | |
| 1402 | Other threads can assign and retrieve their own values for the |
| 1403 | \member{number} and \member{url} attributes. You can subclass |
| 1404 | \class{local} to initialize attributes or to add methods. |
| 1405 | (Contributed by Jim Fulton.) |
| 1406 | |
Andrew M. Kuchling | a331e86 | 2004-09-10 13:05:22 +0000 | [diff] [blame] | 1407 | \item The \module{timeit} module now automatically disables periodic |
| 1408 | garbarge collection during the timing loop. This change makes |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1409 | consecutive timings more comparable. (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | a331e86 | 2004-09-10 13:05:22 +0000 | [diff] [blame] | 1410 | |
Raymond Hettinger | 874ebd5 | 2004-05-31 03:15:02 +0000 | [diff] [blame] | 1411 | \item The \module{weakref} module now supports a wider variety of objects |
| 1412 | including Python functions, class instances, sets, frozensets, deques, |
| 1413 | arrays, files, sockets, and regular expression pattern objects. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1414 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 1415 | |
| 1416 | \item The \module{xmlrpclib} module now supports a multi-call extension for |
Andrew M. Kuchling | 0045717 | 2004-07-15 11:52:40 +0000 | [diff] [blame] | 1417 | transmitting multiple XML-RPC calls in a single HTTP operation. |
Andrew M. Kuchling | 3d3db96 | 2004-08-31 13:57:02 +0000 | [diff] [blame] | 1418 | |
| 1419 | \item The \module{mpz}, \module{rotor}, and \module{xreadlines} modules have |
| 1420 | been removed. |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 1421 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1422 | \end{itemize} |
| 1423 | |
| 1424 | |
| 1425 | %====================================================================== |
Raymond Hettinger | ca1a775 | 2004-07-12 13:00:45 +0000 | [diff] [blame] | 1426 | % whole new modules get described in subsections here |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1427 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1428 | \subsection{cookielib} |
| 1429 | |
| 1430 | The \module{cookielib} library supports client-side handling for HTTP |
| 1431 | cookies, just as the \module{Cookie} provides server-side cookie |
Andrew M. Kuchling | 71432f1 | 2004-07-05 01:40:07 +0000 | [diff] [blame] | 1432 | 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] | 1433 | transparently stores cookies offered by the web server in the cookie |
| 1434 | jar, and fetches the cookie from the jar when connecting to the |
| 1435 | server. Similar to web browsers, policy objects control whether |
| 1436 | cookies are accepted or not. |
| 1437 | |
| 1438 | In order to store cookies across sessions, two implementations of |
| 1439 | cookie jars are provided: one that stores cookies in the Netscape |
| 1440 | format, so applications can use the Mozilla or Lynx cookie jars, and |
| 1441 | one that stores cookies in the same format as the Perl libwww libary. |
| 1442 | |
| 1443 | \module{urllib2} has been changed to interact with \module{cookielib}: |
| 1444 | \class{HTTPCookieProcessor} manages a cookie jar that is used when |
| 1445 | accessing URLs. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1446 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 1447 | \subsection{doctest} |
| 1448 | |
| 1449 | The \module{doctest} module underwent considerable refactoring thanks |
| 1450 | to Edward Loper and Tim Peters. |
| 1451 | |
| 1452 | % XXX describe this |
| 1453 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1454 | % ====================================================================== |
| 1455 | \section{Build and C API Changes} |
| 1456 | |
| 1457 | Changes to Python's build process and to the C API include: |
| 1458 | |
| 1459 | \begin{itemize} |
| 1460 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 1461 | \item Three new convenience macros were added for common return |
| 1462 | values from extension functions: \csimplemacro{Py_RETURN_NONE}, |
| 1463 | \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1464 | (Contributed by Brett Cannon.) |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 1465 | |
Andrew M. Kuchling | 5785a13 | 2004-07-26 19:28:46 +0000 | [diff] [blame] | 1466 | \item Another new macro, \csimplemacro{Py_CLEAR(\var{obj})}, |
| 1467 | decreases the reference count of \var{obj} and sets \var{obj} to the |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1468 | null pointer. (Contributed by Jim Fulton.) |
Andrew M. Kuchling | 5785a13 | 2004-07-26 19:28:46 +0000 | [diff] [blame] | 1469 | |
Fred Drake | ce3caf2 | 2004-02-12 18:13:12 +0000 | [diff] [blame] | 1470 | \item A new function, \cfunction{PyTuple_Pack(\var{N}, \var{obj1}, |
| 1471 | \var{obj2}, ..., \var{objN})}, constructs tuples from a variable |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1472 | length argument list of Python objects. (Contributed by Raymond Hettinger.) |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1473 | |
Fred Drake | ce3caf2 | 2004-02-12 18:13:12 +0000 | [diff] [blame] | 1474 | \item A new function, \cfunction{PyDict_Contains(\var{d}, \var{k})}, |
| 1475 | implements fast dictionary lookups without masking exceptions raised |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1476 | during the look-up process. (Contributed by Raymond Hettinger.) |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 1477 | |
Andrew M. Kuchling | 0c78956 | 2004-09-23 20:15:41 +0000 | [diff] [blame] | 1478 | \item The \csimplemacro{Py_IS_NAN(\var{X})} macro returns 1 if |
| 1479 | its float or double argument \var{X} is a NaN. |
| 1480 | (Contributed by Tim Peters.) |
| 1481 | |
Andrew M. Kuchling | f3958f1 | 2004-10-11 19:20:06 +0000 | [diff] [blame] | 1482 | \item C code can avoid unnecessary locking by using the new |
| 1483 | \cfunction{PyEval_ThreadsInitialized()} function to tell |
| 1484 | if any thread operations have been performed. If this function |
| 1485 | returns false, no lock operations are needed. |
| 1486 | (Contributed by Nick Coghlan.) |
| 1487 | |
Andrew M. Kuchling | e30c4d4 | 2004-08-07 13:58:02 +0000 | [diff] [blame] | 1488 | \item A new function, \cfunction{PyArg_VaParseTupleAndKeywords()}, |
| 1489 | is the same as \cfunction{PyArg_ParseTupleAndKeywords()} but takes a |
| 1490 | \ctype{va_list} instead of a number of arguments. |
| 1491 | (Contributed by Greg Chapman.) |
| 1492 | |
Fred Drake | ce3caf2 | 2004-02-12 18:13:12 +0000 | [diff] [blame] | 1493 | \item A new method flag, \constant{METH_COEXISTS}, allows a function |
Andrew M. Kuchling | 71432f1 | 2004-07-05 01:40:07 +0000 | [diff] [blame] | 1494 | defined in slots to co-exist with a \ctype{PyCFunction} having the |
| 1495 | same name. This can halve the access time for a method such as |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1496 | \method{set.__contains__()}. (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 1497 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 1498 | \item Python can now be built with additional profiling for the |
| 1499 | interpreter itself. This is intended for people developing on the |
| 1500 | Python core. Providing \longprogramopt{--enable-profiling} to the |
| 1501 | \program{configure} script will let you profile the interpreter with |
| 1502 | \program{gprof}, and providing the \longprogramopt{--with-tsc} |
| 1503 | switch enables profiling using the Pentium's Time-Stamp-Counter |
| 1504 | register. The switch is slightly misnamed, because the profiling |
| 1505 | feature also works on the PowerPC platform, though that processor |
Raymond Hettinger | 468af71 | 2004-09-20 17:47:46 +0000 | [diff] [blame] | 1506 | architecture doesn't call that register a TSC. |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 1507 | |
Andrew M. Kuchling | d0b6d9d | 2004-07-04 15:35:00 +0000 | [diff] [blame] | 1508 | \item The \ctype{tracebackobject} type has been renamed to \ctype{PyTracebackObject}. |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 1509 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1510 | \end{itemize} |
| 1511 | |
| 1512 | |
| 1513 | %====================================================================== |
| 1514 | \subsection{Port-Specific Changes} |
| 1515 | |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 1516 | \begin{itemize} |
| 1517 | |
| 1518 | \item The Windows port now builds under MSVC++ 7.1 as well as version 6. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1519 | (Contributed by Martin von Loewis.) |
Raymond Hettinger | 97ef8de | 2004-01-05 00:29:57 +0000 | [diff] [blame] | 1520 | |
| 1521 | \end{itemize} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1522 | |
| 1523 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1524 | |
| 1525 | %====================================================================== |
| 1526 | \section{Porting to Python 2.4} |
| 1527 | |
| 1528 | This section lists previously described changes that may require |
| 1529 | changes to your code: |
| 1530 | |
| 1531 | \begin{itemize} |
| 1532 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 1533 | \item The \function{zip()} built-in function and \function{itertools.izip()} |
| 1534 | now return an empty list instead of raising a \exception{TypeError} |
| 1535 | exception if called with no arguments. |
Andrew M. Kuchling | 7642f7a | 2004-09-13 15:06:50 +0000 | [diff] [blame] | 1536 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 1537 | |
| 1538 | \item \function{dircache.listdir()} now passes exceptions to the caller |
| 1539 | instead of returning empty lists. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1540 | |
Andrew M. Kuchling | 71432f1 | 2004-07-05 01:40:07 +0000 | [diff] [blame] | 1541 | \item \function{LexicalHandler.startDTD()} used to receive the public and |
| 1542 | system IDs in the wrong order. This has been corrected; applications |
Fred Drake | 56fcc23 | 2004-05-06 02:55:35 +0000 | [diff] [blame] | 1543 | relying on the wrong order need to be fixed. |
Martin v. Löwis | 456ab1d | 2004-05-06 01:54:36 +0000 | [diff] [blame] | 1544 | |
Andrew M. Kuchling | 71432f1 | 2004-07-05 01:40:07 +0000 | [diff] [blame] | 1545 | \item \function{fcntl.ioctl} now warns if the \var{mutate} |
| 1546 | argument is omitted and relevant. |
Martin v. Löwis | 77ca6c4 | 2004-06-03 12:47:26 +0000 | [diff] [blame] | 1547 | |
Andrew M. Kuchling | 87c98b2 | 2004-08-25 13:38:46 +0000 | [diff] [blame] | 1548 | \item The \module{tarfile} module now generates GNU-format tar files |
| 1549 | by default. |
| 1550 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1551 | \end{itemize} |
| 1552 | |
| 1553 | |
| 1554 | %====================================================================== |
| 1555 | \section{Acknowledgements \label{acks}} |
| 1556 | |
| 1557 | The author would like to thank the following people for offering |
| 1558 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | b6ffc27 | 2004-10-12 16:36:57 +0000 | [diff] [blame^] | 1559 | article: Hye-Shik Chang, Michael Dyck, Raymond Hettinger, Hamish Lawson, |
| 1560 | Fredrik Lundh. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1561 | |
| 1562 | \end{document} |