3 ms·
Raw byte string literals (b'') and their type in the language are details you can be unaware of if you didn't live through the Python 2->3 transition or don't d
by ok123456 2y ago
Raw byte string literals (b'') and their type in the language are details you can be unaware of if you didn't live through the Python 2->3 transition or don't deal with binary data buffers.
- ziml77 2y agoI was surprised that bytes were a lesser known type in Python, but now that I think about what you're saying I guess it's true. The bytes type doesn't get surfaced if you're only working with higher level libraries that do the bytes to str conversion for you.
- dfox 2y agoI used to think that only various external libraries (eg. Flask) do various hidden conversion to shield the user from the str-bytes distinction, but even the builtin MIME implementation does that kind of stuff, which means that it will break 8bit S/MIME messages (and when you get bitten by that hard, and just go off implementing your own MIME parser the fact that bytes has all the string methods like .split() comes very handy).
- nerdponx 2y agoJust about every I/O interface in the Python standard library does it: env vars, reading data in files, reading stdin, even filenames themselves.
- dfox 2y agoThere is a difference: packet on a wire is inherently bytes, while interaction with the OS is a can of worms. On UNIX all that are just an opaque byte blobs, but with expectation that these things mean something to humans, so it either are strings in the locale encoding or UTF-8 encoded strings, then there is Windows where this kind of interfaces use something like UTF-16. In general, the name of the thing that you use to interact with the OS probably is a meaningful string of Unicode code points, but it does not have to be so and you should have some kind of plan what to do when it is not. Doing that in a way that abstracts away the conventions of the underlying OS is almost certainly impossible.