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

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.

00:59 Otherwise, you are ready to go. 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.

02:23 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