[Python-ideas] Have REPL print less by default

Franklin? Lee leewangzhong+python at gmail.com
Sun Apr 17 21:04:11 EDT 2016


Two different (probably radical) ideas, with the same justification: reduce
often-useless output in the REPL, which can flood out terminal history and
overwhelm the user.

1. Limit the output per entered command: If you type into the REPL (AKA
interactive shell),

    list(range(n))

and you forgot that you set n to 10**10, the interpreter should not print
more than a page of output. Instead, it will print a few lines ("... and
approximately X more lines"), and tell you how to print more. (E.g. "Call
'_more()' for more. Call '_full()' for full output.")

Alternatively, have "less"-like behavior.

2. Only print a few parts of the stack trace. In particular, for a
recursive or mutually recursive function, if the error was due to maximum
recursion (is this reasonably distinguishable? the error is
`RuntimeError('maximum recursion depth exceeded')`), try to print each
function on the stack once each.

Again, there should be a message telling you how to get the full stacktrace
printed. EXACTLY how, preferably in a way that is easy to type, so that a
typo won't cause the trace to be lost. It should not use `sys.something()`,
because the user's first few encounters with this message will result in,
"NameError: name 'sys' is not defined".

A few possible rules to reduce stacktrace size:
- Always show the last (top) frame(?).
- Hide any other stdlib funcs directly below (above) it.
- If a function appears more than once in a row, show it once, with the
note, "(and X recursive calls)".
- If functions otherwise appears more than once (usually by mutual
recursion?), and there is a run of them, list them as, "(Mutual recursion:
'x' (5 times), 'y' (144 times), 'z' (13 times).)".

These two behaviors and their corresponding functions could go into a
special module which is (by default) loaded by the interactive shell. The
behaviors can be turned off with some command-line verbosity flag, or tuned
with command-line parameters (e.g. how many lines/pages to print).

Justification:

  - Excessive output floods the terminal window. Some terminals have a
limit on output history (Windows *defaults* to 300 lines), or don't let you
scroll up at all (at least, my noob self couldn't figure it out when I did
get flooded).

  - Students learning Python, and also everyone else using Python, don't
usually need 99% of a 10000-line output or stack trace.

  - People who want the full output are probably advanced users with, like,
high-limit or unlimited window size, and advanced users are more likely to
look for a verbosity flag, or use a third-party REPL. Default should be
newbie friendly, because advanced users can work around it.

Thoughts? Even if the specific proposals are unworkable, is limiting REPL
output (by default) a reasonable goal?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160417/4ff848e2/attachment.html>


More information about the Python-ideas mailing list