4 ms·
When you say "strings" are you referring to strings or bytes? Why shouldn't you slice or index them? It seems like those operations make sense in either case bu
by aidos 11y ago
When you say "strings" are you referring to strings or bytes? Why shouldn't you slice or index them? It seems like those operations make sense in either case but I'm sure I'm missing something.
On the guessing encodings when opening files, that's not really a problem. The caller should specify the encoding manually ideally. If you don't know the encoding of the file, how can you decode it? You could still open it as raw bytes if required.
- saurik 11y agoI think you are missing the difference between codepoints (as distinct from codeunits) and characters.
- aidos 11y agoAnd unfortunately, I'm not anymore enlightened as to my misunderstanding. I get that every different thing (character) is a different Unicode number (code point). To store / transmit these you need some standard (encoding) for writing them down as a sequence of bytes (code units, well depending on the encoding each code unit is made up of different numbers of bytes). How is any of that in conflict with my original points? Or is some of my above understanding incorrect. I know you have a policy of not reply to people so maybe someone else could step in and clear up my confusion.
- DasIch 11y agoCodepoints and characters are not equivalent. A character can consist of one or more codepoints. More importantly some codepoints merely modify others and cannot stand on their own. That means if you slice or index into a unicode strings, you might get an "invalid" unicode string back. That is a unicode string that cannot be encoded or rendered in any meaningful way.
- aidos 11y agoRight, ok. I recall something about this - ü can be represented either by a single code point or by the letter 'u' preceded by the modifier. As the user of unicode I don't really care about that. If I slice characters I expect a slice of characters. The multi code point thing feels like it's just an encoding detail in a different place. I guess you need some operations to get to those details if you need. Man, what was the drive behind adding that extra complexity to life?! Thanks for explaining. That was the piece I was missing.
- DasIch 11y agoI used strings to mean both. Byte strings can be sliced and indexed no problems because a byte as such is something you may actually want to deal with. Slicing or indexing into unicode strings is a problem because it's not clear what unicode strings are strings of. You can look at unicode strings from different perspectives and see a sequence of codepoints or a sequence of characters, both can be reasonable depending on what you want to do. Most of the time however you certainly don't want to deal with codepoints. Python however only gives you a codepoint-level perspective. Guessing encodings when opening files is a problem precisely because - as you mentioned - the caller should specify the encoding, not just sometimes but always. Guessing an encoding based on the locale or the content of the file should be the exception and something the caller does explicitly.
- aidos 11y agoIt slices by codepoints? That's just silly, so we've gone through this whole unicode everywhere process so we can stop thinking about the underlying implementation details but the api forces you to have to deal with them anyway. Fortunately it's not something I deal with often but thanks for the info, will stop me getting caught out later.