views:

484

answers:

10

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?

+5  A: 

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!

wsorenson
+10  A: 

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.

Robert Harvey
+2  A: 

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". :)

jonesy
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`.
Mike DeSimone
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. :)
jonesy
+2  A: 

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

ghostdog74
+2  A: 

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.

Xavier Ho
+1  A: 

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.

inspectorG4dget
+13  A: 

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?

asmeurer
+1 Great post! (it's a same I can't vote it twice)
Khelben
`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.
Mike DeSimone
+2  A: 

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.

Amit
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.
Mike DeSimone
+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.
Amit
+1  A: 

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.

Mike DeSimone
A: 

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.

Andy Rose