为什么 base64.b64encode() 返回一个字节对象? [英] Why does base64.b64encode() return a bytes object?
问题描述
base64.b64encode()
的目的是将二进制数据转换为 ASCII 安全的文本".但是,该方法返回一个字节类型的对象:
简单地获取输出和 decode()
很容易,但我的问题是:base64.b64encode()
返回 bytes 的意义是什么
而不是 str
?
base64.b64encode() 函数的目的是将二进制数据转换为 ASCII 安全的文本"
Python 不同意这一点 - base64 被故意归类为 二进制转换.
强制字节和文本分离并禁止隐式转换是 Python 3 中的一个设计决定.Python 现在对此非常严格,以至于 bytes.encode
甚至不存在,因此 b'abc'.encode('base64')
会引发 属性错误
.
该语言的观点是字节串对象已经编码.将字节编码为文本的编解码器不适合这种范式,因为当您想从字节域转到文本域时,它是一个解码.请注意,rot13
编码也已从 标准编码 出于同样的原因 - 它不适合 Python 3 范式.
还可以提出一个性能参数:假设 Python 自动处理了 base64 输出的解码,这是一个 ASCII 编码的二进制表示,由来自 binascii
模块,转换为文本域中的 Python 对象.如果你真的想要字节,你只需要通过再次编码为 ASCII 来撤消解码.这将是一个浪费的往返,一个不必要的双重否定.最好选择加入"解码到文本步骤.
The purpose of base64.b64encode()
is to convert binary data into ASCII-safe "text". However, the method returns an object of type bytes:
>>> import base64
>>> base64.b64encode(b'abc')
b'YWJj'
It's easy to simply take that output and decode()
it, but my question is: what is a significance of base64.b64encode()
returning bytes
rather than a str
?
The purpose of the base64.b64encode() function is to convert binary data into ASCII-safe "text"
Python disagrees with that - base64 has been intentionally classified as a binary transform.
It was a design decision in Python 3 to force the separation of bytes and text and prohibit implicit transformations. Python is now so strict about this that bytes.encode
doesn't even exist, and so b'abc'.encode('base64')
would raise an AttributeError
.
The opinion the language takes is that a bytestring object is already encoded. A codec which encodes bytes into text does not fit into this paradigm, because when you want to go from the bytes domain to the text domain it's a decode. Note that rot13
encoding was also banished from the list of standard encodings for the same reason - it didn't fit properly into the Python 3 paradigm.
There also can be a performance argument to make: suppose Python automatically handled decoding of the base64 output, which is an ASCII-encoded binary representation produced by C code from the binascii
module, into a Python object in the text domain. If you actually wanted the bytes, you would just have to undo the decoding by encoding into ASCII again. It would be a wasteful round-trip, an unnecessary double-negation. Better to 'opt-in' for the decode-to-text step.
这篇关于为什么 base64.b64encode() 返回一个字节对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!