Because they changed a core datastructure. str used to be what bytes is today, but it also predated unicode (today called str). Therefore the bytes type was used for text and binary APIs.
When fixing all this, they had to break a lot of core APIs that used to accept bytes and today sensibly only accepts the unicode str.
And because of that huge change they also took the opportunity to change a few other idiosyncrasies.
My only gripe: One additional thing they should have changed is that {} should be the empty set and {:} should be the empty dict.
Because you don't have to memorise an arbitrary symbol, you just need to unpack the meaning of ordinary Python syntax that you're probably already using a thousand times a day.
{comma separated elements} is a set;
*spam unpacks spam as comma-separated elements;
() is an empty tuple;
so *() unpacks an empty tuple;
and {*()} creates a set from the elements you get when unpacking an empty tuple;
which is the empty set. You already knew that, or at least you already knew all the individual pieces. You just have to put them all together. Like Lego blocks. Or if you prefer, like programming.
You can make the same argument for many other things that are equally as unreadable at a glance. I know what all of the different pieces mean, but I still had to stop and think for a second. Reading and understanding set() is much faster and much more clear.
My only gripe: One additional thing they should have changed is that {} should be the empty set and {:} should be the empty dict.
Not sure I agree with that. It's awkward that you can't have a literal empty set, but having {:} would be inconsistent and a special case that (I think) would be worse than set().
(2) isn't a special case because tuples are declared in python with commas e.g. a = b, c. Brackets here are just used to clear up ambiguity e.g. 6 / 3 * 2 being 4 or 1. So (2) == 2 and (2,) == 2, == tuple ([2, ]). https://wiki.python.org/moin/TupleSyntax
Perhaps surprisingly (given what we know now about the migration process), the switch to unicode strings wasn't expected to be a big deal (it didn't even get its own PEP, and was included in a PEP of small changes for Python 3 - PEP 3100), and the other changes were seen as more break-y.
Set literals (e.g.{1, 2, 3}) were added in Python 2.7 and Python 3.1.
So to change the very common empty dict notation of {} would of required breaking backwards compatibility between 3.1 and 3.0 and either not being able to accurately back-port the feature to 2.7 or breaking compatibility between 2.7 and all other 2.x versions.
It was decided, fairly rightly, that it would of been too much churn for the fairly minimal aesthetic niceness / consistency benefits. {} is littered in code all the time whereas set() is pretty rare.
The explanation is confusing. Just ignore how it was before, because it was incorrect. In python 2 first mistake was mixing text with binary data. They introduced unicode type, but did so badly (implicit casting) it actually made things worse. Ironically if your application didn't use unicode type you might have less work converting it to work with python 3.
Right now it is:
str = text
bytes = binary data what's stored on disk, flies over network etc.
73
u/flying-sheep Sep 16 '20
Because they changed a core datastructure.
strused to be whatbytesis today, but it also predatedunicode(today calledstr). Therefore thebytestype was used for text and binary APIs.When fixing all this, they had to break a lot of core APIs that used to accept
bytesand today sensibly only accepts the unicodestr.And because of that huge change they also took the opportunity to change a few other idiosyncrasies.
My only gripe: One additional thing they should have changed is that
{}should be the empty set and{:}should be the empty dict.