Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Fix Mistakes Quickly

00:00 Fix Mistakes More Quickly. Code editing capability is another area where the Python standard REPL is lacking. Often you’ll find yourself retyping the same piece of code over and over because typos in nested blocks are difficult to fix without starting from scratch.

00:17 Even with bpython’s intelligent code suggestions and auto-completion, you’ll occasionally make mistakes or just change your mind about a particular implementation when typing out code.

00:28 The bpython REPL makes editing and reevaluating your code a breeze, offering many useful features that allow you to rewind one or more lines, edit code in an external editor, or reload imported modules.

00:43 Here you’ll learn how to use these neat features of bpython to quickly fix mistakes and typos or to change the implementation of your code. When you make a typo in the middle of a code block using the standard Python REPL, then you have to retype the entire block of code from scratch.

01:01 In bpython, you can press Ctrl and R to undo just one or the last few lines and replace them with new ones.

01:31 Be aware that each time you rewind even a single line of code, bpython runs the entire REPL session from the beginning again, including unedited lines that you’ve already executed.

01:43 Therefore, you should be extra careful about the potential side effects of mutating an external state, for example, when writing to a file database or network connection.

01:54 But even with this limitation, the rewind feature is a great way to fix a mistake that you spotted right after making it, but it’s ill-suited for fixing earlier errors or for making major changes.

02:05 For this, bpython has something else to offer. By pressing Ctrl and X on your keyboard, you can add or modify code located in the current line in your bpython REPL using an external code editor.

02:21 The line selected for editing may be empty, or it may already contain some Python instruction that bpython will first save to a temporary file for the external editor to load.

02:34 The default editor configured in bpython is vi, which is an historically accurate choice, but one which leads to a lot of head scratching. There’s an old joke about being able to generate random keypresses by seeing how people try to get out of vi, and this is for good reason.

02:50 While using vi is outside the scope of this course, a couple of basics will serve you well.

02:58 When you enter vi, you are in command mode, so you won’t be able to type any text. To change that, press I on your keyboard, and you’ll enter insert mode.

03:10 You can then type the code you want. As seen on-screen, the fib() function is being defined across multiple lines. Once

03:21 you finish typing, press Escape, and you’ll go back to command mode. The magic key combination that you need to send your code back to bpython is :wq as seen on screen. Once you hit Enter, the code will be sent back to bpython and evaluated.

03:40 While vi is a powerful editor, it’s not friendly, so you may want to choose a different editor that doesn’t require as much learning to master. You’ll find out how to change the code editor in bpython to something more modern, such as Visual Studio Code, later on in the course.

03:58 In addition to editing a single line or block of code, you can edit your entire REPL session using an external editor in bpython. Press F7 to open the current session in an editor.

04:14 This time you see the entire contents of the REPL session, including any outputs of the previous instructions in the form of comments. They’ll be ignored, as bpython will eventually reevaluate your session when you close the editor.

04:27 Once more, as we’re working in vi, we need to press I to enter insert mode and then to make the edits needed—in this case, to add in the missing call to fib(3).

04:39 Once the edit is complete, press Escape to go back to command mode and issue the :wq command to save the file and quit vi to return to bpython.

04:50 Once back in bpython, the session is reevaluated with the new edition in place.

04:58 There’s one more way to use a code editor with bpython, and that’s what you’ll learn about in the next section of the course.

Become a Member to join the conversation.