gpt4 book ai didi

python - 什么是类字节对象

转载 作者:太空宇宙 更新时间:2023-11-04 00:28:05 25 4
gpt4 key购买 nike

在 Python 3.6 中,base64.b64encode() 函数需要“一个类似字节的对象,而不是 str”。

与普通字符串相对的类字节对象的示例是什么?

最佳答案

任何逻辑上存储字节序列的东西都符合条件。这包括实际的 bytes 类型、bytearraymmap.mmaparray.array('B')、 etc. str 在Python 3中是文本类型;字符没有以指定的编码存储,因此不能直接将它们用作原始二进制数据;它们必须用特定的编码显式编码

有关技术定义,请参阅 Python 3 glossary :

An object that supports the Buffer Protocol and can export a C-contiguous buffer. This includes all bytes, bytearray, and array.array objects, as well as many common memoryview objects. Bytes-like objects can be used for various operations that work with binary data; these include compression, saving to a binary file, and sending over a socket.

Some operations need the binary data to be mutable. The documentation often refers to these as “read-write bytes-like objects”. Example mutable buffer objects include bytearray and a memoryview of a bytearray. Other operations require the binary data to be stored in immutable objects (“read-only bytes-like objects”); examples of these include bytes and a memoryview of a bytes object.

关于python - 什么是类字节对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46750606/

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