Search code examples
pythonpython-idle

Can a python IDLE be used for iterative/in-memory development?


I'm not sure if I worded the subject correctly but essentially I'm curious if someone can develop code in the Python IDLE, or a similar tool, and then through some command spit out the current code in memory. I believe I did this previously when going through a Lisp book and recall it being a very different approach than the usual re-running of static files. Any suggestions as to how to do this or something similar? Thanks

UPDATE I ended up using a combination of the IDLE using execfile and reload commands, while editing code in a separate editor (eclipse/pydev). I changed my "main" file so that nothing executes immediately when execfile is called on it. Code in the main file and modules imported are loaded into the current scope/stack so as I'm writing new code or an error occurs I can test directly in the IDLE command line. Once I have found the problem or way forward I then update code in editor, run reload(module) for updated modules, then execfile(path) on the main file.


Solution

  • The reason why this is sensible with LISP is that every LISP programs is just a bunch of macros and functions and the s-expressions can be formatted automatically into a nice representation.

    This isn't the case in Python, where you have more complex syntax (significant whitespace, decorators, lots of control structures, different types of string literals, ...) and more semantic elements (classes, functions, top-level code, ...), so this approach will not work very well here. The resulting code would get really messy for even the smallest of projects and the resulting code would still require a lot of "post-processing", somewhat annihilating the speed of development advantage.

    Instead, you can just write the code in a good text editor and

    • Use built-in functionality to integrate it with the REPL (EMACS and Vim have good support for this kind of stuff) or
    • load it into REPL using execfile, which will give you the comfort of good text editing and the interactivity of the prompt.
    • along with the program, write a suite of unit tests. This is to be recommended for any non-trivial piece of software and automates the testing of your code, so you'll have to spend less time in an interactive prompt, manually checking if a function works correctly.

    You could also grab a more fully-featured IDE that supports code evaluation and full-blown debugging (PyDev is an example here, thanks to sr2222).