Jesus H Christ, really?
Your headline could equally have been "Python 3.15 joins the 21st century". I can't wait for the next release where they add support for 32-bit numbers.
After a week’s delay, the new version of Python, v3.15, has been released. With it comes a proper profiler, an experimental just-in-time compiler, and a tightening of dictionary structures. It also offers a time-saving Lazy Import option for programs to not load modules until needed, the implementation of which ironically …
Strings have been UTF-8 throughout the 3.x series, which introduced the bytes type. Initially this inverted declaration with the old u"" required to indicate unicode replaced by needing to use b"" to indicate bytes. This broke a lot of libraries and the implementation was slow, together the main reason why no-one switched to Python 3 as quickly as developers expected: all pain with no gain. Things started to change in Python 3.3, I think, which ran about as fast as Python 2.7.
I haven't used Python on Windows for a while but I think this is about the default encoding used by the interpreter itself. Data processing usually needs manual encoding control because those CSV files often aren't in the format they say they are. I'm using to swapping between utf-8 and latin-15 but had some "fun" a while back with some CSVs which used windows-1252 without a declaration. Figuring out the right encoding in Polars, which runs on Rust, was a barrel of laughs!
In the days of the 2.x series I worked on an open source Python project. A contributor from Venezuela added something to one of the files with a non-ASCII character, it may have been his name in the copyright (GPL) information, I can't recall for sure at this point. However, it worked fine for him but not on my PC because of different locale settings. He was able to fix it by adding a magic string to the top of the file which stated the encoding was UTF-8.
As already mentioned by someone else, strings have been UTF-8 by default since the start of the 3.x series, which was quite a while ago. A new data type called "bytes" was then added to handle raw sequences of bytes to handle use cases where you didn't want strings to have a specified encoding (much like how strings in C are just sequences of bytes).
Default UTF-8 support was in fact the main motivation for creating the 3.x series to begin with nearly 20 years ago. All the other changes were done on the basis that since UTF-8 support was going to break compatibility anyway, they may as well get all the compatibility breakages that were needed for reasons unrelated to UTF-8 done at once instead of doing it again later.
I think that all is being done now is cleaning up the remaining areas where the locale encoding on Windows still creeps in.
Some years ago, looking at a US government site, I was interested by the number of documents in Latin-1 with a Content-Encoding: UTF-8 header. I'm not sure that this was the government's fault, either: I think it was serving up what companies submitted to it. I got used to thinking about the errors= kwarg.
And in general, in cases where I didn't control the input I took care to do some reality checks on it. Is there a byte-order mark? Trust that. No? Try to decode the first 2^n bytes as UTF-8: if it works, continue, if not go with Latin-1 and errors=replace.
I suspect this is because Windows doesn't have native UTF8 support.
Ironically this is because Microsoft got ahead of the game. Faced with the legacy of DOS "codepages" and other character encodes they adopted Unicode. At the time, Unicode v1 only needed 16 bits (two bytes) to represent a character and nobody had invented UTF8. So they used 16 bits to represent a characters in their API - and even, sometimes, in on disk text files. (BOM! BOM!)
Meanwhile the unix community decided they didn't want horrible 16 bit wide characters. (Rightly, in my opinion.) Unicode grew to 20-ish bits. And people invented UTF8 which could use up to four bytes to encode all of the unicode character set and safely work in most existing 8 bit APIs. This left early-adopter Microsoft stuck with the now legacy 16 bit encoding (UTF16/UCS2/whatever we're calling it this week) as well a legacy 8 bit APIs that worked with codepages and translated internally into UTF16, potentially corrupting UTF8 in the process.
So if you need UTF8 support on Windows you have to roll your own translation into UFT16.
Linux doesn't have "native" UTF8 either. Filenames are just nul terminated array of bytes. File contents are just bytes. It is up to applications to interpret those bytes as something because the FS doesn't care that much. Typically that is by setting an environment variable LANG=C.UTF-8 (or en_GB.UTF-8 or some other locale) so if a string is displayed it is decoded and rendered with the proper symbols.
Windows had the misfortune of getting in early. A wide char held the 65536 code points of Unicode 1.x. But it turned out there were more than 65536 code points so subsequent Unicodes named the first 65536 code points the Basic Multilingual Plane (BMP) and added new symbols outside which were only accessible with surrogate pairs of wide chars. Countries like China later made a big deal of requiring Windows to support symbols outside of the BMP. So the new surrogates meant UCS2 became UTF-16. Using wide chars was especially a pain in the arse for programming because macros like _T, TCHAR, A2W, W2A, T2W etc would be peppered through the code if you had to build the same code in ANSI and Unicode.
But Linux was late to the party and never had the concept of wide/narrow char so it just made more sense to retrofit UTF-8.
UTF-8 and UTF-16 can be converted losslessly between each other so it really doesn't matter a lot although UTF-16 is more pain all round. The biggest advantage of UTF-8 is its a more natural format for languages like C and C++ and is usually more compact in memory. Websites like utf8everywhere.org make a good case for why it should be the default encoding unless you have an explicit reason not to use it. But you still have to manipulate byte arrays sensibly - slicing or iterating a byte at a time thinking its ASCII is no good.
Okay, when I implied Linux has native support I claimed too much. But it had a safe 8-bit path that Windows lacked which means the only way to talk to Windows is UTF16 and wchar_t.
And, yes, wide characters and all the macros are absolutely horrible. I've done too much of it. In fact, I did a quick google, Windows 10 has codepage 65001 which is utf8. But I can't see a way to set it programatically - and certainly not per process - to say this app is using UTF8 and then stick to the 8-bit "ANSI" path. (But perhaps I've missed a function.) Still, CHCP 65001 will be useful at the console...
If you wanted to do UTF-8 on Windows, the best way is to compile with UNICODE defined so all of Win32 is wide, but use 8-bit strings throughout, ensuring the contract / assumption in the code is that all strings are UTF-8. The only place you need to transform them to be wide is to Win32 calls where you could write some to_wide(s), from_wide(s) functions to transform them.
UTF-16 comes about because UCS-2, which can only do the Basic Multilingual Plane, can't in fact do all of Chinese (and Japanese/Korean) basically. So UCS-2 had to be extended to have the same variable-length thing UTF-8 has to represent those characters, which means you get the worst of both worlds - 16 bits per codepoint, no ASCII compatibility, except now that it's not necessarily 16 bits per codepoint you can't directly index things any more anyway which was previously the big selling point. It's all a bit of a clusterfuck (which remains part of the fundamental Windows NT/Win32 API).
Any oldtimer can tell you that coverage of UTF-8 on MS Windows has long been rather spasmodic. For several decades you could only rely on Windows-1252, Microsofts peculiar take on ISO 8859-15, with the Euro-sign on 80 instead of A4. Python switching to UTF-8 as default on MS Windows can be seen as evidence that MS Windows is finally catching up.
Maybe I don't understand "UTF-8 is an 8-bit binary encoding", but UTF-8 is between 8 and 4x8 bits.
UTF-8 is a variable-length binary encoding that uses 8-bit code units (bytes), but it is not restricted to 8 bits total.
The clever thing, AFAIK: for plain ASCII, you only need one byte, and only the usual 7 bits of that, with the MSBit set to 0. Plain ASCII. As soon as the MSBit is 1, you know more bytes are there ... for non-ASCII chars.
UTF-8 is basically a superset of ASCII, treating values above 127 as special escape characters. The issue is that any ANSI derived encoding, e.g. Latin-1 or Windows code pages uses values above 127 for their own purposes. e.g German umlauts are a byte in Latin-1 and 2 bytes in UTF-8.
Code that processes strings, even 8-bit strings needs to have a concrete understanding of what those bytes *are* in order to walk a string a symbol/character at a time. Code that attempts to search, slice, truncate, count, concatenate characters assuming 1 byte == 1 user perceived character is going to be in big trouble since it could cut an array in the middle of a continuation point. Even on Linux this is no different and libraries like icu are necessary to do it safely.
> Data sharing and configuration formats such as JSON, TOML, and YAML all require 8-bit encodings.
As they should. So it is good to have your language's library functions willing and able to process UTF-8 inside a string/buffer.
> Most code editors, from Visual Studio Code to Windows Notepad, prefer UTF-8
Now, being able to edit your UTF-8 data and config files in your code editor, very good.
But, preferring it? Always? Perhaps even, ulp, defaulting to it for a new file in an unnamed buffer before you've told it the file type?
Well, you do you and all that, but I'm sticking to 7-bit ASCII for all of my source code. And taking a very dim view of any third-party or generated code that doesn't stick to it.
Errors hiding away because of confusion between l and I, and even 1 on a bad font day, are bad enough. But deliberately opening the door - well, we know what havoc typosquatting can cause.
Yes, this draconian attitude means no fancy quotes when you print "the value of '$varname$' is '$value$'\n" but if it really needs that to keep the User happy, chances are they'll want it multilingual one day, so stick that into an internationalisation file on day one - and make it data. UTF-8 data good.
As soon as you live in a country where you need more than 7-bit ASCII in day-to-day life, you may find it necessary to include snippets in source code: it could be sample variables, or a comment, in any case UTF-8 as standard makes a lot of sense.
Firstly, take note of, from the comment you are replying to:
>> Well, you do you and all that, but I'm...
Now, taking my view from the "strings are all UTF-8 unless specified otherwise" code I've done since before the turn of the century, for international markets, even with non-English-speaking colleagues (they just sigh at the perennial Anglicisms, like "while" and "until", in the code) and even in MFC and Visual Studio 6 (hint: never, ever, ever, write your code using anything to do with TCHAR or things starting with _w).
> sample variables
I presume you actually meant "samples of the values that variables can take"? Nope, don't see that: anything where you are parsing for specific contents can use codepoints. Anything like showing the possible contents of a message being shown to the user - nope, refer to the translation files. That this variable may contain something else that needs to be expressed in Unicode? Well, every string could contain that. If I've missed what seems obvious to you, please give a concrete example - after all, nobody claims to have seen *every* possible situation, just - what I've seen so far has always been covered.
For pre-defined initialisation of variables, treat generic text like any other arbitrary data: to the code, it is just another binary blob, to the human, open the file/resource containing that in the appropriate editor.
> comments
Comments can be (arguably, *should* always be if they are of any length which *needs* to be fully expressed in your native tongue - and, yes, that DOES include English) added by means of a documentation system such as Doxygen. Which allows documentation to be placed into separate files alongside the "pure source code". Those files are a Good Thing, as you can organise your documentation to be readable (ref Literate Programming), include any examples that need to be given without stretching out the source files. The internal documentation of the source can then be translated, be improved without forcing a recompile, include diagrams in many forms - e.g. use Mermaid to help generate, use markup such as Markdown or LaTeX maths mode etc etc. You can even use your build system to read your source code and convert sections of that into diagrams/neat explanations. There are so many advantages to using source documentation systems that "dealing with some text in a wide range of non-ASCII forms" just falls out as a freebie.
> UTF-8 as standard makes a lot of sense
For data, bulk text, anything that your program manipulates - as I said before: yes, absolutely. If you only have one string representation in your programming language, that really, really, should be UTF-8 and good on Python for finally defaulting to it everywhere. (and if you have more than one, make sure the type declaration notes what they can - and can not - do).
For the literal source code: no, the risks due to oddball characters, accidentally or deliberately, extending the 'l' or 'I' confusion is not, in my opinion and experience, worth it.
As a final note to end on,
>> Well, you do you
To quote Smokey Robinson: I Second That Emotion.
There may be no rational motive to keep restricting your code to 7-bit ASCII, but staying on the safe side just feels better. I know from experience that other people may mangle your UTF-8 code if they don't use the proper tools.
There's one exception: I'm perfectly fine with <meta charset="UTF-8"> in html code. Anybody who can't deal with that: tant pis.
Python has had UTF-8 support for a very long time.
All that changed regarding UTF-8 is the fact that it's now used by default in certain situations where it wasn't the default before. Before, all you had to do was explicitly ask for it, with `encoding='utf-8'`.
You could have picked any number of actually exciting features. I would have chosen `frozendict` as the headline feature, because that's one that _was_ genuinely, and unintuitively, missing when it ought to have been there from the start!