4 msΒ·
Actually it is. What you suggesting would completely break those operations. Compared just not getting what you want. In any way you can tell python how to beha
by takeda 6y ago
Actually it is. What you suggesting would completely break those operations. Compared just not getting what you want. In any way you can tell python how to behave through normalization of the unicode.
- csande17 6y ago> What you suggesting would completely break those operations. Compared just not getting what you want. I reversed a string several times in Python 2 and not once did my computer catch fire! I just got a string back that, in certain situations, didn't contain the characters I was expecting. And then my father had to have a talk with me about Unicode, and how computers sometimes use multiple bytes to represent a single character, and so if you rearrange the individual bytes you get different, usually more broken-looking characters. And when my child runs "π¨π¦"[::-1] in Python 3, I'll still have to give them The Unicode Talk. I just need to use the word "codepoint" instead of the word "byte". The situation is basically the same--if you know enough about Unicode to understand Python 3's behavior on certain non-English text, you know enough to understand Python 2's behavior.
- takeda 6y ago> And when my child runs "π¨π¦"[::-1] in Python 3, I'll still have to give them The Unicode Talk What were you expecting to get when doing reverse on π¨π¦? The flag symbol is encoded using regional indicator symbol letters: C, A. When you reverse it you get regional indicator symbol letters A, C which represents Ascension Island. In python2 you get a total garbage. The flag representation is not part of unicode, unicode only specifies that you can encode country codes using ISO 3166-1 alpha-2 and it is up to the application how to present it.
- csande17 6y ago> What were you expecting to get when doing reverse on π¨π¦? My future child sees only one character in that string, an emoji. (If you put the cursor to the right of π¨π¦ and press backspace, the whole flag goes away, so that must mean it's only one character, right?) If they reverse that string, they'd expect to get the same result back. > The flag symbol is encoded using regional indicator symbol letters: C, A. When you reverse it you get regional indicator symbol letters A, C which represents Ascension Island. Right, this is exactly what I'll have to explain! Just like how in UTF-8, "Γ©" is encoded using two bytes: 0xC3 0xA9. So if you reverse that, you get bytes 0xA9 0xC3, which do not represent "Γ©". It's the exact same problem, you've just moved up one level of abstraction. (The Python 3 version of the problem isn't unique to flags -- there are all kinds of modifiers and ZWJ sequences and combining characters that get messed up if you rearrange individual codepoints.) A few languages have tried to solve this problem in other ways, like how Swift considers each character of a string to be a "grapheme cluster", but that comes with a lot of its own drawbacks.
- takeda 6y agoIMO this isn't really a Python issue though. By unicode standard how to present it is left to the frontend. Even displaying "CA" might be enough. With combining character sequences there's at least a normalization and you can transform to the right representation based on your need. As you mentioned that Swift allows one to operate on grapheme clusters, and it comes with advantages but also some issues as you pointed out. The topic is complex but Python 3 does what unicode standard specifies, while Python 2 would output a garbage in similar scenario.