【发布时间】:2021-04-22 11:37:25
【问题描述】:
行为显示在代码中:
>>> b = bytes(b'asdf')
>>> print(type(b))
<class 'bytes'>
>>> print(type(b[0]))
<class 'int'>
为什么当我从 b[0] 这样的字节对象中获取一个字节时,它会将其转换为 int。
【问题讨论】:
-
使用 slice 将其作为字节获取
type(b[:0]) -
你有相同的列表
a = [1,2,3]-type(a)给出<class 'list'>和type(a[0])给出<class 'int'>而不是<class 'list'>但type(a[:0])仍然给出<class 'list'>。bytes可能也是如此 - 它可能意味着"list of integers"和当 y -
这是预期的 documented 行为:“字节对象实际上表现得像不可变的整数序列,序列中的每个值都受到限制,使得 0
标签: python python-3.x byte