Question

Sometimes I'll be working with, say, a list of thousands of items in IDLE, and accidently print it out to the shell. When this happens, it crashes or at least very significaly slows down IDLE. As you can imagine, this is extremely inconvenient. Is there a way to make it, rather than printing the entire thing, just give me a summarised [1, 2, ...] output? Any help would be much appreciated.

Was it helpful?

Solution

As above, try a custom print function like:

def my_print(obj):  
    if hasattr(obj, '__len__') and len(obj) > 100:  
        print '... omitted object of %s with length %d ...' % (type(obj), len(obj))  
    else: print obj

OTHER TIPS

Use IPython as shell instead.

You could use custom print function.

In Python 3, since print is a function, you should be able to "override" it. (I don't have it installed so I can't try it out to make sure.) Probably not recommended for real applications but if you're just trying things out, it would be okay I suppose.

It would go something like:

def myprint(*args):
    # write the function as described by other people
print = myprint

The Squeezer extension for IDLE was written to do just this. From the description on Pypi:

IDLE can hang if very long output is printed. To avoid this, the Squeezer extensions catches any output longer than 80 lines of text (configurable) and displays a rectangular box instead:

Squeezer, and many other IDLE extensions are included in IdleX.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top