4 ms·
You don't grok it. Bytes are not ASCII strings. They are NUMBERS between 0 and 255. Strings are strings. DECODING bytes with ASCII encoding into TEXT is not "ca
by Walkman 10y ago
You don't grok it. Bytes are not ASCII strings. They are NUMBERS between 0 and 255. Strings are strings. DECODING bytes with ASCII encoding into TEXT is not "casting", it's DECODING.
- Spidler 10y agoA thing I've found to help people about that is to override __repr__ on bytestrings in python3 and bytes in python3. If you make it not print strings as "ascii" but as hex, people grasp the difference easier.
- jstimpfle 10y agoI wouldn't say they are not ASCII strings, because they are if you squint. Better say "bytes are not Unicode strings".
- Walkman 10y agoNo, they are NOT strings, they are not any kind of strings. They are bytes: >>> b'asd'[0] 97 >>> b'asd'[1] 115 >>> b'asd'[2] 100 They happen to contain an ASCII DECODABLE string in this case, but to get the text back, you NEED TO decode it first: >>> text = b'asd'.decode() >>> text[0] 'a' >>> text[1] 's' >>> text[2] 'd'
- jstimpfle 10y agoTechnically true. I'd inferred a different context on this one: By "strings" I meant the abstract concept in this case, not the python str which is the one meant.