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

Go To StackoverFlow.com

3

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.

2012-04-05 15:22
by Dan Roberts
Niklas explained why 'in memory' is not possible. I routinely use IDLE for iterative development. After running an edited file, one can interact with the live process before editing some more. No execfile or the 3.x equivalent is needed - Terry Jan Reedy 2016-02-09 17:32


5

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

2012-04-05 15:27
by Niklas B.
You could also use Eclipse/PyDev with breakpoints and the Expressions pain in the debug view to execute arbitrary code from any point in your program - Silas Ray 2012-04-05 15:29
@sr2222: Not everyone likes Eclipse (I hate it, it's really a pain in the ass compared to any decent text editor like EMACS or Vim, which also have very good REPL integration) - Niklas B. 2012-04-05 15:31
Thanks for the suggestions. I was not familiar with execfile (sounds familiar though) or using the Expressions pane - Dan Roberts 2012-04-05 15:33
@NiklasB, fair enough. But not everybody hates it either. : - Silas Ray 2012-04-05 15:33
@sr2222: Yes, you have a point. I added this to the answer - Niklas B. 2012-04-05 15:39
@Dan: I extended the explanation a bit, check the edit : - Niklas B. 2012-04-05 15:39
@sr2222: The expression pane wasn't a generic REPL as I was hoping. However after some searching around I found that the console becomes interactive during debugging. This is going to be extremely helpful - Dan Roberts 2012-04-05 15:59
@DanRoberts oh yes, that too. : - Silas Ray 2012-04-05 17:30
Ads