Most of the world is moving to unicode exactly because of this (I know Sillicon Vally companies are probably moving to unicode, so that their users can use cute emojis).
Most people also need to handle stuff that are in different encodings (I work daily in both Latin1 (north-western European) and UTF8), translating between that was painful/borderline-impossible in the old days (python 2).
The sharp distinction between bytes and strings are essential for any non-english/american/aussie person. Sadly python2 got that wrong, and hence had to die..
Which is probably also why Perl6 choose to go the same route..
If you really need a good reason to go to python 3 other than 6 billion extra potential customers, try "oh look, we have emojis" <sarcasm/>
Python 3 is objectively harder and more confusing to use in a multilingual environment. Python 2 didn't pretend that only Unicode exists and Python 2 was encoding-agnostic. This is the correct default for the real world.
Ironically, the effect of Python 3 was the opposite: it made life really hard for those that aren't exclusive anglophones and made life a lot easier for American programmers who now don't have to bother with the whole character encoding mess.
Personally, Python 3's broken support for internationalisation made me drop Python altogether. Python 2 was confusing, but the defaults were at least workable.
I find it so much easier (I am also not an English language programmer), because it is very explicit about what is the codepoints (i.e. string) and what is the bytes (i.e. on the disk or the network).
The encode/decode functions is basically the only change, but it gives you the reason to explicitly tell it about the encoding you expect..
But as you left python, the point is moot :)
Anyways, the point is that Python 3's design was broken from the start. Anglophone programmers think that internationalisation means "upgrading" from ASCII to Unicode. In reality, though, the rest of the world's programmers got along just fine long before Unicode was invented and will continue to do fine long after Unicode gets replaced by something else.
True internationalisation means mechanisms to deal with the world's text encodings in a neutral and culture-agnostic way. (And "Unicode only everywhere and no exceptions" is definitely not that.)
If you do recognize that there is such a thing called text, that human understand, and for example the text "hello" has five "letters".... The point that that text might be represented in five different (and equally valid) ways with different amounts of bytes is irrelevant to the "human user"...
Hence the sharp distinction between makes sense to most humans.
The danish word "rødgrød" for example is seven letters (i.e. every person would say it is 7 letters long), a computer would call it 7, 7 9 or 16 bytes (in cp865 (nordics) latin1("wester european", utf8 and utf16...)
Unless you work in the ideal "text" mode getting the correct length of the text is not that trivial. Equally, the pair 2-3 and and 7-8 bytes in utf8 must not be split (as either half has not meaning on its own).. hence (in utf8) a function "give me the next letter" will return 1,2,1,1,1,2,1 bytes in its revocations..
Also casefolding (upper/lower case) is hard when working in bytes (some glyphs might not even have one or the other)..
I am unable to say if unicode fits all languages in the world as needed, but it is much better as byte-wrangling if you have multiple possible encodings at once..
EDIT: for reference the bytes of "rødgrød" are
cp865 : b'r\x9bdgr\x9bd'
latin1 : b'r\xf8dgr\xf8d'
utf8 : b'r\xc3\xb8dgr\xc3\xb8d'
utf16le: b'r\x00\xf8\x00d\x00g\x00r\x00\xf8\x00d\x00'
utf16be: b'\x00r\x00\xf8\x00d\x00g\x00r\x00\xf8\x00d'
(and it cannot be written in ASCII ("C"))
Anyone saying that bytes and text are the same are nuts...
The hoops of encoding and decoding bytes are not optional, manipulating text and manipulating arbitrary bytes are not the same thing and encoding and decoding is how you translate between the two domains. Python 2 hid this for a subset of the bypes and code was usually broken as a result. Python 3 requires that this split be taken in account in all cases (as do e.g. Java or C#) and is significantly better as a result.
> Anglophone programmers think that internationalisation means "upgrading" from ASCII to Unicode.
Which is a pretty significant upgrade from their previous case of literally not giving a fuck.
> True internationalisation means mechanisms to deal with the world's text encodings in a neutral and culture-agnostic way.
That's completely meaningless word salad.
1. encoding and decoding has relatively little to do with internationalisation
2. encoding and decoding is no more culture specific than the source encoding is, if you're dealing with culture-specific encodings aside from not doing anything with the content (not even displaying it) you can't be culture-agnostic until after you've decoded the text
3. you can't "neutrally" deal with "the world's text encoding" (whatever that's supposed to mean in your mind) because most of them are not compatible with one another since they use the same binary space for completely different text mappings
Byte handling is definately not fun in variable encodings, and even fixed-length multi-byte encodings require a lot of support functionality when working with bytes..
Such a simple thing as "len" is suddenly hard (for the human definition of length and not the byte-length), the latter is also easily attainable by converting the string to bytes and get the length of that in the given encoding.
In python2 len(str) will tell you exactly how many bytes some text takes, which is useful.
* Rust has a very strict split between bytes and text
* Go has fucked up bytestrings which depending on validity may break at any point depending on the API you're applying
* Erlang's strings are widely considered awful, and are literally just linked lists of integers making them slow, memory-heavy and prone to pretty-printer whims (as the pretty printer decides on the fly whether a given list looks like a string — and should be printed thus — or does not — and should be printed as a regular list of integers)
What the fuck. And I thought constant-time indexable strings were a bad idea!
Erlang has 3 types of strings:
1. the list of integers thing, which you should mostly avoid
2. binary strings (like python2 str or go)
3. iolists: a list or element of either of 1, 2 or 3. This is the nice part – it basically means erlang has ropes. And all the "egress" functions (sending udp or tcp packets or writing to stdout or files or sockets etc.) take iolists. So you can avoid a lot of pointless copying and write text or binary data very efficiently; I know of no other (non-BEAM) language that has similarly good support for this, do you?
See http://www.evanmiller.org/elixir-ram-and-the-template-of-doo... for more details.
It is nice in a theoretical sense, in the same way Haskell's [Char] is nice, in a practical sense it's awful.
> 2. binary strings (like python2 str or go)
AKA not actually strings, I don't remember Erlang having much if any string-manipulation API for working on bytes.
> 3. iolists: a list or element of either of 1, 2 or 3. This is the nice part – it basically means erlang has ropes. And all the "egress" functions (sending udp or tcp packets or writing to stdout or files or sockets etc.) take iolists.
This is only for output, so they're not actually ropes.
> So you can avoid a lot of pointless copying and write text or binary data very efficiently; I know of no other (non-BEAM) language that has similarly good support for this, do you?
Most mutable languages will use buffered output streams and possibly some sort of string sequence (even C has iovecs), so while iolists are a good fit for Erlang they make sense in few other languages.
Haskell's list of Char is awful in both a theoretical and practical sense whereas in erlang's case you can think of it as just a degenerate iolist.
> I don't remember Erlang having much if any string-manipulation API for working on bytes
Elixir is better but erlang certainly has string manipulation functionality for binaries: pattern matching, regular expressions, search, splitting and joining etc.
> This is only for output, so they're not actually ropes.
Not true, lots of non-output functions also accept iolists, e.g. the re module.
Sure, but for most languages older than a few years this ship has sadly long sailed and few would be willing to switch to O(n) indexing.
> It's much better to mess up in not having a clear distinction between bytes and text (annoying, but mostly easy to work around)
Except when it's not and the packages you're trying to use simply do not work and fuck up mightily with non-ascii data.
> then to mess up by having constant-time indexable utf-32 as python3 does
It's funny that just above you praise Erlang which does exactly that. Meanwhile Python 3.3 introduced a flexible string representation so depending on the content of the string the internal string encoding may be ISO-8859-1, UCS2 or UCS4 (and yes sadly only these as the designers refused to break the constant-time indexing guarantee).
I do, and I do believe Python 3's approach is superior for strings.
An array of 8-bit integers is not a string. It's an array of 8-bit integers. I want my string type to be a sequence of Unicode codepoints, and I want iterating it to yield codepoints, I want indexing into it to yield the codepoint at the index, and I want the length of it to be the number of codepoints it contains. When I'm ready to send it over a wire or flush it to disk, then I'll think about the best way to represent it as an array of 8-bit integers, but bringing the baggage of encodings (especially the baggage of UTF-8) into any part of my code except the boundary layer is asking for trouble.
I want the fundamental constituent unit of my strings to be the thing that the string is conceptually made of. When I'm ready to turn it into a sequence of bytes, I'll do that myself.
a) low-level mucking around like normalization -- a fringe use
b) a (in the general case) bad approximation to characters -- the normal use, which will produce bad results for various "atypical" cases
Now, personally, I'd often rather muck around with bytes because frequently they are a more useful abstraction than codepoints and lots of in practice important text operations can be performed quite fine (and efficiently) on them, even if the text is full of non-ascii characters.
Firstly, you can _still_ work entirely in bytes in Python 3 if you want to. But you can't call print on them... For good reason! You don't know the encoding to use to display them!
When we transitioned, there were a lot of subtle issues in our code due to our almost cargo cult usage of encoded/decode that ended with a lot of redundant transformations. Loads of that disappeared afterwards.
You can still operate in "Python 2 mode" in Python 3. The only exception I can think of is the difficulty in getting a bytestream version of standard output.
Also... Python 2 is decidably not like Go or Rust. The simplest example is how json.dumps would return bytes or a Unicode string depending on the keys on your object. No respect for typing
sys.{stderr, stdout, stdin}.buffer
For every project that ports, the risk goes down for the rest (of those who still need to move), as more bugs are uncovered and more lessons/best-practices have been learned.
Hence porting late is an advantage from a cost/risk point..
Also, if there are no strong technical people advocating for the change, most organisation only see porting as a (unnecessary) costly exercise..
I think the judge is still out on whether that is really disproved, but right now you are likely correct :)
Python 3 is different but not necessarily better in this regard. While I hated the encode/decode on .. every .. single .. function call, py3 pretends a lot of bytestreams are instead unicode (file and path names, for example). This can lead to bugs. It's not that it wasn't worth it, but it could have been done a lot better.
> Which is probably also why Perl6 choose to go the same route
Not at all. Perl 5 did unicode right. The difference between character, glyph, code point and byte is clear and uniform throughout the entire stack. Python could have copied this and it would have been awesome.
It's implausible to expect Python programmers to correctly handle this, hence fsencode/fsdecode and surrogate escaping.
The problem is universal to every time you interface with the operating system. Sometimes it's locale dependent too and you never know beforehand so a program might blow up in the hands of a customer. While the encode/decode process was tiresome at least it gave predictable results.
If you have to work around your runtime in order to give the parameters you want to the underlying syscalls, you are doing it wrong. The premise to move to unicode was a laudable one, but I wish more attention had been paid to how other languages do it, because anything worse than what Perl 5 did more than a decade earlier isn't that great.
Python's behaviour is only problematic when all of the below is true:
- Running on Linux and,
- using glibc and,
- glibc's locale files were deleted or the locale is otherwise not configured and,
- you try to read/write non-ASCII file names. (In this scenario Python falls back to the only locale it can process by itself, which is 7-bit ASCII -- the problem is not ASCII, rather the 7-bit restriction. I believe there is a bug open that suggests changing this fallback to UTF-8, which would solve this problem.).
The only time I saw that was with rear which did all of the above on purpose to save like 5 MB on the generated images. In all other cases there is no problem with any kind of file name.
Note that this is a severely broken system configuration where a lot of other stuff falls apart as well.
Python2 unicode is not broken, the basic type str() is broken because it defaults to ascii.
What specific issues do you mean?
I am not sure, you are correct that py2-unicode == py3-str and py3-bytes == py2-str ... But even if (and I must admit that it is more then 5 years since I wrote py2 of any length)..
The value is... in py3 "str" is the ideal string, which does things the way humans expect.. When you need to pump in or out bytes, the class is called "bytes" and requires an explicit conversion, where you get prompted to think about encodings.. (but you don't need to think about it all the time as you would with byte-wrangling)
This way, people not well-versed in the horrible mess of encodings are going to be safe by default... If they do something unsupported they get an exception, not something that is wrong... That alone is worth it..
Also, the full stack of python is working correctly with unicode, you don't have to handle two different "string" classes ("str" and "unicode") where one of them is like to do the wrong thing..
One often overlook thing is that py3 defaults to utf8 source code, in the old days integrating stuff from files with non-ascii chars in different encodings was a nightmare...
You can already remove the implicit conversion in Python 2 (sys.setdefaultencoding(None)), it will break pretty much every non-trivial software as the standard library itself does not have strict separation and many of its "string" API operate on bytes.
> JavaScript has this strict mode, albeit to solve a different problem
That it solves a completely different and almost entirely syntactic issue is a bit of a difference there, you'll note that despite all of its changes the ES committee has refused to come even close to touching strings.