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