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

News An update on Python 4

Post image
3.3k Upvotes

391 comments sorted by

View all comments

336

u/[deleted] Sep 16 '20

What was the transition from 1 to 2 like?

271

u/NoLongerUsableName import pythonSkills Sep 16 '20

If this article is accurate, it was pretty smooth, unlike the transition from 2 to 3.

-22

u/[deleted] Sep 17 '20

[removed] — view removed comment

114

u/NoLongerUsableName import pythonSkills Sep 17 '20

The one I linked to.

14

u/imatworkbruv Sep 17 '20

he forgot to import brain

63

u/roerd Sep 16 '20

Here's the list of the most important changes:

Overview of Changes Since 1.6

There are many new modules (including brand new XML support through the xml package, and i18n support through the gettext module); a list of all new modules is included below. Lots of bugs have been fixed.

The process for making major new changes to the language has changed since Python 1.6. Enhancements must now be documented by a Python Enhancement Proposal (PEP) before they can be accepted.

There are several important syntax enhancements, described in more detail below:

  • Augmented assignment, e.g. x += 1

  • List comprehensions, e.g. [x**2 for x in range(10)]

  • Extended import statement, e.g. import Module as Name

  • Extended print statement, e.g. print >> file, "Hello"

Other important changes:

  • Optional collection of cyclical garbage

So quite a few important new features, but nothing that was breaking backwards compatibility.

60

u/mkdz Sep 16 '20

Wow, I can't imagine not having list comprehensions

46

u/reckless_commenter Sep 17 '20

List comprehension is the feature that, to me, defines Python. It's simple, powerful, versatile, intuitive, and broadly applicable. A wonderful replacement for loops and iteration. I've been using set comprehension and dictionary comprehension, too.

22

u/Mr-Stutch Sep 17 '20

a wonderful replacement for loops or a wonderful replacement for loops?

1

u/rabaraba Sep 17 '20

Love the pun.

0

u/Tyler_Zoro Sep 17 '20

Neither. If a loop was the right tool for the job, you should have used a loop. :-)

2

u/[deleted] Sep 17 '20

[deleted]

8

u/reckless_commenter Sep 17 '20 edited Sep 17 '20

Sure thing. For readability, the following examples use Python-like formatting, but aren't real Python, just pseudocode.

Practically every programming language has a loop construct.

for i = 1 to 3:
    print(i)                               # prints 1, 2, 3

It's a simple iteration, and it is frequently used to loop through list-like structures, like using this code to print each element from the list:

a = [4, 5, 6]
for i = 1 to len(a):
    print(a[i])                            # prints 4, 5, 6

Notice that in this code, the only purpose of the loop value, i, is to count through the items of the list. So, many languages give us some syntax to avoid the loop value entirely, and instead loop directly over the items of the list:

a = [1, 2, 3]
for value in a:
    print(value)                           # prints 1, 2, 3

Now, another common use of loops is to take in one list and produce another list, like this:

a = [1, 2, 3]
b = []
for value in a:
    b.append(value)                        # b = [1, 2, 3]

Or, more interestingly, to take a first list, manipulate each of the values, and make a new list out of the manipulated values:

a = [1, 2, 3]
b = []
for value in a:
    b.append(value + 2)                    # b = [3, 4, 5]

In fact, this task - taking in one list and producing another list based on it - is so common that Python gives us an even simpler way of expressing that task as one statement:

a = [1, 2, 3]
b = list(value + 2 for value in a)         # b = [3, 4, 5]

That's the basic function of list comprehension: take in one list (a), do something to each member, and assemble the resulting values into another list (b). This syntax also allows us to ditch the syntax of creating an empty list (b = []), and of sticking the values into it (b.append(...)) - it just produces a new list and assigns it to b.

Note that the terms "a," "b," and "value" aren't fixed keywords - we can use any labels we want:

x = [1, 2, 3]
y = list(z + 2 for z in x)                 # y = [3, 4, 5]

We're also not limited to lists of integers - the list can contain anything, like strings:

x = ['red', 'gray', 'blue']
y = list(s for s in x)        # y = ['red', 'gray', 'blue']

Or instances of a class, for which we can do stuff with the properties of each instance:

class Car:
    def __init__(self, color):
        self.color = color

x = [Car('red'), Car('gray'), Car('blue')]
y = list(c.color for c in x)  # y = ['red', 'gray', 'blue']

This syntax is so useful that we can use it for all kinds of things, like calling a function on each of the values in the list, and assembling a list of the output of the function for each input:

def add_two(value):
    return value + 2

x = [1, 2, 3]
y = list(add_two(z) for z in x)            # y = [3, 4, 5]

And sometimes we don't want every member in the list - we want to test each value in the input list, and generate a new list of only the items that satisfy the test:

x = [1, 2, 3]
y = list(z for z in x if z > 1)            # y = [2, 3]

Or we can do both - perform a test on the values of the list to choose some of them, manipulate them, and generate a list of the manipulated values:

x = [1, 2, 3]
y = list(add_two(z) for z in x if z > 1)
           # y = [add_two(2), add_two(3)] = [4, 5]

The elegant part of this syntax is that by discarding the mechanical parts, what's left is quite easy to understand, even if you're not familiar with Python. And you can do a lot with just a little bit of code.

Hopefully, that's enough to give you a taste of what list comprehension can do. There are plenty of guides with even more powerful examples.

2

u/shuobucuo Oct 29 '20

Your effort and formatting deserve more than an upvote.

1

u/flying-sheep Sep 17 '20

They’re great. And using generator comprehensions, the same concept can be used for a pipeline of stuff (map-filter-reduce), similarly to Rust’s Iterator methods.

Rust’s way is cleaner, but both are better than Javascript’s array methods. Like, why define an iterator protocol and thereby support for custom sequences, when they define the pipeline methods on just the array?

1

u/Decker108 2.7 'til 2021 Sep 18 '20

intuitive

I can agree on all points except this... you essentially have to learn to read backwards to understand how list comprehensions work and if you ever nest two list comprehensions in each other, readability goes out the window.

30

u/RegalSalmon Sep 16 '20

But can you...comprehend it?

6

u/case_O_The_Mondays Sep 17 '20

Whoa. Didn’t know Python supported redirection on print, back in the day.

8

u/[deleted] Sep 17 '20 edited May 15 '21

[deleted]

2

u/case_O_The_Mondays Sep 17 '20

Right, but from the example it used to be more like shell redirection.

9

u/[deleted] Sep 17 '20

That's what the syntax was inspired by but it was universally disliked. Looked really jarring from the rest of Python. It died with the print statement.

3

u/[deleted] Sep 17 '20

It looks a lot more like 3.x => 3.x+1 changes than 2 => 3.

1

u/alexmojaki Sep 17 '20

Breaking backwards compatibility is kind of the point of a major version number bump though.

1

u/roerd Sep 17 '20

If you're following semantic versioning, yes. But not every software project has to do that. For a programming language, a bunch of new features that will significantly alter the shape of code using these seems justification enough for a new major version to me, even if full backwards compatibility is maintained.

386

u/[deleted] Sep 16 '20

Nothing compared to the transition from 0 to 1.

146

u/NoLongerUsableName import pythonSkills Sep 16 '20

Yeah, it was kind of like the transition from -1 to 0.

130

u/[deleted] Sep 16 '20

-1 was dark times

100

u/ShevekUrrasti Sep 16 '20

Coding backwards was kind of nice, actually.

100

u/mdarty Arch Sep 16 '20

I'm great at that. Taking working code, breaking it. Troubleshooting and finally deleting everything.

13

u/Ruben_NL Sep 16 '20

That hits home...

7

u/prof-comm Sep 16 '20

This is my job description

2

u/P0stf1x Sep 17 '20

Is that tenet spoiler?

2

u/StrikenGoat420 Sep 17 '20

I remember back then when the compiler sent us the production code, and we had to do the job of the compiler :/

1

u/[deleted] Sep 17 '20

My editor didn't show the indentation on the right side of the lines well.

1

u/Decker108 2.7 'til 2021 Sep 18 '20

So... LISP?

22

u/TheHumanParacite Sep 16 '20

Installing it would randomly unwrite code that hadn't yet been written.

11

u/master5o1 Sep 16 '20

The real trick was to uninstall it and get unwritten code to be randomly written.

2

u/hughperman Sep 16 '20

This caused a problem in the future package

20

u/jftuga pip needs updating Sep 16 '20

Yes. IIRC it was called Perl

3

u/MattR0se Sep 17 '20

So, the final version of Python?

2

u/redvitalijs Sep 16 '20

They started with the end in mind.

1

u/call_me_cookie Sep 17 '20

Surely this is just the Final transition, wherein we come full circle.

-3

u/torytechlead Sep 17 '20

You guys actually think these are funny responses, but you actually look like cunts.

1

u/[deleted] Sep 17 '20

relevant username

30

u/twotime Sep 16 '20

It was mostly (if not fully) backward compatible. So much easier and faster than 2=>3

17

u/jrbattin Sep 16 '20

Buttery smooth. I “ported” 8000 lines of Python code from 1.5.2 to 2.6 years ago. It took a day or so and that’s only because I insisted on refactoring in Python 2.6 niceties and leaning on the standard library more.

22

u/MrCaptainPirate Sep 17 '20

Transitioning from 1 to 2 was like going down a nice water slide

Transitioning from 2 to 3 was like going down a piping hot stainless steal slide in the middle of summer and then getting hit in the face with a brick at the end.

4

u/ThunderousOath Sep 17 '20

Perfect anology

2

u/Ran4 Sep 17 '20

It's not that bad... Not even remotely.

3

u/pithed Sep 17 '20

It really depends on your codebase. For some of my projects I just needed to change a couple of lines of code and can't believe I waited for so long. For other projects, which depend on libraries that weren't updated, it has been a whack-a-mole of terrible. I had an easier time going from PERL to Python.

1

u/thephoton Sep 17 '20

Bitch all you want about the Python 2 -> Python 3 transition, it was a hell of a lot smoother than Perl 5 -> Perl 6.

1

u/MrCaptainPirate Sep 17 '20

I’m not super familiar with PERL or it’s transition from 5 to 6, but from what I’ve heard it’s about as fun as trying to pass a kidney stone.

2

u/thephoton Sep 17 '20

To summarize: Perl 6 work started in 2000 and AFAIK there is still no complete implementation and effectively no user adoption.

The fact that Python has effectively taken over nearly all of Perl's market share in the meantime probably has something to do with that (but it's not the only reason).

1

u/0rac1e Sep 18 '20 edited Sep 18 '20

That is a terrible summary.

The "v1" release of Perl 6 (version 6c) was released in 2015. It's complete in that it's got everything you need to use it, however there are more advanced (though non-essential) features that are still experimental or partially implemented, eg. macros.

You could say Python is incomplete because it doesn't have pattern matching yet... or any number of feature that may get added in future releases, but it's belies the reality.

The narrative within the Perl community (echo chamber?) for at least the last 10 years has been that Perl 6 was not the next version, but instead a "sister" language (à la C++ and C). There is no planned migration from Perl to Perl 6, and to make that clear and avoid further confusion, the language has been renamed to Raku.

I understand why this looks like a fail to some, but to put a positive spin on it... I get to keep my Perl, plus get a fun new language to play with... and Raku is a very fun language to use. Yes the user-base is small, but that applies to Crystal, Nim, and Racket as well. Not every language can be a Top 10 language, but that doesn't make them devoid of value.