Why does base64.b64encode () return a bytes object?
source link: https://www.codesd.com/item/why-does-base64-b64encode-return-a-bytes-object.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Why does base64.b64encode () return a bytes object?
The purpose of the base64.b64encode()
function is to convert binary data into ASCII-safe "text". However the return type of the method is a bytes object, e.g.:
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> base64.b64encode(b'abc')
b'YWJj'
Now, it's easy to simply take that output and decode()
it, but my question is: What is a significance of base64.b64encode()
returning a bytes
object 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.
Recommend
-
13
How does MSLinqToSqlGenerator determine the return type of a stored procedure? advertisements I have a stored procudre (SP) as follows:...
-
10
Why does PF_VIRT_FIRMWARE_ENABLED return false even when virtualization is enabled in the firmware?Why does PF_VIRT_FIRMWARE_ENABLED return false even when virtualization is enabled in the firmware?
-
10
Does Constructor Return Any Value? To find out whether constructor returns any value or not, let’s learn about a constructor? In simple terms, the constructor is a special method, which is used to ini...
-
7
Why does React state need a new object/array?If you have been using React for a while, you are familiar with how state update works. There are a lot of internal optimizations that React makes for faster rendering. And one of the implemen...
-
2
Why is there trailing garbage when I try to decode the bytes of a HttpContent object? Raymond September 23rd, 2021 A cu...
-
9
A - Why does the grep not return several clues? advertisements For this code: > grep("abc","abcabcabc", v...
-
3
Why does console.log() return ‘undefined’?Why Node.js REPLs are how they are, and what happens when there is nothing to evaluatePhoto by
-
3
Why does my SSH private key still work after changing some bytes in the file? Cryptography Stack Exchange is a question and answer site...
-
3
Written by David Anderson on December 21, 2018 Why are ethernet jumbo frames 9000 bytes? In a marathon of
-
6
TLDRObject.keys() doesn’t return (keyof T)[] in TypeScript because objects can have additional properties at run...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK