diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-02-05 12:13:38 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-02-05 12:13:38 (GMT) |
commit | bd866e972c1db654105a5204742eeff342a91719 (patch) | |
tree | 5b3d6519fe1a059c19d4798f9938d02736a54029 /Doc/howto | |
parent | e6a1464b89fc24f88e92fe0bab05c80efaf9780f (diff) | |
download | cpython-bd866e972c1db654105a5204742eeff342a91719.zip cpython-bd866e972c1db654105a5204742eeff342a91719.tar.gz cpython-bd866e972c1db654105a5204742eeff342a91719.tar.bz2 |
Everybody hates this one :) (bytes indexing)
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/pyporting.rst | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Doc/howto/pyporting.rst b/Doc/howto/pyporting.rst index 38a13af..0c7721c 100644 --- a/Doc/howto/pyporting.rst +++ b/Doc/howto/pyporting.rst @@ -367,6 +367,37 @@ To turn the warning into an exception, use the ``-bb`` flag instead:: BytesWarning: Comparison between bytes and string +Indexing bytes objects +'''''''''''''''''''''' + +Another potentially surprising change is the indexing behaviour of bytes +objects in Python 3:: + + >>> b"xyz"[0] + 120 + +Indeed, Python 3 bytes objects (as well as :class:`bytearray` objects) +are sequences of integers. But code converted from Python 2 will often +assume that indexing a bytestring produces another bytestring, not an +integer. To reconcile both behaviours, use slicing:: + + >>> b"xyz"[0:1] + b'x' + >>> n = 1 + >>> b"xyz"[n:n+1] + b'y' + +The only remaining gotcha is that an out-of-bounds slice returns an empty +bytes object instead of raising ``IndexError``: + + >>> b"xyz"[3] + Traceback (most recent call last): + File "<stdin>", line 1, in <module> + IndexError: index out of range + >>> b"xyz"[3:4] + b'' + + ``__str__()``/``__unicode__()`` ''''''''''''''''''''''''''''''' In Python 2, objects can specify both a string and unicode representation of |