[Python-ideas] Default value for input

Chris Angelico rosuav at gmail.com
Tue Apr 1 14:11:47 CEST 2014


On Tue, Apr 1, 2014 at 10:58 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> I often have the need to include a default or initial value when
> asking the user for input. In other words, I'd like to call this:
>
> input("What is the fish of the day? ", "trout a la creme")
>
>
> and have the prompt be "What is the fish of the day? " and the initial
> value in the edit buffer be "trout a la creme", fully editable with
> whatever line editing tools are available on the user's system. There
> are work-arounds for this lack, but they don't make a nice clean UI.
>
> Is it practical to include an editable initial value on systems without
> readline? If a platform-independent version is not practical, I don't
> think there's any point in only half-supporting it.

You could fudge it on a non-readline system by rewriting the prompt
and turning blank into the default:

_input = input
def input(prompt, initial=''):
    if not initial: return _input(prompt)
    return _input("%s[%s] "%(prompt, initial)) or initial

That's not the same as an *editable* initial value, but it's a visible
default, which is a useful feature. However, the convention is usually
to put the square brackets before the punctuation:

What is the fish of the day [trout a la creme]?

so it might be better to make something more sophisticated that
recognizes question marks and colons and shifts the default marker
before them.

Sounds like a reasonable feature, to me. I've no idea about libedit,
which is the most common non-readline system that has real editing;
you might need to have several options and then a final fall-back that
looks like the above, but it should be possible.

How common is the square-brackets-means-default convention? Would, for
instance, a Windows-only user understand it?

ChrisA


More information about the Python-ideas mailing list