为什么我在索引字节时得到一个 int?

Why do I get an int when I index bytes?

我试图在 python 3.4 中获取字节串的第一个字符,但是当我索引它时,我得到一个 int:

>>> my_bytes = b'just a byte string'
b'just a byte string'
>>> my_bytes[0]
106
>>> type(my_bytes[0])
<class 'int'>

这对我来说似乎不直观,因为我期望得到 b'j'

我发现我可以得到我期望的值,但对我来说感觉像是 hack。

>>> my_bytes[0:1]
b'j'

有人可以解释为什么会这样吗?

bytes 类型是 Binary Sequence type,明确记录为包含 0 到 255 范围内的整数序列。

来自文档:

Bytes objects are immutable sequences of single bytes.

[...]

While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256[.]

[...]

Since bytes objects are sequences of integers (akin to a tuple), for a bytes object b, b[0] will be an integer, while b[0:1] will be a bytes object of length 1. (This contrasts with text strings, where both indexing and slicing will produce a string of length 1).

大胆强调我的。请注意,索引字符串在序列类型中有点例外; 'abc'[0] 给你一个长度为 1 的 str 对象; str 是唯一始终包含其自身类型元素的序列类型。

这与其他语言处理字符串数据的方式相呼应;在 C 中,unsigned char type 也是 0-255 范围内的有效整数。如果您使用未限定的 char 类型,许多 C 编译器默认为 unsigned,并且文本被建模为 char[] 数组。