@@ -2265,8 +2265,8 @@ The :mod:`array` module supports efficient storage of basic data types like
22652265
22662266.. _typebytes :
22672267
2268- Bytes
2269- -----
2268+ Bytes Objects
2269+ -------------
22702270
22712271.. index :: object: bytes
22722272
@@ -2275,69 +2275,71 @@ binary protocols are based on the ASCII text encoding, bytes objects offer
22752275several methods that are only valid when working with ASCII compatible
22762276data and are closely related to string objects in a variety of other ways.
22772277
2278- Firstly, the syntax for bytes literals is largely the same as that for string
2279- literals, except that a ``b `` prefix is added:
2278+ .. class :: bytes([source[, encoding[, errors]]])
22802279
2281- * Single quotes: ``b'still allows embedded "double" quotes' ``
2282- * Double quotes: ``b"still allows embedded 'single' quotes" ``.
2283- * Triple quoted: ``b'''3 single quotes''' ``, ``b"""3 double quotes""" ``
2280+ Firstly, the syntax for bytes literals is largely the same as that for string
2281+ literals, except that a ``b `` prefix is added:
22842282
2285- Only ASCII characters are permitted in bytes literals (regardless of the
2286- declared source code encoding). Any binary values over 127 must be entered
2287- into bytes literals using the appropriate escape sequence.
2283+ * Single quotes: `` b'still allows embedded "double" quotes' ``
2284+ * Double quotes: `` b"still allows embedded 'single' quotes" ``.
2285+ * Triple quoted: `` b'''3 single quotes''' ``, `` b"""3 double quotes""" ``
22882286
2289- As with string literals, bytes literals may also use a `` r `` prefix to disable
2290- processing of escape sequences. See :ref: ` strings ` for more about the various
2291- forms of bytes literal, including supported escape sequences .
2287+ Only ASCII characters are permitted in bytes literals (regardless of the
2288+ declared source code encoding). Any binary values over 127 must be entered
2289+ into bytes literals using the appropriate escape sequence .
22922290
2293- While bytes literals and representations are based on ASCII text, bytes
2294- objects actually behave like immutable sequences of integers, with each
2295- value in the sequence restricted such that ``0 <= x < 256 `` (attempts to
2296- violate this restriction will trigger :exc: `ValueError `. This is done
2297- deliberately to emphasise that while many binary formats include ASCII based
2298- elements and can be usefully manipulated with some text-oriented algorithms,
2299- this is not generally the case for arbitrary binary data (blindly applying
2300- text processing algorithms to binary data formats that are not ASCII
2301- compatible will usually lead to data corruption).
2291+ As with string literals, bytes literals may also use a ``r `` prefix to disable
2292+ processing of escape sequences. See :ref: `strings ` for more about the various
2293+ forms of bytes literal, including supported escape sequences.
23022294
2303- In addition to the literal forms, bytes objects can be created in a number of
2304- other ways:
2295+ While bytes literals and representations are based on ASCII text, bytes
2296+ objects actually behave like immutable sequences of integers, with each
2297+ value in the sequence restricted such that ``0 <= x < 256 `` (attempts to
2298+ violate this restriction will trigger :exc: `ValueError `. This is done
2299+ deliberately to emphasise that while many binary formats include ASCII based
2300+ elements and can be usefully manipulated with some text-oriented algorithms,
2301+ this is not generally the case for arbitrary binary data (blindly applying
2302+ text processing algorithms to binary data formats that are not ASCII
2303+ compatible will usually lead to data corruption).
23052304
2306- * A zero-filled bytes object of a specified length: ``bytes(10) ``
2307- * From an iterable of integers: ``bytes(range(20)) ``
2308- * Copying existing binary data via the buffer protocol: ``bytes(obj) ``
2305+ In addition to the literal forms, bytes objects can be created in a number of
2306+ other ways:
23092307
2310- Also see the :ref: `bytes <func-bytes >` built-in.
2308+ * A zero-filled bytes object of a specified length: ``bytes(10) ``
2309+ * From an iterable of integers: ``bytes(range(20)) ``
2310+ * Copying existing binary data via the buffer protocol: ``bytes(obj) ``
23112311
2312- Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2313- numbers are a commonly used format for describing binary data. Accordingly,
2314- the bytes type has an additional class method to read data in that format:
2312+ Also see the :ref: `bytes <func-bytes >` built-in.
23152313
2316- .. classmethod :: bytes.fromhex(string)
2314+ Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2315+ numbers are a commonly used format for describing binary data. Accordingly,
2316+ the bytes type has an additional class method to read data in that format:
23172317
2318- This :class: `bytes ` class method returns a bytes object, decoding the
2319- given string object. The string must contain two hexadecimal digits per
2320- byte, with ASCII whitespace being ignored.
2318+ .. classmethod :: fromhex(string)
23212319
2322- >>> bytes .fromhex(' 2Ef0 F1f2 ' )
2323- b'.\xf0\xf1\xf2'
2320+ This :class: `bytes ` class method returns a bytes object, decoding the
2321+ given string object. The string must contain two hexadecimal digits per
2322+ byte, with ASCII whitespace being ignored.
23242323
2325- .. versionchanged :: 3.7
2326- :meth: `bytes.fromhex ` now skips all ASCII whitespace in the string,
2327- not just spaces.
2324+ >>> bytes .fromhex(' 2Ef0 F1f2 ' )
2325+ b'.\xf0\xf1\xf2'
23282326
2329- A reverse conversion function exists to transform a bytes object into its
2330- hexadecimal representation.
2327+ .. versionchanged :: 3.7
2328+ :meth: `bytes.fromhex ` now skips all ASCII whitespace in the string,
2329+ not just spaces.
23312330
2332- .. method :: bytes.hex()
2331+ A reverse conversion function exists to transform a bytes object into its
2332+ hexadecimal representation.
23332333
2334- Return a string object containing two hexadecimal digits for each
2335- byte in the instance.
2334+ .. method :: hex()
23362335
2337- >>> b ' \xf0\xf1\xf2 ' .hex()
2338- 'f0f1f2'
2336+ Return a string object containing two hexadecimal digits for each
2337+ byte in the instance.
23392338
2340- .. versionadded :: 3.5
2339+ >>> b ' \xf0\xf1\xf2 ' .hex()
2340+ 'f0f1f2'
2341+
2342+ .. versionadded :: 3.5
23412343
23422344Since bytes objects are sequences of integers (akin to a tuple), for a bytes
23432345object *b *, ``b[0] `` will be an integer, while ``b[0:1] `` will be a bytes
@@ -2367,49 +2369,53 @@ Bytearray Objects
23672369.. index :: object: bytearray
23682370
23692371:class: `bytearray ` objects are a mutable counterpart to :class: `bytes `
2370- objects. There is no dedicated literal syntax for bytearray objects, instead
2371- they are always created by calling the constructor:
2372+ objects.
23722373
2373- * Creating an empty instance: ``bytearray() ``
2374- * Creating a zero-filled instance with a given length: ``bytearray(10) ``
2375- * From an iterable of integers: ``bytearray(range(20)) ``
2376- * Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!') ``
2374+ .. class :: bytearray([source[, encoding[, errors]]])
23772375
2378- As bytearray objects are mutable, they support the
2379- :ref: `mutable <typesseq-mutable >` sequence operations in addition to the
2380- common bytes and bytearray operations described in :ref: `bytes-methods `.
2376+ There is no dedicated literal syntax for bytearray objects, instead
2377+ they are always created by calling the constructor:
23812378
2382- Also see the :ref: `bytearray <func-bytearray >` built-in.
2379+ * Creating an empty instance: ``bytearray() ``
2380+ * Creating a zero-filled instance with a given length: ``bytearray(10) ``
2381+ * From an iterable of integers: ``bytearray(range(20)) ``
2382+ * Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!') ``
23832383
2384- Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2385- numbers are a commonly used format for describing binary data. Accordingly,
2386- the bytearray type has an additional class method to read data in that format:
2384+ As bytearray objects are mutable, they support the
2385+ :ref: ` mutable < typesseq-mutable >` sequence operations in addition to the
2386+ common bytes and bytearray operations described in :ref: ` bytes-methods `.
23872387
2388- .. classmethod :: bytearray.fromhex(string)
2388+ Also see the :ref: ` bytearray < func- bytearray>` built-in.
23892389
2390- This :class: ` bytearray ` class method returns bytearray object, decoding
2391- the given string object. The string must contain two hexadecimal digits
2392- per byte, with ASCII whitespace being ignored.
2390+ Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2391+ numbers are a commonly used format for describing binary data. Accordingly,
2392+ the bytearray type has an additional class method to read data in that format:
23932393
2394- >>> bytearray .fromhex(' 2Ef0 F1f2 ' )
2395- bytearray(b'.\xf0\xf1\xf2')
2394+ .. classmethod :: fromhex(string)
23962395
2397- .. versionchanged :: 3.7
2398- :meth: ` bytearray.fromhex ` now skips all ASCII whitespace in the string,
2399- not just spaces .
2396+ This :class: ` bytearray ` class method returns bytearray object, decoding
2397+ the given string object. The string must contain two hexadecimal digits
2398+ per byte, with ASCII whitespace being ignored .
24002399
2401- A reverse conversion function exists to transform a bytearray object into its
2402- hexadecimal representation.
2400+ >>> bytearray .fromhex( ' 2Ef0 F1f2 ' )
2401+ bytearray(b'.\xf0\xf1\xf2')
24032402
2404- .. method :: bytearray.hex()
2403+ .. versionchanged :: 3.7
2404+ :meth: `bytearray.fromhex ` now skips all ASCII whitespace in the string,
2405+ not just spaces.
24052406
2406- Return a string object containing two hexadecimal digits for each
2407- byte in the instance .
2407+ A reverse conversion function exists to transform a bytearray object into its
2408+ hexadecimal representation .
24082409
2409- >>> bytearray (b ' \xf0\xf1\xf2 ' ).hex()
2410- 'f0f1f2'
2410+ .. method :: hex()
2411+
2412+ Return a string object containing two hexadecimal digits for each
2413+ byte in the instance.
24112414
2412- .. versionadded :: 3.5
2415+ >>> bytearray (b ' \xf0\xf1\xf2 ' ).hex()
2416+ 'f0f1f2'
2417+
2418+ .. versionadded :: 3.5
24132419
24142420Since bytearray objects are sequences of integers (akin to a list), for a
24152421bytearray object *b *, ``b[0] `` will be an integer, while ``b[0:1] `` will be
0 commit comments