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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Editing Code in the Standard REPL

00:00 Editing Code in the Standard REPL. Most versions of the Python interpreter support code editing when running in interactive mode. These editing features include code history and basic code completion.

00:14 If you usually work on Unix-like systems, such as Linux and macOS, then you may be familiar with some of these features, which are present in the Bash and Korn shells.

00:25 These editing features are implemented using the GNU Readline library, which supports several useful styles of editing. First, a quick way to check whether or not you have code editing features available in your current setup is to press Control + P in the REPL’s prompt.

00:43 This shortcut should show the last code instruction in your history. If nothing happens or ^P appears on the screen as seen here, then code editing features aren’t available. Otherwise, you are ready to go.

01:05 The standard REPL logs a complete history of all the code that you’ve typed and run while working in interactive mode. This history is saved to a file called .python_history, typically located in your home directory.

01:25 While in interactive mode, you can browse this history using the arrow keys on your keyboard. With the up key, you can go back in history. With down, you can go forward in history.

01:41 Once you find the desired line of code, you can press Enter to confirm your selection and reuse the target instruction. Remember that every line in your code history keeps the original indentation that you used when you first typed the code, which is convenient and saves time.

02:00 The standard REPL provides basic completion capabilities for variable, object, and module names. This feature is automatically enabled when you start an interactive session.

02:13 To invoke the available code completions, you can type the starting characters of a given name and press the Tab key. This action triggers the completion search. If the search finds a match, then the name will automatically be completed for you, as seen here with the print() command. You can then complete the line as desired.

02:37 If the search finds more than one match, then you’ll have to press Tab again to get the entire list of matching names. The first time you press Tab, you may hear a beep, depending on your system.

02:48 Once you’ve seen the options available, you can complete the line as desired, once again making use of Tab for completion once you’ve typed a unique set of characters which Tab can complete.

03:04 If nothing appears after pressing Tab twice, then your search didn’t find any result, as seen here with the letter k.

03:15 The code completion system looks at these objects when running its search: Python keywords; built-in function, class, and object names; currently defined names, such as variables, functions, and classes; and imported module and package names.

03:34 When it comes to accessing attributes with dot notation, as seen on-screen, the code completion system will suggest completions from the target object’s attributes and methods.

03:46 Here you can see code which has defined an Object class with .x and .y attributes, and a .show_values() method.

04:12 With an instance of the Object class created, it’s possible to see available methods and attributes for the instance by pressing Tab twice.

04:20 The target attribute or method can then be typed and, if desired, completed with Tab.

04:29 The standard REPL provides a rather limited set of code editing features. However, they can be useful when you need to use an interactive session and don’t have a more advanced REPL.

04:40 These code editing features can improve your productivity and make your coding experience more pleasant. Learning keyboard shortcuts can significantly boost your productivity and efficiency when you’re working in a REPL session. For example, pressing Control + C on the REPL’s primary or secondary prompt cancels the input and returns to the primary prompt.

05:12 When you press Control + C, the interpreter raises a KeyboardInterrupt exception and immediately returns to the primary prompt.

05:24 If you press Control + C while code is running, then the KeyboardInterrupt exception interrupts the code’s execution and once more returns to the primary prompt.

05:33 This behavior is useful when you launch a long-running task that you are not willing to complete or when you accidentally run into an infinite loop. This example creates an infinite while loop.

05:48 You can break the loop by pressing Control + C on your keyboard. After this key combination, you are once more back to the primary prompt, and the REPL session is ready for new input.

06:02 The standard REPL provides many other interesting and useful keyboard shortcuts, and the table on-screen shows some of them. These shortcuts will help you be more proficient when typing and editing code in an interactive session.

06:19 In the next section of the course, you’ll look at how to get help while inside the REPL.

Become a Member to join the conversation.