Why use Python interactive mode?
Asked Answered
L

10

22

When I first started reading about Python, all of the tutorials have you use Python's Interactive Mode. It is difficult to save, write long programs, or edit your existing lines (for me at least). It seems like a far more difficult way of writing Python code than opening up a code.py file and running the interpreter on that file.

python code.py

I am coming from a Java background, so I have ingrained expectations of writing and compiling files for programs. I also know that a feature would not be so prominent in Python documentation if it were not somehow useful. So what am I missing?

Linis answered 19/4, 2010 at 1:39 Comment(5)
If you are looking for better interactive shell, you should take a look at dreampie (dreampie.sourceforge.net). The buffer system and history saving is incredibly useful.Fallen
ipython seems to be the best-of-breed of the console-mode shells. I prefer it to any of the GUI-based ones that I've tried, including dreampie. Of course I may be biased towards console-mode tools. I do find that editing doctest files is a better way to suss things out in cases where long strings of commands need to be run repeatedly, with small changes made. However the console is nice because it gives you a history of the various things you tried; you can get that somewhat less conveniently with doctests if you put them under revision control.Emmeram
IPython has a GUI as well with the notebook and qtconsole.Grandioso
I seriously doubt any non traditional language shell is used (or useful in reality) by 99.999999% of developers. I was excited about jshell thinking I could replace bash scripts with jshell scripts but the lack of basic features makes it nothing more than a toy for 12 year old programmers.Hancock
In fact it’s not just about strong typing. I find the jsc JavaScript shell useless too (though node.js I can’t speak to)Hancock
G
39

Let's see:

  • If you want to know how something works, you can just try it. There is no need to write up a file. I almost always scratch write my programs in the interpreter before coding them. It's not just for things that you don't know how they work in the programming language. I never remember what the correct arguments to range are to create, for example, [-2, -1, 0, 1]. I don't need to. I just have to fire up the interpreter and try stuff until I figure out it is range(-2, 2) (did that just now, actually).

  • You can use it as a calculator.

  • Python is a very introspective programming language. If you want to know anything about an object, you can just do dir(object). If you use IPython, you can even do object.<TAB> and it will tab-complete the methods and attributes of that object. That's way faster than looking stuff up in documentation or even in code.

  • help(anything) for documentation. It's way faster than any web interface.

  • Again, you have to use IPython (highly recommended), but you can time stuff. %timeit func1() and %timeit func2() is a common idiom to determine what is faster.

  • How often have you wanted to write a program to use once, and then never again. The fastest way to do this is to just do it in the Python interpreter. Sure, you have to be careful writing loops or functions (they must have the correct syntax the first time), but most stuff is just line by line, and you can play around with it.

  • Debugging. You don't need to put selective print statements in code to see what variables are when you write it in the interpreter. You just have to type >>> a, and it will show what a is. Nice again to see if you constructed something correctly. The building Python debugger pdb also uses the intrepeter functionality, so you can not only see what a variable is when debugging, but you can also manipulate or even change it without halting debugging.

When people say that Python is faster to develop in, I guarantee that this is a big part of what they are talking about.

Commenters: anything I am forgetting?

Grandioso answered 19/4, 2010 at 4:7 Comment(5)
help() is also available from the command line via pydoc. And ipython makes Python close to Matlab (well, Matlab's CLI at least) in interactivity. And even if you mess up a loop, you can just cursor up and fix it. Sometimes I even open a text editor window, work out some commands in it, and then copy and paste it to the Python interpreter. Botch an array index? Fix, copy and paste again. No need to save the file anywhere.Millburn
Add there an option (in iPython) to edit code snippets in an editor, and save (part of) your history of experiments into a file, which then can serve as base for real code. Having the chance to experiment improves productivity - classical equations for effort estimation do count on round trip time - on console, you have the round closed almost immediately.Strontianite
I wrote this before the IPython notebook, but these days, it's the best way to mix interactivity, editing, non-terminal features like plotting, and the ability to save things for later use.Grandioso
import pprint is indispensable for large and complicated dictionary/list/other-things-we-stole-from-JSON structures.Wagonage
At work we use Python to test and debug electronic hardware through a USB interface. It's nice to use the python -i switch in combination with any scripts that are used to set up a test. After they're executed, the interpreter drops back into interactive mode which can then be used to run different subsets of tests between fiddling with oscilloscopes and other test equipment on the bench.Icarus
C
13

REPL Loops (like Python's interactive mode) provide immediate feedback to the programmer. As such, you can rapidly write and test small pieces of code, and assemble those pieces into a larger program.

Castora answered 19/4, 2010 at 1:43 Comment(0)
T
5

You're talking about running Python in the console by simply typing "python"? That's just for little tests and for practicing with the language. It's very useful when learning the language and testing out other modules.

Of course any real software project is written in .py files and later executed by the interpreter!

Tagmemic answered 19/4, 2010 at 1:42 Comment(0)
Q
2

The Python interpreter is a least common denominator: you can run it on multiple platforms, and it acts the same way (modulo platform-specific modules), so it's pretty easy to get a newbie going with.

It's a lot easier to tell a newbie to launch the interpreter and "do this" than to have them open a file, type in some code, save it, make it executable, make sure python is in your PATH, or use a #! line, etc etc. Scrap all of that and just launch the interpreter. For simple examples, you can't beat it. It was never meant for long programs, so if you were using it for that, you probably missed the part of the tutorial that told you "longer scripts go in a file". :)

Quincyquindecagon answered 19/4, 2010 at 1:48 Comment(2)
Regarding the pound-bang (#!) line: I just use #!/usr/bin/env python these days. It makes env go find python on the user's $PATH.Millburn
Right, but then you're already assuming a *nix-like platform, and tutorials would, ideally, not make that assumption. Using the interpreter just means that no matter what platform you're on, the examples look the same, and as a tutorial author, you don't have to create two versions of the example, or write things like "Now, if you're on a Windows machine, you'll need to....". That kind of thing can really get distracting in a long tutorial with lots of examples. For the record though, I code only on *nix-like platforms, and unless I have a reason to do otherwise, I use 'env' as well. :)Quincyquindecagon
S
2

you use the interactive interpreter to test snippets of your code before you put them into your script.

Spinous answered 19/4, 2010 at 1:50 Comment(0)
R
2

As already mentioned, the Python interactive interpreter gives a quick and dirty way to test simple Python functions and/or code snippets.

I personally use the Python shell as a very quick way to perform simple Numerical operations (provided by the math module). I have my environment setup, so that the math module is automatically imported whenever I start a Python shell. In fact, its a good way to "market" Python to non-Pythoniasts. Show them how they can use Python as a neat scientific calculator, and for simple mathematical prototyping.

Romola answered 19/4, 2010 at 4:36 Comment(2)
I have never gotten bc to be a nice, simple, command-line calculator. Python is way better, and it's far lighter weight than Matlab.Millburn
+1 Mike. Folks in my lab mainly use MATLAB even for simple numerical tasks. I try to tell them about Python's interactive mode and its math module, whenever I can.Romola
M
2

One thing I use interactive mode for that others haven't mentioned: To see if a module is installed. Just fire up Python and try to import the module; if it dies, then your PYTHONPATH is broke or the module is not installed.

This is a great first step for "Hey, it's not working on my machine" or "Which Python did that get installed in, anyway" bugs.

Millburn answered 19/4, 2010 at 5:9 Comment(0)
M
1

I find the interactive interpreter very, very good for testing quick code, or to show others the Power of Python. Sometimes I use the interpreter as a handy calculator, too. It's amazing what you can do in a very short amount of time.

Aside from the built-in console, I also have to recommend Pyshell. It has auto-completion, and a decent syntax highlighting. You can also edit multiple lines of code at once. Of course, it's not perfect, but certainly better than the default python console.

Marthmartha answered 19/4, 2010 at 2:1 Comment(0)
L
1

When coding in Java, you almost always will have the API open in some browser window. However with the python interpreter, you can always import any module that you are thinking about using and check what it offers. You can also test the behavior of new methods that you are unsure of, to eliminate the "Oh! so THAT's how it works" as a source of bugs.

Lucubrate answered 19/4, 2010 at 3:21 Comment(0)
A
0

Interactive mode makes it easy to test code snippets before incorporating them into a larger program. If you use IDLE there's syntax highlighting and argument pop-ups to help you out. It's also a quick way of checking that you've figured out how to use a module without having to write a test program.

Apiarian answered 20/4, 2010 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.