4 ms·
Python 3's approach is clearly the best. Because it focuses on the problem at hand, unicode codepoints. A string in python is a sequence of unicode codepoints,
by throwawayffffas 6y ago
Python 3's approach is clearly the best. Because it focuses on the problem at hand, unicode codepoints. A string in python is a sequence of unicode codepoints, it's length should be the number of codepoints in the sequence, it has nothing to do with bytes.
To draw an absurd parallel "[emoji]".len() == 17 is equivalent to [1,2].len() == 8 (2 32 bit integers)
In my opinion the most useful result in the case the article describes is 5. There should of course be a way to get 1 (the number of extended graphemes), but it should not be the strings "length".
- nerdponx 6y agoDon't Swift and Go support iterating over graphemes? Edit: yes, Swift is mentioned at the bottom of the article. It'd be great to have a function for that in other scripting languages like Python, Ruby, etc. There was an interesting sub-thread here on HN a while ago about how a string-of-codepoints is just as bad as a string-of-bytes (subject to truncation, incorrect iteration, incorrect length calculation) and that we should just have string-of-bytes if we can't have string-of-graphemes. I don't agree, but some people felt very strongly about it.
- banthar 6y agoDefining string as a sequence of unicode codepoints is the mistake. Nobody ever cares about unicode codepoints. You either want the number of bytes or the width of the string on screen. UTF-32 codepoints waste space and give you neither.
- stkdump 6y agoThe width on the screen is in pixels. Yes, I find monospace fonts increasingly pointless.
- anaerobicover 6y agoBut what do you do when you're processing a string with codepoints that compose into one user-visible glyph? >>> len("🇨🇦") 2