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.
Will iPython works on Python 3?
Selinap
2009-03-04 22:20:26
+2
A:
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
marqueed
2009-03-12 00:21:44
A:
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
MatrixFrog
2009-06-14 00:56:35