r/Python • Freelancer. AnyFactor.xyz • Sep 16 '20

News An update on Python 4

Post image
3.3k Upvotes

389 comments sorted by

View all comments

95

u/vallas25 Sep 16 '20

Can someone explain point 2 for me? I'm quite new to python programming

282

u/[deleted] Sep 16 '20

Think what he is saying, there will never be a Python 4 and if there is, it will be nothing like python as we know it. It will be like a new language

The transition from python 2 to 3 was an absolute nightmare and they had to support python2 for *ten years* because so many companies refused to transition. The point they're making is that they won't break the whole freaking language if they create a python 4.

77

u/panzerex Sep 16 '20

Why was so much breaking necessary to get Python 3?

73

u/flying-sheep Sep 16 '20

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.

21

u/miggaz_elquez Sep 16 '20

you can write {*()} to have an empty set if you want

32

u/crossroads1112 Sep 16 '20

Thanks I hate it.

26

u/Brainix Sep 17 '20

My favorite thing about {*()} is that you don't even save any characters over typing set(). 😂

2

u/miggaz_elquez Sep 17 '20

Yes but it's a lot more fun.

6

u/mestia Sep 16 '20

And how this is better then Perl's sigils?

0

u/[deleted] Sep 16 '20

[deleted]

7

u/GummyKibble Sep 17 '20

Generally under a full moon at midnight.

7

u/stevenjd Sep 17 '20

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.

5

u/ThePoultryWhisperer Sep 17 '20

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.

→ More replies (3)
→ More replies (1)

33

u/irrelevantPseudonym Sep 16 '20

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().

0

u/[deleted] Sep 16 '20

[deleted]

12

u/hillgod Sep 16 '20

It's definitely not an anti-pattern, and, in fact, the literals perform faster.

→ More replies (12)

3

u/flying-sheep Sep 16 '20

Compare () vs one, vs one, two.

() is also a special case here.

5

u/irrelevantPseudonym Sep 16 '20

I don't think () is the special case. I think (2) not being a tuple is the special case.

19

u/ayy_ess Sep 16 '20

(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

5

u/BooparinoBR Sep 16 '20

Thanks, I have never though about tuples like this

2

u/flying-sheep Sep 16 '20

Exactamente

2

u/TheIncorrigible1 `__import__('rich').get_console().log(':100:')` Sep 17 '20

Fun-fact, () (unit) is literally a special case in Python. It is a singleton and all instances of () point to the same memory.

8

u/james_pic Sep 16 '20

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.

→ More replies (3)

5

u/zurtex Sep 16 '20

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.

2

u/[deleted] Sep 16 '20

Oooooh so that's why I'm confused each time I read what bytes does?

4

u/flying-sheep Sep 16 '20

Maybe, but maybe it's because you didn't have an introduction to binary yet.

→ More replies (2)

4

u/[deleted] Sep 17 '20

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.

→ More replies (1)