Loading video player…

Improved REPL

00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll go over the improvements to the REPL.

00:07 When you run Python on its own without a script as an argument, you get the REPL. That’s short for read, evaluate, print, loop, and it’s one of my favorite places.

00:17 I spend a lot of time there playing around. If I can’t quite remember how something works, firing up the REPL and experimenting is a quick way to try something out before sticking it in my real program.

00:27 Up until now, the REPL’s been kind of creaky. In fact, that’s why there are a half dozen drop-in replacements for it out there. This latest release has rewritten it though, and most of the creakiness is gone.

00:40 The big complaint in the past was if you’re writing a multiline block in the REPL and you make a mistake, you used to have to start from scratch. This is gone now. Blocks are treated as whole things, and when you view them in the history, you can edit any part of the block.

00:56 Early on, some purists decided that the only commands in the REPL should be genuine Python. This caused strange things like when you typed exit, it would tell you that it wouldn’t do that, you had to type quit() as a function.

01:09 The new version now has a concept of commands. So far, the only commands available are quite simple, help, exit, and quit, but at least it doesn’t nag you to write it in function form anymore.

01:21 Both the REPL and error messages now support colorization giving a little extra highlighting when something goes wrong. There are also two new modes in the REPL.

01:30 One that shows your entire command history in a viewer and the other, which allows for easier pasting of multiline code. If something about the new REPL bugs you or the magic they’ve implemented doesn’t work in your favorite terminal, you can still get the old one back by setting the PYTHON_BASIC_REPL=1 environment variable.

01:49 Let me demo some of these new features for you in the terminal.

01:54 I’ve fired up the new REPL, and right away you can see a difference. For me, that difference is magenta. The prompt is now colorized. If you don’t like the colors, there are environment variables you can set to change them.

02:06 Dig into the docs if this isn’t your groove. Let me start with a simple assignment.

02:13 Nothing new here. Just proving it’s still a working REPL. Let’s try something bigger.

02:24 There is no nim, so I get an error. As you can see here, the error is also colorized, giving it a little more pop than it used to have. Now, for the feature I’ve been waiting a long time for.

02:36 Previously, if I wanted to correct this loop, I’d have to use up arrow to get the for line. Then up arrow again to get the print() line.

02:43 For a two-liner like this, it’s not a big deal, but for longer blocks it was painful. Now instead I push up arrow once and it gives me the entire block. And I can just move my arrow keys to make any of the changes I want.

03:00 I can also go up and edit the other parts of the block.

03:06 Then I go back down and hit Enter, and my fixed block runs. This is going to save me so much time and frustration. Another new feature is the addition of function keys.

03:17 Pressing F1 gives you the help screen. From here, I can look up a function and see its docs.

03:27 Then I use quit to get back to the prompt.

03:32 Instead of using F1, I can also use the help command. Note that it doesn’t use parentheses now.

03:46 I have noticed a little bit of double prompting here once in a while. Hopefully it’s something that’ll get polished in the bug fix release.

03:55 What comes after F1? Well, F2 of course. The F2 sequence is your command history. One thing to notice is that it starts at the back of the history. I don’t have a lot here so you can see what I just typed, but if you had a lot more history, what you had just typed would be down at the bottom.

04:13 You can press the N key to get there quickly and use the arrow keys to move around.

04:19 One of the things I like is that you don’t have to just use the arrow keys. If you’re one of those dinosaurs like me who uses vi, well, the vi bindings to move around, J and K, have been included as well.

04:32 You’ll notice here in the help that a bunch of control keys are too. Pretty much whatever the navigation is from your favorite editor will probably work. Down here at the bottom I’m going to quickly grab the code I just wrote into my paste buffer.

04:47 The last feature I’m going to show you is paste mode, which you get into by pressing F3.

04:57 Now that you’re in here, you can paste a block of code like I just copied.

05:02 It handles the multiple lines properly and turns them into a block. This is also a big time saver since in the old REPL you had to paste these line by line.

05:11 Pretty neat, huh? Two experimental performance features have been added to the latest version. I’ll talk about those next.

Become a Member to join the conversation.