gpt4 book ai didi

python - 双端队列扩展时将字节更改为整数

转载 作者:行者123 更新时间:2023-11-30 23:38:47 24 4
gpt4 key购买 nike

from collections import deque

recvBuffer = deque()
x1 = b'\xFF'
recvBuffer.append(x1)
recvBuffer.extend(x1)
x2 = recvBuffer.pop()
x3 = recvBuffer.pop()
print(type(x1))
print(type(x2))
print(type(x3))

上面的代码在Python 3.2.3上打印以下内容

<class 'bytes'>
<class 'int'>
<class 'bytes'>

为什么当extend()-ed为双端队列时,字节变成了int?

最佳答案

字节documented是一个整数序列:

"bytes" object, which is an immutable sequence of integers in the range 0 <= x < 256

当您扩展时,您会迭代该序列。当您迭代 bytes 对象时,您将获得整数。请注意,deque 与此无关。您将在普通列表上使用 extend 或仅使用 for byte in x1 看到相同的行为。

关于python - 双端队列扩展时将字节更改为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14151245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com