Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | **************************** |
| 2 | What's New in Python 3.0 |
| 3 | **************************** |
| 4 | |
Andrew M. Kuchling | bbb809e | 2007-09-01 19:26:28 +0000 | [diff] [blame] | 5 | :Author: Guido van Rossum |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 6 | :Release: 0.1 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 8 | .. Rules for maintenance: |
| 9 | |
| 10 | * Anyone can add text to this document. Do not spend very much time |
| 11 | on the wording of your changes, because your text will probably |
| 12 | get rewritten to some degree. |
| 13 | |
| 14 | * The maintainer will go through Misc/NEWS periodically and add |
| 15 | changes; it's therefore more important to add your changes to |
| 16 | Misc/NEWS than to this file. |
| 17 | |
| 18 | * This is not a complete list of every single change; completeness |
| 19 | is the purpose of Misc/NEWS. Some changes I consider too small |
| 20 | or esoteric to include. If such a change is added to the text, |
| 21 | I'll just remove it. (This is another reason you shouldn't spend |
| 22 | too much time on writing your addition.) |
| 23 | |
| 24 | * If you want to draw your new text to the attention of the |
| 25 | maintainer, add 'XXX' to the beginning of the paragraph or |
| 26 | section. |
| 27 | |
| 28 | * It's OK to just add a fragmentary note about a change. For |
| 29 | example: "XXX Describe the transmogrify() function added to the |
| 30 | socket module." The maintainer will research the change and |
| 31 | write the necessary text. |
| 32 | |
| 33 | * You can comment out your additions if you like, but it's not |
| 34 | necessary (especially when a final release is some months away). |
| 35 | |
| 36 | * Credit the author of a patch or bugfix. Just the name is |
| 37 | sufficient; the e-mail address isn't necessary. |
| 38 | |
| 39 | * It's helpful to add the bug/patch number as a comment: |
| 40 | |
| 41 | % Patch 12345 |
| 42 | XXX Describe the transmogrify() function added to the socket |
| 43 | module. |
| 44 | (Contributed by P.Y. Developer.) |
| 45 | |
| 46 | This saves the maintainer the effort of going through the SVN log |
| 47 | when researching a change. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 49 | This article explains the new features in Python 3.0, comparing to 2.6 |
| 50 | (or in some cases 2.5, since 2.6 isn't released yet). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 52 | The best estimate for a release date is August 2008. |
| 53 | |
| 54 | This article doesn't attempt to provide a complete specification of |
| 55 | the new features, but instead provides a convenient overview. For |
| 56 | full details, you should refer to the documentation for Python 3.0. If |
| 57 | you want to understand the complete implementation and design |
| 58 | rationale, refer to the PEP for a particular new feature. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 60 | .. Compare with previous release in 2 - 3 sentences here. |
| 61 | .. add hyperlink when the documentation becomes available online. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 63 | .. ====================================================================== |
| 64 | .. Large, PEP-level features and changes should be described here. |
| 65 | .. Should there be a new section here for 3k migration? |
| 66 | .. Or perhaps a more general section describing module changes/deprecation? |
| 67 | .. sets module deprecated |
| 68 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
| 70 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 71 | Common Stumbling Blocks |
| 72 | ======================= |
| 73 | |
| 74 | This section briefly lists the changes that are more likely to trip |
| 75 | people up, without necessarily raising obvious errors. These are all |
| 76 | explained in more detail below. (I'm not listing syntactic changes |
| 77 | and removed or renamed features here, since those tend to produce hard |
| 78 | and fast errors; it's the subtle behavioral changes in code that |
| 79 | remains syntactically valid that trips people up. I'm also omitting |
| 80 | changes to rarely used features.) |
| 81 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 82 | * The ``print`` statement has been replaced with a :func:`print` function, |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 83 | with keyword arguments to replace most of the special syntax of the |
| 84 | old ``print`` statement (PEP 3105). Examples:: |
| 85 | |
| 86 | Old: print "The answer is", 2*2 |
| 87 | New: print("The answer is", 2*2) |
| 88 | |
| 89 | Old: print x, # Trailing comma suppresses newline |
| 90 | New: print(x, end=" ") # Appends a space instead of a newline |
| 91 | |
| 92 | Old: print # Prints a newline |
| 93 | New: print() # You must call the function! |
| 94 | |
| 95 | Old: print >>sys.stderr, "fatal error" |
| 96 | New: print("fatal error", file=sys.stderr) |
| 97 | |
| 98 | Old: print (x, y) # prints repr((x, y)) |
| 99 | New: print((x, y)) # Not the same as print(x, y)! |
| 100 | |
| 101 | You can also customize the separator between items, e.g.:: |
| 102 | |
| 103 | print("There are <", 2**32, "> possibilities!", sep="") |
| 104 | |
| 105 | which produces:: |
| 106 | |
| 107 | There are <4294967296> possibilities! |
| 108 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 109 | Notes about the :func:`print` function: |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 110 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 111 | * The :func:`print` function doesn't support the "softspace" feature of |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 112 | the old ``print`` statement. For example, in Python 2.x, |
| 113 | ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0, |
| 114 | ``print("A\n", "B")`` writes ``"A\n B\n"``. |
| 115 | |
| 116 | * Initially, you'll be finding yourself typing the old ``print x`` |
| 117 | a lot in interactive mode. Time to retrain your fingers to type |
| 118 | ``print(x)`` instead! |
| 119 | |
| 120 | * When using the ``2to3`` source-to-source conversion tool, all |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 121 | ``print`` statements are autmatically converted to :func:`print` |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 122 | function calls, so this is mostly a non-issue for larger projects. |
| 123 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 124 | * Python 3.0 uses strings and bytes instead of the Unicode strings and |
| 125 | 8-bit strings. This means that pretty much all code that uses |
| 126 | Unicode, encodings or binary data in any way has to change. The |
| 127 | change is for the better, as in the 2.x world there were numerous |
| 128 | bugs having to do with mixing encoded and unencoded text. |
| 129 | |
| 130 | * Text files enforce an encoding; binary files use bytes. This means |
| 131 | that if a file is opened using an incorrect mode or encoding, I/O |
| 132 | will likely fail. |
| 133 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 134 | * :func:`map` and :func:`filter` return iterators. A quick fix is e.g. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 135 | ``list(map(...))``, but a better fix is often to use a list |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 136 | comprehension (especially when the original code uses :keyword:`lambda`). |
| 137 | Particularly tricky is :func:`map` invoked for the side effects of the |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 138 | function; the correct transformation is to use a for-loop. |
| 139 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 140 | * :class:`dict` methods :meth:`dict.keys`, :meth:`dict.items` and |
| 141 | :meth:`dict.values` return views instead of lists. For example, this no |
| 142 | longer works: ``k = d.keys(); k.sort()``. Use ``k = sorted(d)`` instead. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 143 | |
Kurt B. Kaiser | 2bc6b5e | 2008-02-13 18:03:11 +0000 | [diff] [blame] | 144 | * :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the *cmp* |
Kurt B. Kaiser | 9d0d616 | 2008-02-14 02:47:50 +0000 | [diff] [blame] | 145 | argument providing a comparison function. Use the *key* argument |
Kurt B. Kaiser | 2bc6b5e | 2008-02-13 18:03:11 +0000 | [diff] [blame] | 146 | instead. N.B. the *key* and *reverse* arguments are now "keyword-only". |
Kurt B. Kaiser | a140101 | 2008-02-13 16:09:27 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 148 | * ``1/2`` returns a float. Use ``1//2`` to get the truncating behavior. |
| 149 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 150 | * The :func:`repr` of a long integer doesn't include the trailing ``L`` |
Georg Brandl | cc595bd | 2007-12-09 09:04:01 +0000 | [diff] [blame] | 151 | anymore, so code that unconditionally strips that character will |
| 152 | chop off the last digit instead. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 154 | |
| 155 | Strings and Bytes |
| 156 | ================= |
| 157 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 158 | * There is only one string type; its name is :class:`str` but its behavior and |
| 159 | implementation are like :class:`unicode` in 2.x. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 160 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 161 | * The :class:`basestring` superclass has been removed. The ``2to3`` tool |
| 162 | replaces every occurence of :class:`basestring` with :class:`str`. |
Christian Heimes | f534f7b | 2008-01-25 11:02:28 +0000 | [diff] [blame] | 163 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 164 | * PEP 3137: There is a new type, :class:`bytes`, to represent binary data (and |
| 165 | encoded text, which is treated as binary data until you decide to decode it). |
| 166 | The :class:`str` and :class:`bytes` types cannot be mixed; you must always |
| 167 | explicitly convert between them, using the :meth:`str.encode` (str -> bytes) |
| 168 | or :meth:`bytes.decode` (bytes -> str) methods. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 169 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 170 | .. XXX add bytearray |
| 171 | |
| 172 | * PEP 3112: Bytes literals, e.g. ``b"abc"``, create :class:`bytes` instances. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 173 | |
| 174 | * PEP 3120: UTF-8 default source encoding. |
| 175 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 176 | * PEP 3131: Non-ASCII identifiers. (However, the standard library remains |
| 177 | ASCII-only with the exception of contributor names in comments.) |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 178 | |
| 179 | * PEP 3116: New I/O Implementation. The API is nearly 100% backwards |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 180 | compatible, but completely reimplemented (currently mostly in Python). Also, |
| 181 | binary files use bytes instead of strings. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 182 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 183 | * The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead, import |
| 184 | :class:`io.StringIO` or :class:`io.BytesIO`. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | PEP 3101: A New Approach to String Formatting |
| 188 | ============================================= |
| 189 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 190 | .. XXX expand this |
| 191 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 192 | * A new system for built-in string formatting operations replaces the ``%`` |
| 193 | string formatting operator. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 194 | |
| 195 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 196 | PEP 3106: Revamping dict :meth:`dict.keys`, :meth:`dict.items` and :meth:`dict.values` |
| 197 | ====================================================================================== |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 198 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 199 | .. XXX expand this |
| 200 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 201 | * The :meth:`dict.iterkeys`, :meth:`dict.itervalues` and :meth:`dict.iteritems` |
| 202 | methods have been removed. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 203 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 204 | * :meth:`dict.keys`, :meth:`dict.values` and :meth:`dict.items` return objects |
| 205 | with set behavior that reference the underlying dict. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 206 | |
| 207 | |
| 208 | PEP 3107: Function Annotations |
| 209 | ============================== |
| 210 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 211 | .. XXX expand this |
| 212 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 213 | * A standardized way of annotating a function's parameters and return values. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 214 | |
| 215 | |
| 216 | Exception Stuff |
| 217 | =============== |
| 218 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 219 | * PEP 352: Exceptions must derive from :exc:`BaseException`. This is the root |
| 220 | of the exception hierarchy. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 221 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 222 | * :exc:`StandardError` was removed (already in 2.6). |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 223 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 224 | * Dropping sequence behavior (slicing!) and :attr:`message` attribute of |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 225 | exception instances. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 226 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 227 | * PEP 3109: Raising exceptions. You must now use ``raise Exception(args)`` |
| 228 | instead of ``raise Exception, args``. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 229 | |
| 230 | * PEP 3110: Catching exceptions. |
| 231 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 232 | * PEP 3134: Exception chaining. (The :attr:`__context__` feature from the PEP |
| 233 | hasn't been implemented yet in 3.0a2.) |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 234 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 235 | * A few exception messages are improved when Windows fails to load an extension |
| 236 | module. For example, ``error code 193`` is now ``%1 is not a valid Win32 |
| 237 | application``. Strings now deal with non-English locales. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 239 | |
| 240 | New Class and Metaclass Stuff |
| 241 | ============================= |
| 242 | |
| 243 | * Classic classes are gone. |
| 244 | |
| 245 | * PEP 3115: New Metaclass Syntax. |
| 246 | |
Skip Montanaro | a86f5d4 | 2007-09-04 02:48:01 +0000 | [diff] [blame] | 247 | * PEP 3119: Abstract Base Classes (ABCs); ``@abstractmethod`` and |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 248 | ``@abstractproperty`` decorators; collection ABCs. |
| 249 | |
| 250 | * PEP 3129: Class decorators. |
| 251 | |
| 252 | * PEP 3141: Numeric ABCs. |
| 253 | |
| 254 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | Other Language Changes |
| 256 | ====================== |
| 257 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 258 | Here are most of the changes that Python 3.0 makes to the core Python |
| 259 | language and built-in functions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 261 | * Removed backticks (use :func:`repr` instead). |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 262 | |
| 263 | * Removed ``<>`` (use ``!=`` instead). |
| 264 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 265 | * ``!=`` now returns the opposite of ``==``, unless ``==`` returns |
| 266 | ``NotImplemented``. |
| 267 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 268 | * :keyword:`as` and :keyword:`with` are keywords. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 269 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 270 | * ``True``, ``False``, and ``None`` are keywords. |
| 271 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 272 | * PEP 237: :class:`long` renamed to :class:`int`. That is, there is only one |
| 273 | built-in integral type, named :class:`int`; but it behaves like the old |
| 274 | :class:`long` type, with the exception that the literal suffix ``L`` is |
| 275 | neither supported by the parser nor produced by :func:`repr` anymore. |
| 276 | :data:`sys.maxint` was also removed since the int type has no maximum value |
| 277 | anymore. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 278 | |
| 279 | * PEP 238: int division returns a float. |
| 280 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 281 | * The ordering operators behave differently: for example, ``x < y`` where ``x`` |
| 282 | and ``y`` have incompatible types raises :exc:`TypeError` instead of returning |
| 283 | a pseudo-random boolean. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 284 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 285 | * :meth:`__getslice__` and friends killed. The syntax ``a[i:j]`` now translates |
| 286 | to ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or |
| 287 | :meth:`__delitem__`, depending on context). |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 288 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 289 | * PEP 3102: Keyword-only arguments. Named parameters occurring after ``*args`` |
| 290 | in the parameter list *must* be specified using keyword syntax in the call. |
| 291 | You can also use a bare ``*`` in the parameter list to indicate that you don't |
| 292 | accept a variable-length argument list, but you do have keyword-only |
| 293 | arguments. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 294 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 295 | * PEP 3104: :keyword:`nonlocal` statement. Using ``nonlocal x`` you can now |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 296 | assign directly to a variable in an outer (but non-global) scope. |
| 297 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 298 | * PEP 3111: :func:`raw_input` renamed to :func:`input`. That is, the new |
| 299 | :func:`input` function reads a line from :data:`sys.stdin` and returns it with |
| 300 | the trailing newline stripped. It raises :exc:`EOFError` if the input is |
| 301 | terminated prematurely. To get the old behavior of :func:`input`, use |
| 302 | ``eval(input())``. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 303 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 304 | * :func:`xrange` renamed to :func:`range`. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 305 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 306 | * PEP 3113: Tuple parameter unpacking removed. You can no longer write ``def |
| 307 | foo(a, (b, c)): ...``. Use ``def foo(a, b_c): b, c = b_c`` instead. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 308 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 309 | * PEP 3114: ``.next()`` renamed to :meth:`__next__`, new builtin :func:`next` to |
| 310 | call the :meth:`__next__` method on an object. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 311 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 312 | * PEP 3127: New octal literals; binary literals and :func:`bin`. Instead of |
| 313 | ``0666``, you write ``0o666``. The :func:`oct` function is modified |
| 314 | accordingly. Also, ``0b1010`` equals 10, and ``bin(10)`` returns |
| 315 | ``"0b1010"``. ``0666`` is now a :exc:`SyntaxError`. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 316 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 317 | * PEP 3132: Extended Iterable Unpacking. You can now write things like ``a, b, |
| 318 | *rest = some_sequence``. And even ``*rest, a = stuff``. The ``rest`` object |
| 319 | is always a list; the right-hand side may be any iterable. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 320 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 321 | * PEP 3135: New :func:`super`. You can now invoke :func:`super` without |
| 322 | arguments and the right class and instance will automatically be chosen. With |
| 323 | arguments, its behavior is unchanged. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 324 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 325 | * :func:`zip`, :func:`map` and :func:`filter` return iterators. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 326 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 327 | * :data:`string.letters` and its friends (:data:`string.lowercase` and |
| 328 | :data:`string.uppercase`) are gone. Use :data:`string.ascii_letters` |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 329 | etc. instead. |
| 330 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 331 | * Removed: :func:`apply`, :func:`callable`, :func:`coerce`, :func:`execfile`, |
| 332 | :func:`file`, :func:`reduce`, :func:`reload`. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 333 | |
Georg Brandl | 0327e60 | 2008-02-18 22:20:55 +0000 | [diff] [blame^] | 334 | * Removed: :meth:`dict.has_key` -- use the ``in`` operator instead. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 335 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 336 | * :func:`exec` is now a function. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 337 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 338 | * There is a new free format floating point representation, which is based on |
| 339 | "Floating-Point Printer Sample Code", by Robert G. Burger. ``repr(11./5)`` |
| 340 | now returns ``2.2`` instead of ``2.2000000000000002``. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 341 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 342 | * The :meth:`__oct__` and :meth:`__hex__` special methods are removed -- |
| 343 | :func:`oct` and :func:`hex` use :meth:`__index__` now to convert the argument |
| 344 | to an integer. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 345 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 346 | * Support is removed for :attr:`__members__` and :attr:`__methods__`. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 347 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 348 | * Renamed the boolean conversion C-level slot and method: ``nb_nonzero`` is now |
| 349 | ``nb_bool`` and :meth:`__nonzero__` is now :meth:`__bool__`. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 350 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 351 | * Removed :data:`sys.maxint`. Use :data:`sys.maxsize`. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 352 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 354 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | |
| 356 | |
| 357 | Optimizations |
| 358 | ------------- |
| 359 | |
| 360 | * Detailed changes are listed here. |
| 361 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 362 | The net result of the 3.0 generalizations is that Python 3.0 runs the pystone |
| 363 | benchmark around 33% slower than Python 2.5. There's room for improvement; we |
| 364 | expect to be optimizing string and integer operations significantly before the |
| 365 | final 3.0 release! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 366 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 367 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 368 | |
| 369 | |
| 370 | New, Improved, and Deprecated Modules |
| 371 | ===================================== |
| 372 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 373 | As usual, Python's standard library received a number of enhancements and bug |
| 374 | fixes. Here's a partial list of the most notable changes, sorted alphabetically |
| 375 | by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more |
| 376 | complete list of changes, or look through the Subversion logs for all the |
| 377 | details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 378 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 379 | * The :mod:`cPickle` module is gone. Use :mod:`pickle` instead. Eventually |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 380 | we'll have a transparent accelerator module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 381 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 382 | * The :mod:`imageop` module is gone. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 383 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 384 | * The :mod:`audiodev`, :mod:`Bastion`, :mod:`bsddb185`, :mod:`exceptions`, |
| 385 | :mod:`linuxaudiodev`, :mod:`md5`, :mod:`MimeWriter`, :mod:`mimify`, |
| 386 | :mod:`popen2`, :mod:`rexec`, :mod:`sets`, :mod:`sha`, :mod:`stringold`, |
| 387 | :mod:`strop`, :mod:`sunaudiodev`, :mod:`timing`, and :mod:`xmllib` modules are |
| 388 | gone. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 389 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 390 | * The :mod:`new` module is gone. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 391 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 392 | * The functions :func:`os.tmpnam`, :func:`os.tempnam` and :func:`os.tmpfile` |
| 393 | have been removed in favor of the :mod:`tempfile` module. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 394 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 395 | .. ====================================================================== |
| 396 | .. whole new modules get described in subsections here |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 397 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 398 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 399 | |
| 400 | |
| 401 | Build and C API Changes |
| 402 | ======================= |
| 403 | |
| 404 | Changes to Python's build process and to the C API include: |
| 405 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 406 | * PEP 3118: New Buffer API. |
| 407 | |
| 408 | * PEP 3121: Extension Module Initialization & Finalization. |
| 409 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 410 | * PEP 3123: Making :cmacro:`PyObject_HEAD` conform to standard C. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 412 | * No more C API support for restricted execution. |
| 413 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 414 | * :cfunc:`PyNumber_Coerce`, :cfunc:`PyNumber_CoerceEx`, :cfunc:`PyMember_Get`, |
| 415 | and :cfunc:`PyMember_Set` C APIs are removed. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 416 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 417 | * New C API :cfunc:`PyImport_ImportModuleNoBlock`, works like |
| 418 | :cfunc:`PyImport_ImportModule` but won't block on the import lock (returning |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 419 | an error instead). |
| 420 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 421 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 422 | |
| 423 | |
| 424 | Port-Specific Changes |
| 425 | --------------------- |
| 426 | |
| 427 | Platform-specific changes go here. |
| 428 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 429 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 430 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | |
| 432 | |
| 433 | .. _section-other: |
| 434 | |
| 435 | Other Changes and Fixes |
| 436 | ======================= |
| 437 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 438 | As usual, there were a bunch of other improvements and bugfixes |
| 439 | scattered throughout the source tree. A search through the change |
| 440 | logs finds there were XXX patches applied and YYY bugs fixed between |
| 441 | Python 2.6 and 3.0. Both figures are likely to be underestimates. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 442 | |
| 443 | Some of the more notable changes are: |
| 444 | |
| 445 | * Details go here. |
| 446 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 447 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 | |
| 449 | |
| 450 | Porting to Python 3.0 |
| 451 | ===================== |
| 452 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 453 | This section lists previously described changes that may require |
| 454 | changes to your code: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 | |
| 456 | * Everything is all in the details! |
| 457 | |
Georg Brandl | 7274858 | 2008-01-20 10:59:44 +0000 | [diff] [blame] | 458 | * Developers can include :file:`intobject.h` after :file:`Python.h` for |
| 459 | some ``PyInt_`` aliases. |
Christian Heimes | f78b1c6 | 2007-12-02 16:52:32 +0000 | [diff] [blame] | 460 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 461 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | |
| 463 | |
| 464 | .. _acks: |
| 465 | |
| 466 | Acknowledgements |
| 467 | ================ |
| 468 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 469 | The author would like to thank the following people for offering |
| 470 | suggestions, corrections and assistance with various drafts of this |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 471 | article: Georg Brandl. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 472 | |