Exploring REPL Features
00:00 In the previous lesson, I gave an overview of the course. In this lesson I’ll be talking about the new features in the Python REPL. The REPL is Python’s interactive mode, which starts if you run the Python command on its own without passing it a file to execute. REPL has been rather stagnant until recently, but Python 3.13 added the ability to edit blocks of code, a long overdue feature.
00:24 Python 3.14 continues the improvements with two key items. First, it now supports tab completion when doing imports. And second, the little bit of color added in Python 3.13 has exploded into the beauty of full syntax highlighting.
00:39 Let’s head to the REPL to see some of these features. As I mentioned, the REPL now supports tab completion for imports. It works pretty much how you’d think.
00:50
Once you’ve typed import, you can start to type the name of a module. If you haven’t given Python enough info to choose something, it tells you that. There are just too many modules beginning with ‘s’ for it to do anything.
01:04
If I give it a few more letters, see that? It partially auto-completed. There are two modules in scope that start with sta: stat and statistics, so it expands to the common prefix and then tells you that it isn’t done.
01:20
And by giving it the extra ‘i’, it now fully auto-completes. There you go. I’ve imported statistics, and saved six key presses.
01:32 You may have noticed that the import was a pretty color. That’s because the REPL now supports syntax highlighting.
01:40 There are different colors for keywords, built-ins, data types, errors, and more. Granted, color is a question of taste. Personally, I don’t like this choice.
01:50 Red should be reserved for errors, and furthermore, the text contrast here is rather low. You can control the coloring if you like. Now, what I’m going to show you is a private module, which means it isn’t part of the public interface, and so it’s not guaranteed to be stable.
02:06 They may change this in the future, but for now, if you want to muck with the palette, I’m going to show you how to do it.
02:19
That’s a lot. The _colorize module contains the color control for the REPL and other color formatting in Python tools. The set_theme() function is key to changing your color preferences.
02:33
The default_theme object contains the default settings allowing you to explore them, and you use the SyntaxClass and ANSIColors objects to make changes to the syntax colors.
02:45 Let’s explore all that a little bit.
02:49
Yeah, I remember a second ago when I said that’s a lot? I guess I should have warned you that there would be a lot more a lot. The default_theme object contains a set of attributes for different colorization areas, and each of those has a set of attributes specifying their colors.
03:05 If you’re not familiar with ANSI color escape codes, that’s what all of these strings are. Put a pin in that for a second.
03:15
Looking at only the syntax area of default_theme, you can see attributes that correspond to the REPL’s syntax highlighting. These specify colors for the prompt, keywords, built-ins, the horrible red for comments, and more. Let me look at one of them.
03:35 This is the ANSI terminal escape sequence for keywords. It starts with a hex value that is a non-printable character that the terminal uses to control its appearance.
03:45
After the left square bracket, you get information on the color itself. The control codes can have multiple parts. This one has two. The 1 says to make it bold. Look up the screen where it says from or import.
03:58
You’ll see it’s actually bolded. The 34 indicates blue. You could scour the internet for these codes. Get very confused by the fact that ANSI codes and X term codes aren’t the same thing, and that when you Google one, you often get the results for the other.
04:14
Or you could use the ANSIColors convenience object.
04:21
ANSIColors has attributes containing a preset number of codes. The bold_blue attribute is the one I just explained, and it’s used for keywords. To change the palette, you call the set_theme() function.
04:38
The default_theme object has a copy_with() method, allowing you to use it as a base and make only the changes you want. That way you don’t have to type everything if you don’t want to.
04:53 Remember, the theme has different areas. I’m going to change just the syntax area, setting comments to yellow and built-ins to green.
05:13 And there you go. Now the comments are more readable, or to me at least. Color is a preference.
05:22
In addition to syntax highlighting in the REPL, several of the Python command-line utilities now use coloring as well. The image on the right is the output from unittest, and you can see how it helps you understand the error.
05:34
Of course, if you’ve been using pytest, this is old hat, but now it’s in the standard library as well.
05:42 Let’s face it, coding is the art of putting errors into text. Next up, how Python 3.14 helps you understand those errors a little bit better.
Become a Member to join the conversation.
