
PYTHON — Auto-Indentation in Python- A Contextual History
In the software world, the moment you start using someone else’s software, you are living in their world, under their philosophy. — Richard Stallman
Auto-Indentation and Contextual History in Python
When writing long blocks of code in the Python standard REPL, manually indenting each line can be tedious, error-prone, and unnatural if you’re used to writing code in a full-fledged editor. Fortunately, bpython provides a solution by automatically adding the appropriate amount of indentation to the next line when the Enter key is pressed.
The default indentation in bpython is four spaces, compliant with the Python style described in PEP 8. However, you can change the corresponding tab length option in bpython’s configuration if you prefer a different indentation size.
To exit the current block of code, hitting Enter without typing anything on that line or Backspace reduces the indentation level by one.
Unlike the standard Python REPL, bpython maintains a contextual history with results. Depending on where you are in your code, you can browse the history 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. You can hit Enter to confirm your choice and reuse one of the old instructions.
Moreover, 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. Additionally, history 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. You can accept it by pressing the right arrow on your keyboard to have it auto-completed or ignore it by typing something else over it.
To access the history in the standard Python REPL, you can find your command history in a file named .python_history located in your user’s home directory. On the other hand, 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.
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.






