Python REPL: issuing commands in advance to execute after block

This is a bit of an odd question; it came up in the context of a tool that exposes a Python API, which we spend a lot of time querying interactively from the REPL. The particular idiom causing issues is something like this:

for var in slow_generator_of_giant_list():
    stats = update(stats, var)
print stats

To enter this at the REPL, I can type this:

>>> for var in slow_generator_of_giant_list():
...    stats = update(stats, var)

If I now attempt to type the print, I get a syntax error due to improper indentation. (Or else I put the print inside the loop and do it on every iteration.)

But if I hit enter to go to the next line, the loop runs immediately, and I have to wait for it to finish, or type the print command in the face of possible output coming at me, etc.

Obviously I can define a function containing the above, and it might be worth saving into a file anyway, but in the general case we're constructing these on the fly, and it would be nice to have a way to "schedule" a command to run after the end of a loop from the REPL. In a language with block delimiters, I could of course put it after the ending delimiter (and any necessary statement separator). But my coworkers and I were stumped trying to do something similar here.

Is there perhaps an ugly abuse of Pythonic syntax that will do the trick that my coworkers and I couldn't think of? Or a recommended way to avoid the problem while still making it easy to throw together ad hoc interactive queries?

Thanks for any pointers.


Not beautiful, but this should work:

>>> mygen = slow_generator_of_giant_list()
>>> try:
...     while True: stats = update(stats,
... except StopIteration:
...     print stats

I would just say that you would find it easier just to not use the interactive shell for this.

It's not much effort to save a file and run it. You only have to keep it around for as long as you use it.

I actually have found this answering on SO. I keep a file open in my text editor with a terminal in the right directory, and just use it as a scratchpad for mocking up answers in.

Need Your Help

Active Report how to page break when dynamically add control to the page during report Start Event?

c# report page-break activereports

I am adding table base on my database and dynamically create the table from code behind. The problem is i want a page break after the table is created. Since i cannot add control on detail Event, I...

How can I get stack trace error in Swift?

ios exception swift

In Objective-C, Whenever an application crashes, I can get stack trace to see where is the last method that causes the error by using this code in AppDelegate

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.