Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5fae0e5

Browse files
committed
Improve str() and object.__str__() documentation (issue #13538).
1 parent 9ddfb19 commit 5fae0e5

7 files changed

Lines changed: 105 additions & 54 deletions

File tree

Doc/c-api/buffer.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
.. highlightlang:: c
22

3+
.. index::
4+
single: buffer protocol
5+
single: buffer interface; (see buffer protocol)
6+
single: buffer object; (see buffer protocol)
7+
38
.. _bufferobjects:
49

510
Buffer Protocol
@@ -10,9 +15,6 @@ Buffer Protocol
1015
.. sectionauthor:: Stefan Krah
1116

1217

13-
.. index::
14-
single: buffer interface
15-
1618
Certain objects available in Python wrap access to an underlying memory
1719
array or *buffer*. Such objects include the built-in :class:`bytes` and
1820
:class:`bytearray`, and some extension types like :class:`array.array`.
@@ -24,8 +26,8 @@ characteristic of being backed by a possibly large memory buffer. It is
2426
then desirable, in some situations, to access that buffer directly and
2527
without intermediate copying.
2628

27-
Python provides such a facility at the C level in the form of the *buffer
28-
protocol*. This protocol has two sides:
29+
Python provides such a facility at the C level in the form of the :ref:`buffer
30+
protocol <bufferobjects>`. This protocol has two sides:
2931

3032
.. index:: single: PyBufferProcs
3133

Doc/library/functions.rst

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ are always available. They are listed here in alphabetical order.
534534
is used by most built-in types: :ref:`formatspec`.
535535

536536
The default *format_spec* is an empty string which usually gives the same
537-
effect as calling ``str(value)``.
537+
effect as calling :func:`str(value) <str>`.
538538

539539
A call to ``format(value, format_spec)`` is translated to
540540
``type(value).__format__(format_spec)`` which bypasses the instance
@@ -1249,37 +1249,50 @@ are always available. They are listed here in alphabetical order.
12491249
For more information on static methods, consult the documentation on the
12501250
standard type hierarchy in :ref:`types`.
12511251

1252+
.. index::
1253+
single: string; str() (built-in function)
1254+
12521255

12531256
.. _func-str:
12541257
.. function:: str(object='')
1255-
str(object[, encoding[, errors]])
1256-
1257-
Return a :ref:`string <textseq>` version of an object, using one of the
1258-
following modes:
1259-
1260-
If *encoding* and/or *errors* are given, :func:`str` will decode the
1261-
*object* which can either be a byte string or a character buffer using
1262-
the codec for *encoding*. The *encoding* parameter is a string giving
1263-
the name of an encoding; if the encoding is not known, :exc:`LookupError`
1264-
is raised. Error handling is done according to *errors*; this specifies the
1265-
treatment of characters which are invalid in the input encoding. If
1266-
*errors* is ``'strict'`` (the default), a :exc:`ValueError` is raised on
1267-
errors, while a value of ``'ignore'`` causes errors to be silently ignored,
1268-
and a value of ``'replace'`` causes the official Unicode replacement character,
1269-
U+FFFD, to be used to replace input characters which cannot be decoded.
1270-
See also the :mod:`codecs` module.
1271-
1272-
When only *object* is given, this returns its nicely printable representation.
1273-
For strings, this is the string itself. The difference with ``repr(object)``
1274-
is that ``str(object)`` does not always attempt to return a string that is
1275-
acceptable to :func:`eval`; its goal is to return a printable string.
1276-
With no arguments, this returns the empty string.
1277-
1278-
Objects can specify what ``str(object)`` returns by defining a :meth:`__str__`
1279-
special method.
1280-
1281-
For more information on strings and string methods, see the :ref:`textseq`
1282-
section. To output formatted strings, see the :ref:`string-formatting`
1258+
str(object=b'', encoding='utf-8', errors='strict')
1259+
1260+
Return a :ref:`string <textseq>` version of *object*. If *object* is not
1261+
provided, returns the empty string. Otherwise, the behavior of ``str()``
1262+
depends on whether *encoding* or *errors* is given, as follows.
1263+
1264+
If neither *encoding* nor *errors* is given, ``str(object)`` returns
1265+
:meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely
1266+
printable string representation of *object*. For string objects, this is
1267+
the string itself. If *object* does not have a :meth:`~object.__str__`
1268+
method, then :func:`str` falls back to returning
1269+
:meth:`repr(object) <repr>`.
1270+
1271+
.. index::
1272+
single: buffer protocol; str() (built-in function)
1273+
single: bytes; str() (built-in function)
1274+
1275+
If at least one of *encoding* or *errors* is given, *object* should be a
1276+
:class:`bytes` or :class:`bytearray` object, or more generally any object
1277+
that supports the :ref:`buffer protocol <bufferobjects>`. In this case, if
1278+
*object* is a :class:`bytes` (or :class:`bytearray`) object, then
1279+
``str(bytes, encoding, errors)`` is equivalent to
1280+
:meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes
1281+
object underlying the buffer object is obtained before calling
1282+
:meth:`bytes.decode`. See :ref:`binaryseq` and
1283+
:ref:`bufferobjects` for information on buffer objects.
1284+
1285+
Passing a :class:`bytes` object to :func:`str` without the *encoding*
1286+
or *errors* arguments falls under the first case of returning the informal
1287+
string representation (see also the :option:`-b` command-line option to
1288+
Python). For example::
1289+
1290+
>>> str(b'Zoot!')
1291+
"b'Zoot!'"
1292+
1293+
``str`` is a built-in :term:`type`. For more information on the string
1294+
type and its methods, see the :ref:`textseq` and :ref:`string-methods`
1295+
sections. To output formatted strings, see the :ref:`string-formatting`
12831296
section. In addition, see the :ref:`stringservices` section.
12841297

12851298

Doc/library/stdtypes.rst

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,19 +1346,18 @@ range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.)
13461346
The :attr:`start`, :attr:`stop` and :attr:`step` attributes.
13471347

13481348

1349+
.. index::
1350+
single: string; text sequence type
1351+
single: str() (built-in function); (see also string)
1352+
object: string
1353+
13491354
.. _textseq:
13501355

13511356
Text Sequence Type --- :class:`str`
13521357
===================================
13531358

1354-
.. index::
1355-
object: string
1356-
object: bytes
1357-
object: bytearray
1358-
object: io.StringIO
1359-
1360-
1361-
Textual data in Python is handled with ``str`` objects, which are immutable
1359+
Textual data in Python is handled with :class:`str` objects, or :dfn:`strings`.
1360+
Strings are immutable
13621361
:ref:`sequences <typesseq>` of Unicode code points. String literals are
13631362
written in a variety of ways:
13641363

@@ -1383,6 +1382,9 @@ function :func:`str`.
13831382
Since there is no separate "character" type, indexing a string produces
13841383
strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``.
13851384

1385+
.. index::
1386+
object: io.StringIO
1387+
13861388
There is also no mutable string type, but :meth:`str.join` or
13871389
:class:`io.StringIO` can be used to efficiently construct strings from
13881390
multiple fragments.
@@ -2064,6 +2066,9 @@ that ``'\0'`` is the end of the string.
20642066
longer replaced by ``%g`` conversions.
20652067

20662068

2069+
.. index::
2070+
single: buffer protocol; binary sequence types
2071+
20672072
.. _binaryseq:
20682073

20692074
Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview`
@@ -2077,8 +2082,8 @@ Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview
20772082

20782083
The core built-in types for manipulating binary data are :class:`bytes` and
20792084
:class:`bytearray`. They are supported by :class:`memoryview` which uses
2080-
the buffer protocol to access the memory of other binary objects without
2081-
needing to make a copy.
2085+
the :ref:`buffer protocol <bufferobjects>` to access the memory of other
2086+
binary objects without needing to make a copy.
20822087

20832088
The :mod:`array` module supports efficient storage of basic data types like
20842089
32-bit integers and IEEE754 double-precision floating values.

Doc/reference/datamodel.rst

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,11 @@ Basic customization
11401140
modules are still available at the time when the :meth:`__del__` method is
11411141
called.
11421142

1143+
.. index::
1144+
single: repr() (built-in function); __repr__() (object method)
11431145

1144-
.. method:: object.__repr__(self)
11451146

1146-
.. index:: builtin: repr
1147+
.. method:: object.__repr__(self)
11471148

11481149
Called by the :func:`repr` built-in function to compute the "official" string
11491150
representation of an object. If at all possible, this should look like a
@@ -1157,18 +1158,25 @@ Basic customization
11571158
This is typically used for debugging, so it is important that the representation
11581159
is information-rich and unambiguous.
11591160

1161+
.. index::
1162+
single: string; __str__() (object method)
1163+
single: format() (built-in function); __str__() (object method)
1164+
single: print() (built-in function); __str__() (object method)
1165+
11601166

11611167
.. method:: object.__str__(self)
11621168

1163-
.. index::
1164-
builtin: str
1165-
builtin: print
1169+
Called by :func:`str(object) <str>` and the built-in functions
1170+
:func:`format` and :func:`print` to compute the "informal" or nicely
1171+
printable string representation of an object. The return value must be a
1172+
:ref:`string <textseq>` object.
11661173

1167-
Called by the :func:`str` built-in function and by the :func:`print` function
1168-
to compute the "informal" string representation of an object. This differs
1169-
from :meth:`__repr__` in that it does not have to be a valid Python
1170-
expression: a more convenient or concise representation may be used instead.
1171-
The return value must be a string object.
1174+
This method differs from :meth:`object.__repr__` in that there is no
1175+
expectation that :meth:`__str__` return a valid Python expression: a more
1176+
convenient or concise representation can be used.
1177+
1178+
The default implementation defined by the built-in type :class:`object`
1179+
calls :meth:`object.__repr__`.
11721180

11731181
.. XXX what about subclasses of string?
11741182

Lib/test/test_builtin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ def test_setattr(self):
12861286
self.assertRaises(TypeError, setattr, sys, 1, 'spam')
12871287
self.assertRaises(TypeError, setattr)
12881288

1289+
# test_str(): see test_unicode.py and test_bytes.py for str() tests.
12891290

12901291
def test_sum(self):
12911292
self.assertEqual(sum([]), 0)

Lib/test/test_unicode.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,26 @@ def __str__(self):
11821182

11831183
self.assertRaises(TypeError, str, 42, 42, 42)
11841184

1185+
def test_constructor_keyword_args(self):
1186+
"""Pass various keyword argument combinations to the constructor."""
1187+
# The object argument can be passed as a keyword.
1188+
self.assertEqual(str(object='foo'), 'foo')
1189+
self.assertEqual(str(object=b'foo', encoding='utf-8'), 'foo')
1190+
# The errors argument without encoding triggers "decode" mode.
1191+
self.assertEqual(str(b'foo', errors='strict'), 'foo') # not "b'foo'"
1192+
self.assertEqual(str(object=b'foo', errors='strict'), 'foo')
1193+
1194+
def test_constructor_defaults(self):
1195+
"""Check the constructor argument defaults."""
1196+
# The object argument defaults to '' or b''.
1197+
self.assertEqual(str(), '')
1198+
self.assertEqual(str(errors='strict'), '')
1199+
utf8_cent = '¢'.encode('utf-8')
1200+
# The encoding argument defaults to utf-8.
1201+
self.assertEqual(str(utf8_cent, errors='strict'), '¢')
1202+
# The errors argument defaults to strict.
1203+
self.assertRaises(UnicodeDecodeError, str, utf8_cent, encoding='ascii')
1204+
11851205
def test_codecs_utf7(self):
11861206
utfTests = [
11871207
('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ Tools/Demos
283283
Documentation
284284
-------------
285285

286+
- Issue #13538: Improve str() and object.__str__() documentation.
287+
286288
- Issue #16489: Make it clearer that importlib.find_loader() requires any and
287289
all packages to be separately imported.
288290

0 commit comments

Comments
 (0)