I have minimal experience with Ruby in Visual Studio and used irb for quickly testing code snippets and unit testing. I’m wondering if Python offers something similar for interactive testing and experimenting with functions or classes.
If you’re coming from Ruby’s irb, the closest thing in Python is the interactive Python shell (REPL).
You just open your terminal and type:
Then you can experiment with code snippets, call functions, and create objects on the fly:
>>> def add(a, b):
... return a + b
...
>>> add(2, 3)
5
Why I love this:
-
It’s super lightweight and doesn’t require any setup.
-
Perfect for testing small bits of code quickly.
Tip: You can even import your modules while in the REPL and experiment interactively:
import my_module
my_module.my_function(10)
It’s simple, but extremely effective for quick testing.
I personally use IPython, which is like a supercharged REPL.
Install it with:
pip install ipython
Then just run:
ipython
Benefits over the default REPL:
-
Syntax highlighting and autocompletion.
-
Magic commands for quick experiments (%timeit, %run).
-
Easier navigation and better history support.
For example, you can test a function and time it interactively:
In [1]: def multiply(a, b):
...: return a * b
...:
In [2]: %timeit multiply(10, 20)
IPython makes testing and debugging your Python code much faster and more fun.
Another approach I swear by is Jupyter Notebooks.
It’s perfect for interactive experiments and even unit testing:
pip install notebook
jupyter notebook
You get a web-based interface where you can:
-
Write small test cells for functions or classes.
-
Run tests interactively without restarting your script.
-
Visualize outputs immediately, which is great for experimenting.
For example, you can test a function in one cell:
def divide(a, b):
return a / b
divide(10, 2)
And later tweak it in another cell, without touching the previous code. I love this for trying out new logic quickly.