What should Python apps do when asked to show help?
Random832
random832 at fastmail.com
Sat Apr 30 01:20:11 EDT 2016
On Fri, Apr 29, 2016, at 23:23, Steven D'Aprano wrote:
> Seriously, I'm thinking that a keyword argument to help might be useful:
>
> help(object, pager=None)
I'd call it something more generic like "output".
> where:
>
> - pager=None gives the current behaviour;
>
> - pager="foo" calls out to the external program "foo";
>
> - pager=callable passes the help text to callable().
>
>
> pager=print would do exactly what people are asking for, and you could
> then
> create your own wrapper to change the default:
>
> help = functools.partial(builtins.help, pager=print)
>
>
> I think that would make it easier to test help(). Thoughts?
For testing purposes, help could return the result of the output
function, so that you can use lambda x: x to have it return the help
text.
More general thoughts: It might also be useful to move the pager
machinery from pydoc to an independent module. It's got, among other
things, a simple pager written in pure python, for use as a fallback.
There's a lot of stuff that could be improved in the pager stuff, while
we're at it. Probably needs a single function to handle "use external
program as a pager", rather than having a bunch of logic embedded in
getpager() which only works for os.environ['PAGER'].
The "pager" function doesn't behave "properly" (in its intended behavior
of calling the expensive getpager() once when it is first called and
subsequently reusing the cached value) if a reference to it is stored
elsewhere (e.g. by importing it to another module); it should store the
cached pager function somewhere else rather than by replacing itself.
The pure-python pager only works on unix-style ttys, an analogous
function could be written for windows using msvcrt.getwch.
More information about the Python-list
mailing list