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

Auto-Indentation and Contextual History

00:00 Auto-Indentation and Contextual History. When you write long blocks of code in the Python standard REPL, you must correctly indent each line yourself. This can be tedious, error-prone, and unnatural if you’re used to writing code in a full-fledged editor.

00:18 Fortunately, bpython automatically adds the appropriate amount of indentation to the next line when you press the Enter key.

00:31 The default indentation in bpython is four spaces, which complies with the Python style described in a document called PEP 8. However, you can change the corresponding tab length option in bpython’s configuration if you prefer a different indentation size.

00:48 To exit the current block of code, you can hit Enter without typing anything on that line or Backspace. Either will reduce the indentation level by one.

01:06 The standard Python REPL keeps an unlimited history of the inline instructions that you type previously, even those from finished interpreter sessions. You can find your command history in a file named .python_history located in your user’s home directory. On the other hand, your bpython history is stored separately in a file called .pythonhist and is limited to a hundred lines by default, although you can increase that limit in the configuration.

01:35 Despite these differences, both the standard Python REPL and bpython conceptually support the same basic commands to access the history. bpython also maintains a contextual history with results.

01:48 Depending on where you are in your code, you can browse the history by using the arrow keys on your keyboard. The up arrow goes back in time, and the down arrow goes forward in time one line of code at a time.

02:02 You can hit Enter to confirm your choice and reuse one of the old instructions.

02:26 Notice how the historical suggestions offered by bpython don’t always follow their chronological order. Instead, bpython filters out suggestions that wouldn’t fit the context on your current indentation level.

02:41 Unlike the standard Python REPL, in bpython, history also comes into play when you start typing a line of code that’s already been executed before. As soon as bpython finds a historical entry that begins with a matching character sequence, it will show a grayed-out completion.

02:58 You can accept it by pressing the right arrow on your keyboard to have it auto-completed, or you can ignore it by typing something else over it.

03:14 In the next section of the course, you’ll take a look at how bpython can avoid you needing to switch to other applications during the course of development.

Become a Member to join the conversation.