@@ -129,25 +129,29 @@ Note:
129129 :func: `print ` function calls, so this is mostly a non-issue for
130130 larger projects.
131131
132- Text Strings Vs. Bytes
133- ----------------------
132+ Text Vs. Data Instead Of Unicode Vs. 8-bit
133+ ------------------------------------------
134134
135135Everything you thought you knew about binary data and Unicode has
136- changed. There's a longer section below; here's a summary of the
137- changes:
138-
139- * Python 3.0 uses *strings * and *bytes * instead of *Unicode strings *
140- and *8-bit strings *. The difference is that any attempt to mix
141- strings and bytes in Python 3.0 raises a TypeError exception,
142- whereas if you were to mix Unicode and 8-bit strings in Python 2.x,
143- you would only get an exception if the 8-bit string contained
144- non-ASCII values. As a consequence, pretty much all code that
145- uses Unicode, encodings or binary data most likely has to change.
146- The change is for the better, as in the 2.x world there were
147- numerous bugs having to do with mixing encoded and unencoded text.
148-
149- * You no longer need to use ``u"..." `` literals for Unicode text.
150- However, you must use ``b"..." `` literals for binary data.
136+ changed:
137+
138+ XXX HIRO
139+
140+ * Python 3.0 uses the concepts of *text * and (binary) *data * instead
141+ of Unicode strings and 8-bit strings. All text is Unicode; however
142+ *encoded * Unicode is represented as binary data. The type used to
143+ hold text is :class: `str `, the type used to hold data is
144+ :class: `bytes `. The difference is that any attempt to mix text and
145+ data in Python 3.0 raises a TypeError exception, whereas if you were
146+ to mix Unicode and 8-bit strings in Python 2.x, you would only get
147+ an exception if the 8-bit string contained non-ASCII values. As a
148+ consequence, pretty much all code that uses Unicode, encodings or
149+ binary data most likely has to change. The change is for the
150+ better, as in the 2.x world there were numerous bugs having to do
151+ with mixing encoded and unencoded text.
152+
153+ * You no longer use ``u"..." `` literals for Unicode text. However,
154+ you must use ``b"..." `` literals for binary data.
151155
152156* Files opened as text files (still the default mode for :func: `open `)
153157 always use an encoding to map between strings (in memory) and bytes
@@ -167,6 +171,50 @@ changes:
167171 don't have functionality enough in common to warrant a shared base
168172 class.
169173
174+ * All backslashes in raw strings are interpreted literally. This
175+ means that ``'\U' `` and ``'\u' `` escapes in raw strings are not
176+ treated specially.
177+
178+ XXX Deal with dupes below
179+
180+ * There is only one text string type; its name is :class: `str ` but its
181+ behavior and implementation are like :class: `unicode ` in 2.x.
182+
183+ * The :class: `basestring ` superclass has been removed. The ``2to3 ``
184+ tool (see below) replaces every occurrence of :class: `basestring `
185+ with :class: `str `.
186+
187+ * :pep: `3137 `: There is a new type, :class: `bytes `, to represent
188+ binary data (and encoded text, which is treated as binary data until
189+ it is decoded). The :class: `str ` and :class: `bytes ` types cannot be
190+ mixed; you must always explicitly convert between them, using the
191+ :meth: `str.encode ` (str -> bytes) or :meth: `bytes.decode ` (bytes ->
192+ str) methods.
193+
194+ * Like :class: `str `, the :class: `bytes ` type is immutable. There is a
195+ separate *mutable * type to hold buffered binary data,
196+ :class: `bytearray `. Nearly all APIs that accept :class: `bytes ` also
197+ accept :class: `bytearray `. The mutable API is based on
198+ :class: `collections.MutableSequence `.
199+
200+ * :pep: `3138 `: The :func: `repr ` of a string no longer escapes
201+ non-ASCII characters. It still escapes control characters and code
202+ points with non-printable status in the Unicode standard, however.
203+
204+ * :pep: `3120 `: The default source encoding is now UTF-8.
205+
206+ * :pep: `3131 `: Non-ASCII letters are now allowed in identifiers.
207+ (However, the standard library remains ASCII-only with the exception
208+ of contributor names in comments.)
209+
210+ * :pep: `3116 `: New I/O implementation. The API is nearly 100%
211+ backwards compatible, but completely reimplemented (currently largely
212+ in Python). Also, binary files use bytes instead of strings.
213+
214+ * The :mod: `StringIO ` and :mod: `cStringIO ` modules are gone. Instead,
215+ import :class: `io.StringIO ` or :class: `io.BytesIO `, for text and
216+ data respectively.
217+
170218* See also the :ref: `unicode-howto `, which was updated for Python 3.0.
171219
172220Views And Iterators Instead Of Lists
@@ -254,8 +302,8 @@ Overview Of Syntax Changes
254302This section gives a brief overview of every *syntactic * change in
255303Python 3.0.
256304
257- Additions
258- ---------
305+ New Syntax
306+ ----------
259307
260308* :pep: `3107 `: Function argument and return value annotations. This
261309 provides a standardized way of annotating a function's parameters
@@ -304,8 +352,8 @@ Additions
304352
305353* Bytes literals are introduced with a leading ``b `` or ``B ``.
306354
307- Changes
308- -------
355+ Changed Syntax
356+ --------------
309357
310358* New :keyword: `raise ` statement syntax: ``raise [expr [from expr]] ``.
311359 Also note that string exceptions are no longer legal (:pep: `0352 `).
@@ -333,8 +381,8 @@ Changes
333381 *must * now be spelled as ``... ``. (Previously it could also be
334382 spelled as ``. . . ``, by a mere accident of the grammar.)
335383
336- Removals
337- --------
384+ Removed Syntax
385+ --------------
338386
339387* :pep: `3113 `: Tuple parameter unpacking removed. You can no longer
340388 write ``def foo(a, (b, c)): ... ``.
@@ -362,7 +410,6 @@ Removals
362410 (:pep: `0328 `)
363411
364412
365-
366413Changes Already Present In Python 2.6
367414=====================================
368415
@@ -401,8 +448,7 @@ consulted for longer descriptions.
401448
402449* :ref: `pep-3112 `. The ``b"..." `` string literal notation (and its
403450 variants like ``b'...' ``, ``b"""...""" ``, and ``br"..." ``) now
404- produces a literal of type :class: `bytes `. More about
405- :class: `bytes ` below.
451+ produces a literal of type :class: `bytes `.
406452
407453* :ref: `pep-3116 `. The :mod: `io ` module is now the standard way of
408454 doing file I/O, and the initial values of :data: `sys.stdin `,
@@ -411,14 +457,17 @@ consulted for longer descriptions.
411457 alias for :func: `io.open ` and has additional keyword arguments
412458 *encoding *, *errors *, *newline * and *closefd *. Also note that an
413459 invalid *mode * argument now raises :exc: `ValueError `, not
414- :exc: `IOError `.
460+ :exc: `IOError `. The binary file object underlying a text file
461+ object can be accessed as :attr: `f.buffer ` (but beware that the
462+ text object maintains a buffer of itself in order to speed up
463+ the encoding and decoding operations).
415464
416465* :ref: `pep-3118 `. The old builtin :func: `buffer ` is now really gone;
417466 the new builtin :func: `memoryview ` provides (mostly) similar
418467 functionality.
419468
420469* :ref: `pep-3119 `. The :mod: `abc ` module and the ABCs defined in the
421- :mod: `collections ` module plays a slightly more prominent role in
470+ :mod: `collections ` module plays a somewhat more prominent role in
422471 the language now, and builtin collection types like :class: `dict `
423472 and :class: `list ` conform to the :class: `collections.MutableMapping `
424473 and :class: `collections.MutableSequence ` ABC, respectively.
@@ -427,11 +476,11 @@ consulted for longer descriptions.
427476 notation is the only one supported, and binary literals have been
428477 added.
429478
430- * :ref: `pep-3129 `. This speaks for itself.
479+ * :ref: `pep-3129 `.
431480
432481* :ref: `pep-3141 `. The :mod: `numbers ` module is another new use of
433482 ABCs, defining Python's "numeric tower". Also note the new
434- :mod: `fractions ` module.
483+ :mod: `fractions ` module which implements :class: ` numbers.Rational ` .
435484
436485
437486Library Changes
@@ -532,58 +581,14 @@ Some other library changes (not covered by :pep:`3108`):
532581* Cleanup of the :mod: `random ` module: removed the :func: `jumpahead ` API.
533582
534583
535- Strings And Bytes
536- =================
537-
538- This section discusses the many changes in string XXX
539-
540- * There is only one string type; its name is :class: `str ` but its behavior and
541- implementation are like :class: `unicode ` in 2.x.
542-
543- * The :class: `basestring ` superclass has been removed. The ``2to3 `` tool
544- replaces every occurrence of :class: `basestring ` with :class: `str `.
545-
546- * :pep: `3137 `: There is a new type, :class: `bytes `, to represent
547- binary data (and encoded text, which is treated as binary data until
548- you decide to decode it). The :class: `str ` and :class: `bytes ` types
549- cannot be mixed; you must always explicitly convert between them,
550- using the :meth: `str.encode ` (str -> bytes) or :meth: `bytes.decode `
551- (bytes -> str) methods.
552-
553- .. XXX add bytearray
554-
555- * All backslashes in raw strings are interpreted literally. This means that
556- ``'\U' `` and ``'\u' `` escapes in raw strings are not treated specially.
557-
558- * :pep: `3138 `: :func: `repr ` of a string no longer escapes all
559- non-ASCII characters. XXX
560-
561- * :pep: `3112 `: Bytes literals, e.g. ``b"abc" ``, create :class: `bytes `
562- instances.
563-
564- * :pep: `3120 `: UTF-8 default source encoding.
565-
566- * :pep: `3131 `: Non-ASCII identifiers. (However, the standard library remains
567- ASCII-only with the exception of contributor names in comments.)
568-
569- * :pep: `3116 `: New I/O Implementation. The API is nearly 100% backwards
570- compatible, but completely reimplemented (currently mostly in Python). Also,
571- binary files use bytes instead of strings.
572-
573- * The :mod: `StringIO ` and :mod: `cStringIO ` modules are gone. Instead, import
574- :class: `io.StringIO ` or :class: `io.BytesIO `.
575-
576-
577-
578584:pep: `3101 `: A New Approach To String Formatting
579585================================================
580586
581587* A new system for built-in string formatting operations replaces the
582588 ``% `` string formatting operator. (However, the ``% `` operator is
583589 still supported; it will be deprecated in Python 3.1 and removed
584- from the language at some later time.)
585-
586- .. XXX expand this
590+ from the language at some later time.) Read :pep: `3101 ` for the full
591+ scoop.
587592
588593
589594:pep: `3106 `: Revamping dict :meth: `dict.keys `, :meth: `dict.items ` and :meth: `dict.values `
@@ -632,16 +637,24 @@ Exception Stuff
632637New Class And Metaclass Stuff
633638=============================
634639
640+ XXX Move to new syntax section???
641+
635642* Classic classes are gone.
636643
637- * :pep: `3115 `: New Metaclass Syntax.
644+ * :pep: `3115 `: New Metaclass Syntax. Instead of::
645+
646+ class C:
647+ __metaclass__ = M
648+ ...
638649
639- * :pep: `3119 `: Abstract Base Classes (ABCs); ``@abstractmethod `` and
640- ``@abstractproperty `` decorators; collection ABCs.
650+ you now use::
641651
642- * :pep: `3129 `: Class decorators.
652+ class C(metaclass=M):
653+ ...
643654
644- * :pep: `3141 `: Numeric ABCs.
655+ The module-global :data:`__metaclass__` variable is no longer supported.
656+ (It was a crutch to make it easier to default to new-style classes
657+ without deriving every class from :class:`object`.)
645658
646659
647660Other Language Changes
0 commit comments