What’s New in Python Docs
More at Real Python & Other Resources
What’s New in Python Docs
More at Real Python & Other Resources
00:00 The previous lesson covered Python 3.7 and data classes. This lesson covers Python 3.8 and 3.9 and includes demonstrations on the walrus operator as well as the equals sign inside of inside of an f-string. Python 3.8 was an impactful release, but not because of the features.
00:19
More on that later. It was the release that introduced the walrus operator that is :=
. Tilt your head to the side. It looks like the eyes and tusks of a walrus, hence the name.
00:30 This operator allows you to assign a value inside of a conditional statement. This release also added a useful little debugging tool, the use of the equals operator inside of an f-string field.
00:41 When you do this, the output shows the field’s name, the equal sign, and then the field’s value. Helpful if you’re throwing prints around for debugging. You’ll recall that Python 3.0 added the ability to force the use of keyword arguments in a function declaration.
00:56
3.8 topped that up with the ability to force positional arguments. You do that with the slash, so my compare()
declaration here has both the slash and the asterisk, meaning everything to the left of the slash has to be positional, to the right of the asterisk has to be a keyword.
01:13 And the things in between can be either.
01:17 Okay, let’s start with the walrus coo coo ca-choo. Well, eventually. Let me show you the reason it was created first.
01:28
See, Python doesn’t let you do that. Now if Python’s your first language, you might rightfully ask why would anyone want to, but loads of languages treat assignment as a truthy statement and in this case, would assign three to the number
and it would always evaluate to True
.
01:44 This is the cause of a common bug. If you mistype the double equals, you’ll get something that’s always true. Hence why Python disallowed it. And since I’m writing this in a Python’s 3.13 REPL, it even suggests I might have wanted the walrus operator.
01:58 Let me show you a coding pattern where being able to do something like this actually saves a bit of code.
02:06 Say I want the number and it’s square,
02:09
and then I want to check the size of the square printing yes if it was big enough. With the walrus operator, I can combine two of these lines here. The number
gets squared and put in the square
variable.
02:27 And since the walrus operator was used, the return value of the assignment is the result itself. Hence saving us the assignment line of code above. Note the use of the brackets here. They’re important.
02:39 Otherwise, what would get assigned is the comparison of the number squared and five,
02:50 And there you go. I’ll admit I’ve only used the walrus operator once or twice since it came out. It isn’t that big a deal, especially in this case, but it can be helpful if you’re dealing with complex comprehensions where assignment needs to happen inside of them.
03:04 Have you used it? I don’t have a need that often. Again, it’s, it’s kind of a really unique situation. I know a handful of people at Real Python that, again, going back to Geir Arne, he was very excited by this and I always see it in lots of versions of his code.
03:20 It’s sort of a shortcut. And I guess it, it depends on the patterns and the way that you think.
03:25 You know, the flip side of it, I was coming to Python, I was very glad of the double equals being the only syntax allowed inside of a comparison statement because I have shot myself in the foot in C many times with that mistake.
03:39 Yeah, so, so the reason for the walrus operator existing is actually a very good one.
03:45 And this is just a little quickie to show off that equals in an f-string thing I was talking about.
03:52 There’s an f-string without it.
03:56 And here’s one with it, kind of like the walrus operator, saves a tiny bit of typing.
04:02 This course is coming out about the time that a Python documentary is going to be released, and there’ll be a lot more details about this backstory because it is quite a contentious and Guido rage quit, as he called it, Python at the point.
04:20 He had been the Benevolent Dictator For Life, if you haven’t seen that abbreviation before BDFL. And so there was a big fight about this particular feature, the walrus operator to say it’s contentious.
04:33 I don’t even know. They’ll probably list how many different messages were in the board as this was being added. This again, is right about the time I’m starting.
04:41 And so I was listening to podcasts and things about this stuff and it was very interesting to kind of follow it up. It was interesting to cover this release.
04:50 I did the version of the course, which we’ll learn also about the new features here. And it was my introduction to what they decided as a solution, which is the Python Steering Council to be created to basically take over for the responsibilities of the benevolent dictator.
05:07 You probably might already know that Guido did come back and is involved again, but he’s not in the same role. And the Python Steering Council has taken over for that.
05:17 And there’s a bunch of rules that are set up in there and you can learn a little more about it. And I’ve had a few people from the steering council on the show as far as the podcast considered also. If you’d like to learn about the Python assignment expressions using the walrus operator, we have a nice tutorial and a course on the subject.
05:37 And I did the cool new features for Python 3.8 video course. And of course we have the written tutorial as reference.
05:46 Alright, welcome to Python 3.9. Here are the features we’re gonna highlight, starting with the union operator for dictionaries, which allow you to update or augment dictionaries, which is nice.
05:58
The zoneinfo
module was added, which was great for not having to bring in separate time zone libraries, which is really nice. And the PEG parser was introduced, which is something that Guido was working on in the background.
06:12 And so the PEG parser allowed for a lot of great features that you’re gonna see moving forward from Python 3.9.
06:22 If you’d like to learn a a little bit more about what PEG, the Parsing Expression Grammar does, there’s an overview. Guido wrote his thoughts as he went through this.
06:31 There’s a whole series, I think there’s like three different articles on Medium that you can kind of check out if you’d like a little more deeper insight into what was happening in the introduction of this in Python.
06:44
If you’d like to learn a little more about Python’s datetime
module and the introduction of the zoneinfo
that’s included inside this course, along with the written tutorial.
06:55 And this started a new trend where Mr. Trudeau was taking over the cool new features along with there’s the written tutorial, and we decided to do a podcast which featured Geir Arne Hjelle, Mr.
07:07 Trudeau, and myself discussing all the new features in Python 3.9 and moving forward.
07:15 Next up, far less trauma. Python 3.10 introduces structural pattern matching.
Become a Member to join the conversation.