Exploration and introspection with a good IDE¶
A good IDE indexes and analyzes the code in your project and all installed libraries and makes it navigable for the developer. This is also a very good way to learn through exploration. Learning is much easier if you have the source code and documentation directly at your fingertips.
PyCharm is my IDE of choice so everything in this toolkit that refers to an IDE will be about that. The capabilities for code exploration and introspection in PyCharm are built on top of the languages capabilities for introspection offering a convenient interface to them.
Reading documentation and source code¶
PyCharm offers a lot of actions to provide information about elements of your project and the libraries you use. The most useful here are:
Invoking any of those actions with the text cursor on a piece of code will provide more insight about that object.
You can query this information directly from the search popups to inspect the objects. As an example: If you want to know what you project contains to do with files you just type "file" in the "Search Everywhere" popup and get offered the file builtin, you can scroll down to that entry and invoke the Quick Documentation action and you see the documentation including a link to the external documentation on the web.
Structure Tool Window¶
The Structure Tool Window makes all classes and functions explorable from a bird's eye perspective.
There are a lot of ways to navigate in the code which is what you do a lot, when you work with larger code bases an/or work with a lot of libraries. The best documentation is often the source code and if it's easy to have a quick look at a module you are using this often is all you need in way of documentation.
You can navigate the code by activating the "Goto Declaration" Action or by holding down
Ctrl and clicking with the mouse on the object to go to the source code of the referenced object. The "Navigate Back" action (
Alt+left) brings you back to the old position just like the history function in a web browser. This way of navigating through the code lets you see the code as a collection of connected objects instead of as just text.
This (especially in combination with "Quick Documentation" and "Quick Definition" directly from the completion popup) is actually also a very effective way to explore the code. Pycharm has basic and SmartType completion. I don't quite get the smart in smartType though ... it just offers you lots of types which are currently not imported. Auto Completion is quite versatile.
PyCharm Auto completion works even in Markdown¶
You can even use completion and query features in this markdown document (this only works if you have this file open in PyCharm):
import inspect inspect.getcallargs(inspect.getargs)
Integrated visual debugger¶
A visual debugger visual debugger makes the objects easy to explore right in the IDE is also a good learning tool to compare your assumptions about the code to what is actually happening by stepping through the execution.
PythonTutor: Visualize code execution¶
PythonTutor is a great online tool to see exactly what happens in memory when Python code is executed. You can step through each line and see the state of the execution frames and the object. This will give you a good foundation on which you can reason about the code you are developing, because you really know what is going on, instead of just guessing.
Try this little introduction.
Ipython: Python interpreter on steroids¶
The Python interpreter has an interactive mode (more generally known as Read Eval Print Loop - REPL). This provides a way of interactively developing and exploring program code. Ipython enhances the interactive shell in many ways that provide more assistance to the explorer. Some central improvements are shell-like tab completion, shell commands, file completion, pretty tracebacks, magic commands (try
%quickref) and easier introspection of live objects (see main features). Getting help is much easier as well: instead of
help(sys) you can type
sys? to see the documentation and
sys?? to see the source code.
Jupyter: interactive notebooks¶
Jupyter notebooks (formerly known as Ipython notebooks) are a way to create documents that consist of code that can be executed directly from the document to fetch it's output. You can also use Markdown to create rich documents. The results can be saved and rendered in many different output formats. Github renders these Notebooks by default.