Show notes
Python’s interactive shell (or REPL) is an easy way to try out quick python statements. You can launch the default python repl by typing python
in your terminal.
$ python
Python 2.7.14 (default, Nov 3 2017, 10:55:25)
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> bool('')
False
>>>
dir()
The built-in function dir()
can be used to list all the attributes and methods that are available in a module or an object.
>>> import math
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
help()
The built-in function help()
can be used to view the docstring for a function or a class.
>>> import math
>>> help(math.sin)
Help on built-in function sin in module math:
sin(...)
sin(x)
Return the sine of x (measured in radians).
bpython
BPython is an alternative python REPL with auto-completion, syntax-highlighting, undo, module reloading etc.
BPython can be installed using pip.
pip install bpython
Auto-completion
Auto-completion is enabled by default. As soon as we start typing, suggestions will pop up in a menu, which can be selected using the <tab>
key.
In addition to the completion of attributes and methods, bpython will also display the arguments and the docstring for a function.
Use the <F1>
key to view the help menu for bpython.
Undo
BPython has an undo feature that can be triggered by <Ctrl-R>
key. This will undo the previous statement entered into the REPL. This can be used to fix quick typos made while entering a python command.
Rewriting REPL History
BPython also allows the user to rewrite the entire history of the current session. This can be achieved by pressing <F7>
which will load the history of the current session into an external editor (vim by default). The user can then rearrange the commands in the history and save the file, which will reevaluate the entire session based on the newly rewritten history.
Module Reloading
Once we import a function from a module into the REPL, it is not possible to reload that module in the default Python shell.
>>> from binary_search import binsearch
>>> reload(binsearch)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: reload() argument must be module
>>> reload(binary_search)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'binary_search' is not defined
In bpython
a module can be reloaded by pressing <F6>
. This will reload the entire session which includes reloading the module from disk.
Pressing <F5>
in bpython will enable auto-reload. This will reload the session anytime one of the modules imported into bpython is changed on disk. This auto-reload feature relies on a module called watchdog
. Watchdog can be installed using pip install watchdog
.
References:
bpython: https://bpython-interpreter.org