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

Skip to content

Commit 18da8f0

Browse files
committed
#3220: improve bytes docs a bit.
1 parent 07a1f94 commit 18da8f0

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -512,17 +512,21 @@ string literals. In addition to the functionality described here, there are
512512
also string-specific methods described in the :ref:`string-methods` section.
513513

514514
Bytes and bytearray objects contain single bytes -- the former is immutable
515-
while the latter is a mutable sequence. Bytes objects can be constructed from
516-
literals too; use a ``b`` prefix with normal string syntax: ``b'xyzzy'``. To
517-
construct byte arrays, use the :func:`bytearray` function.
515+
while the latter is a mutable sequence. Bytes objects can be constructed the
516+
constructor, :func:`bytes`, and from literals; use a ``b`` prefix with normal
517+
string syntax: ``b'xyzzy'``. To construct byte arrays, use the
518+
:func:`bytearray` function.
518519

519520
.. warning::
520521

521522
While string objects are sequences of characters (represented by strings of
522523
length 1), bytes and bytearray objects are sequences of *integers* (between 0
523524
and 255), representing the ASCII value of single bytes. That means that for
524-
a bytes or bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]``
525-
will be a bytes or bytearray object of length 1.
525+
a bytes or bytearray object *b*, ``b[0]`` will be an integer, while
526+
``b[0:1]`` will be a bytes or bytearray object of length 1. The
527+
representation of bytes objects uses the literal format (``b'...'``) since it
528+
is generally more useful than e.g. ``bytes([50, 19, 100])``. You can always
529+
convert a bytes object into a list of integers using ``list(b)``.
526530

527531
Also, while in previous Python versions, byte strings and Unicode strings
528532
could be exchanged for each other rather freely (barring encoding issues),
@@ -1413,15 +1417,14 @@ Wherever one of these methods needs to interpret the bytes as characters
14131417
The bytes and bytearray types have an additional class method:
14141418

14151419
.. method:: bytes.fromhex(string)
1420+
bytearray.fromhex(string)
14161421

1417-
This :class:`bytes` class method returns a bytes object, decoding the given
1418-
string object. The string must contain two hexadecimal digits per byte, spaces
1419-
are ignored.
1422+
This :class:`bytes` class method returns a bytes or bytearray object,
1423+
decoding the given string object. The string must contain two hexadecimal
1424+
digits per byte, spaces are ignored.
14201425

1421-
Example::
1422-
1423-
>>> bytes.fromhex('f0 f1f2 ')
1424-
b'\xf0\xf1\xf2'
1426+
>>> bytes.fromhex('f0 f1f2 ')
1427+
b'\xf0\xf1\xf2'
14251428

14261429
.. XXX verify/document translate() semantics!
14271430

0 commit comments

Comments
 (0)