8000 Add str_object.rst to "What Else" chapter · thecodingchicken/python-future@82ae481 · GitHub
[go: up one dir, main page]

Skip to content

Commit 82ae481

Browse files
committed
Add str_object.rst to "What Else" chapter
1 parent d1d2d65 commit 82ae481

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

docs/source/str_object.rst

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ str
44
-----
55

66
The :class:`str` object in Python 3 is quite similar but not identical to the
7-
Python 2 :class:`unicode` object. The major differences are:
7+
Python 2 :class:`unicode` object.
88

9-
- repr of unicode strings in Py2 is "u'...'" versus "'...'"
10-
- stricter type-checking in Py3 to enforce the distinction between unicode
11-
strings and byte-strings, such as when comparing, concatenating, joining, or
12-
replacing parts of strings.
9+
The major difference is the stricter type-checking with Py3 ``str`` to enforce
10+
the distinction between unicode strings and byte-strings, such as when
11+
comparing, concatenating, joining, or replacing parts of strings.
12+
13+
There are also minor differences, such as the ``repr`` of unicode strings in
14+
Py2 having a ``"u'...'"`` prefix, versus simply ``"'...'"``, and the removal of
15+
the :func:`str.decode` method in Py3.
1316

1417
``future`` contains a backport of the :mod:`str` object from Python 3 which
1518
inherits from the Python 2 :class:`unicode` class but has customizations to
@@ -46,8 +49,9 @@ behaviours to Python 3's :class:`str`::
4649
>>> assert list(s) == ['A', 'B', 'C', 'D']
4750
>>> assert s.split('B') == ['A', 'CD']
4851

49-
Currently the easiest way to ensure identical use of strings in a Py3/2
50-
codebase is to wrap string literals in a :func:`~str` call, as follows::
52+
Currently the easiest way to ensure identical use of (unicode) strings across
53+
Py3 and Py2 in a single-source codebase is to wrap string literals in a
54+
:func:`~str` call, as follows::
5155
5256
from __future__ import unicode_literals
5357
from future.builtins import *
@@ -59,7 +63,7 @@ codebase is to wrap string literals in a :func:`~str` call, as follows::
5963
# ...
6064

6165
Most of the time this is unnecessary, but the stricter type-checking of the
62-
``future.builtins.str`` object, in particular, may be useful while ensuring
63-
that Py2's unicode strings are not mixed up with implicit coercions from
64-
byte-strings.
66+
``future.builtins.str`` object, in particular, may be useful for ensuring
67+
the same consistent separation between unicode and byte strings on Py2 as on
68+
Py3.
6569

docs/source/what_else.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Essentials
1313
==========
1414

1515
.. include:: bytes_object.rst
16+
.. include:: str_object.rst
1617
.. include:: dict_methods.rst
1718
.. include:: custom_str_methods.rst
1819
.. include:: custom_iterators.rst

0 commit comments

Comments
 (0)
0