Guido van Rossum | f2ffce0 | 2000-09-05 04:38:34 +0000 | [diff] [blame] | 1 | What's New in Python 2.0b1? |
Guido van Rossum | 6100033 | 1997-08-15 04:39:58 +0000 | [diff] [blame] | 2 | =========================== |
| 3 | |
Guido van Rossum | 8ed602b | 2000-09-01 22:34:33 +0000 | [diff] [blame] | 4 | Below is a list of all relevant changes since release 1.6. Older |
Guido van Rossum | f2ffce0 | 2000-09-05 04:38:34 +0000 | [diff] [blame] | 5 | changes are in the file HISTORY. If you are making the jump directly |
| 6 | from Python 1.5.2 to 2.0, make sure to read the section for 1.6 in the |
| 7 | HISTORY file! Many important changes listed there. |
Guido van Rossum | 6100033 | 1997-08-15 04:39:58 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | f2ffce0 | 2000-09-05 04:38:34 +0000 | [diff] [blame] | 9 | Alternatively, a good overview of the changes between 1.5.2 and 2.0 is |
| 10 | the document "What's New in Python 2.0" by Kuchling and Moshe Zadka: |
| 11 | http://starship.python.net/crew/amk/python/writing/new-python/. |
Guido van Rossum | 1f83cce | 1997-10-06 21:04:35 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | f2ffce0 | 2000-09-05 04:38:34 +0000 | [diff] [blame] | 13 | --Guido van Rossum (home page: http://www.pythonlabs.com/~guido/) |
Guido van Rossum | 437cfe8 | 1999-04-08 20:17:57 +0000 | [diff] [blame] | 14 | |
| 15 | ====================================================================== |
| 16 | |
Guido van Rossum | f2ffce0 | 2000-09-05 04:38:34 +0000 | [diff] [blame] | 17 | Source Incompatibilities |
| 18 | ------------------------ |
| 19 | |
| 20 | None. Note that 1.6 introduced several incompatibilities with 1.5.2, |
| 21 | such as single-argument append(), connect() and bind(), and changes to |
| 22 | str(long) and repr(float). |
| 23 | |
| 24 | |
| 25 | Binary Incompatibilities |
| 26 | ------------------------ |
| 27 | |
| 28 | - Third party extensions built for Python 1.5.x or 1.6 cannot be used |
| 29 | with Python 2.0; these extensions will have to be rebuilt for Python |
| 30 | 2.0. |
| 31 | |
| 32 | - On Windows, attempting to import a third party extension built for |
| 33 | Python 1.5.x or 1.6 results in an immediate crash; there's not much we |
| 34 | can do about this. Check your PYTHONPATH environment variable! |
| 35 | |
| 36 | - Python bytecode files (*.pyc and *.pyo) are not compatible between |
| 37 | releases. |
| 38 | |
| 39 | |
| 40 | Overview of Changes Since 1.6 |
| 41 | ----------------------------- |
| 42 | |
| 43 | There are many new modules (including brand new XML support through |
| 44 | the xml package, and i18n support through the gettext module); a list |
| 45 | of all new modules is included below. Lots of bugs have been fixed. |
| 46 | |
| 47 | There are several important syntax enhancements, described in more |
| 48 | detail below: |
| 49 | |
| 50 | - Augmented assignment, e.g. x += 1 |
| 51 | |
| 52 | - List comprehensions, e.g. [x**2 for x in range(10)] |
| 53 | |
| 54 | - Extended import statement, e.g. import Module as Name |
| 55 | |
| 56 | - Extended print statement, e.g. print >> file, "Hello" |
| 57 | |
| 58 | Other important changes: |
| 59 | |
| 60 | - Optional collection of cyclical garbage |
| 61 | |
| 62 | |
| 63 | Augmented Assignment |
| 64 | -------------------- |
| 65 | |
| 66 | This must have been the most-requested feature of the past years! |
| 67 | Eleven new assignment operators were added: |
| 68 | |
Guido van Rossum | e905e95 | 2000-09-05 12:42:46 +0000 | [diff] [blame] | 69 | += -= *= /= %= **= <<= >>= &= ^= |= |
Guido van Rossum | f2ffce0 | 2000-09-05 04:38:34 +0000 | [diff] [blame] | 70 | |
| 71 | For example, |
| 72 | |
| 73 | A += B |
| 74 | |
| 75 | is similar to |
| 76 | |
| 77 | A = A + B |
| 78 | |
| 79 | except that A is evaluated only once (relevant when A is something |
| 80 | like dict[index].attr). |
| 81 | |
| 82 | However, if A is a mutable object, A may be modified in place. Thus, |
| 83 | if A is a number or a string, A += B has the same effect as A = A+B |
| 84 | (except A is only evaluated once); but if a is a list, A += B has the |
| 85 | same effect as A.extend(B)! |
| 86 | |
| 87 | Classes and built-in object types can override the new operators in |
| 88 | order to implement the in-place behavior; the not-in-place behavior is |
| 89 | used automatically as a fallback when an object doesn't implement the |
| 90 | in-place behavior. For classes, the method name is derived from the |
| 91 | method name for the corresponding not-in-place operator by inserting |
| 92 | an 'i' in front of the name, e.g. __iadd__ implements in-place |
| 93 | __add__. |
| 94 | |
| 95 | Augmented assignment was implemented by Thomas Wouters. |
| 96 | |
| 97 | |
| 98 | List Comprehensions |
| 99 | ------------------- |
| 100 | |
| 101 | This is a flexible new notation for lists whose elements are computed |
| 102 | from another list (or lists). The simplest form is: |
| 103 | |
| 104 | [<expression> for <variable> in <sequence>] |
| 105 | |
| 106 | For example, [x**2 for i in range(4)] yields the list [0, 1, 4, 9]. |
| 107 | This is more efficient than map() with a lambda. |
| 108 | |
| 109 | You can also add a condition: |
| 110 | |
| 111 | [<expression> for <variable> in <sequence> if <condition>] |
| 112 | |
| 113 | For example, [w for w in words if w == w.lower()] would yield the list |
| 114 | of words that contain no uppercase characters. This is more efficient |
| 115 | than filter() with a lambda. |
| 116 | |
| 117 | You can also have nested for loops and more than one 'if' clause. For |
| 118 | example, here's a function that flattens a sequence of sequences:: |
| 119 | |
| 120 | def flatten(seq): |
| 121 | return [x for subseq in seq for x in subseq] |
| 122 | |
| 123 | flatten([[0], [1,2,3], [4,5], [6,7,8,9], []]) |
| 124 | |
| 125 | This prints |
| 126 | |
| 127 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 128 | |
| 129 | List comprehensions originated as a patch set from Greg Ewing; Skip |
| 130 | Montanaro and Thomas Wouters also contributed. |
| 131 | |
| 132 | |
| 133 | Extended Import Statement |
| 134 | ------------------------- |
| 135 | |
| 136 | Many people have asked for a way to import a module under a different |
| 137 | name. This can be accomplished like this: |
| 138 | |
| 139 | import foo |
| 140 | bar = foo |
| 141 | del foo |
| 142 | |
| 143 | but this common idiom gets old quickly. A simple extension of the |
| 144 | import statement now allows this to be written as follows: |
| 145 | |
| 146 | import foo as bar |
| 147 | |
| 148 | There's also a variant for 'from ... import': |
| 149 | |
| 150 | from foo import bar as spam |
| 151 | |
| 152 | This also works with packages; e.g. you can write this: |
| 153 | |
| 154 | import test.regrtest as regrtest |
| 155 | |
| 156 | Note that 'as' is not a new keyword -- it is recognized only in this |
| 157 | context (this is only possible because the syntax for the import |
| 158 | statement doesn't involve expressions). |
| 159 | |
| 160 | Implemented by Thomas Wouters. |
| 161 | |
| 162 | |
| 163 | Extended Print Statement |
| 164 | ------------------------ |
| 165 | |
| 166 | Easily the most controversial new feature, this extension to the print |
| 167 | statement adds an option to make the output go to a different file |
| 168 | than the default sys.stdout. |
| 169 | |
| 170 | For example, to write an error message to sys.stderr, you can now |
| 171 | write: |
| 172 | |
| 173 | print >> sys.stderr, "Error: bad dog!" |
| 174 | |
| 175 | As a special feature, if the expression used to indicate the file |
| 176 | evaluates to None, the current value of sys.stdout used. Thus: |
| 177 | |
| 178 | print >> None, "Hello world" |
| 179 | |
| 180 | is equivalent to |
| 181 | |
| 182 | print "Hello world" |
| 183 | |
| 184 | Design and implementation by Barry Warsaw. |
| 185 | |
| 186 | |
| 187 | Optional Collection of Cyclical Garbage |
| 188 | --------------------------------------- |
| 189 | |
| 190 | Python is now equipped with a garbage collector that can hunt down |
| 191 | cyclical references between Python objects. It's no replacement for |
| 192 | reference counting; in fact, it depends on the reference counts being |
| 193 | correct, and decides that a set of objects belong to a cycle if all |
| 194 | their reference counts can be accounted for from their references to |
| 195 | each other. This devious scheme was first proposed by Eric Tiedemann, |
| 196 | and brought to implementation by Neil Schemenauer. |
| 197 | |
| 198 | There's a module "gc" that lets you control some parameters of the |
| 199 | garbage collection. There's also an option to the configure script |
| 200 | that lets you enable or disable the garbage collection. In 2.0b1, |
| 201 | it's on by default, so that we (hopefully) can collect decent user |
| 202 | experience with this new feature. There are some questions about its |
| 203 | performance. if it proves to be too much of a problem, we'll turn it |
| 204 | off by default in the final 2.0 release. |
| 205 | |
| 206 | |
| 207 | Smaller Changes |
| 208 | --------------- |
| 209 | |
| 210 | A new function zip() was added. zip(seq1, seq2, ...) is equivalent to |
| 211 | map(None, seq1, seq2, ...) when the sequences have the same length; |
| 212 | i.e. zip([1,2,3], [10,20,30]) returns [(1,10), (2,20), (3,30)]. When |
| 213 | the lists are not all the same length, the shortest list wins: |
| 214 | zip([1,2,3], [10,20]) returns [(1,10), (2,20)]. |
| 215 | |
| 216 | sys.version_info is a tuple (major, minor, micro, level, serial). |
| 217 | |
| 218 | Dictionaries have an odd new method, setdefault(key, default). |
| 219 | dict.setdefault(key, default) returns dict[key] if it exists; if not, |
| 220 | it sets dict[key] to default and returns that value. Thus: |
| 221 | |
| 222 | dict.setdefault(key, []).append(item) |
| 223 | |
| 224 | does the same work as this common idiom: |
| 225 | |
| 226 | if not dict.has_key(key): |
| 227 | dict[key] = [] |
| 228 | dict[key].append(item) |
| 229 | |
| 230 | |
| 231 | New Modules and Packages |
| 232 | ------------------------ |
| 233 | |
| 234 | atexit - for registering functions to be called when Python exits. |
| 235 | |
| 236 | imputil - Greg Stein's alternative API for writing custom import |
| 237 | hooks. |
| 238 | |
| 239 | pyexpat - an interface to the Expat XML parser, contributed by Paul |
| 240 | Prescod. |
| 241 | |
| 242 | xml - a new package with XML support code organized (so far) in three |
| 243 | subpackages: xml.dom, xml.sax, and xml.parsers. Describing these |
| 244 | would fill a volume. There's a special feature whereby a |
| 245 | user-installed package named _xmlplus overrides the standard |
| 246 | xmlpackage; this is intended to give the XML SIG a hook to distribute |
| 247 | backwards-compatible updates to the standard xml package. |
| 248 | |
| 249 | webbrowser - a platform-independent API to launch a web browser. |
| 250 | |
| 251 | |
Guido van Rossum | e905e95 | 2000-09-05 12:42:46 +0000 | [diff] [blame] | 252 | Changed Modules |
| 253 | --------------- |
| 254 | |
| 255 | ftplib - ntransfercmd(), transfercmd(), and retrbinary() all now |
| 256 | optionally support the RFC 959 REST command. |
| 257 | |
| 258 | socket - new function getfqdn() |
| 259 | |
Guido van Rossum | 830ca2a | 2000-09-05 15:34:16 +0000 | [diff] [blame^] | 260 | readline - new functions to read, write and truncate history files. The |
| 261 | readline section of the library reference manual contains an example. |
| 262 | |
Guido van Rossum | e905e95 | 2000-09-05 12:42:46 +0000 | [diff] [blame] | 263 | XXX: I'm sure there are others |
| 264 | |
| 265 | |
| 266 | Obsolete Modules |
| 267 | ---------------- |
| 268 | |
| 269 | None. However note that 1.6 made a whole slew of modules obsolete: |
| 270 | stdwin, soundex, cml, cmpcache, dircache, dump, find, grep, packmail, |
| 271 | poly, zmod, strop, util, whatsound. |
| 272 | |
| 273 | |
| 274 | Changed, New, Obsolete Tools |
| 275 | ---------------------------- |
| 276 | |
| 277 | XXX: are there any? If not, say "None" here. |
| 278 | |
| 279 | |
Guido van Rossum | f2ffce0 | 2000-09-05 04:38:34 +0000 | [diff] [blame] | 280 | C-level Changes |
| 281 | --------------- |
| 282 | |
| 283 | Several cleanup jobs were carried out throughout the source code. |
| 284 | |
| 285 | All C code was converted to ANSI C; we got rid of all uses of the |
| 286 | Py_PROTO() macro, which makes the header files a lot more readable. |
| 287 | |
| 288 | Most of the portability hacks were moved to a new header file, |
| 289 | pyport.h; several other new header files were added and some old |
| 290 | header files were removed, in an attempt to create a more rational set |
| 291 | of header files. (Few of these ever need to be included explicitly; |
| 292 | they are all included by Python.h.) |
| 293 | |
| 294 | Vladimir Marangozov redesigned more rational APIs for allocating |
| 295 | memory. See pymem.h. |
| 296 | |
| 297 | Trent Mick ensured portability to 64-bit platforms, under both Linux |
| 298 | and Win64, especially for the new Intel Itanium processor. |
| 299 | |
Guido van Rossum | e905e95 | 2000-09-05 12:42:46 +0000 | [diff] [blame] | 300 | Numerous new APIs were added, e.g. |
| 301 | |
| 302 | XXX: Fill this out. |
Guido van Rossum | ab9d6f0 | 1998-08-10 22:01:13 +0000 | [diff] [blame] | 303 | |
| 304 | ====================================================================== |