[Python-ideas] allow overriding files used for the input builtin

Amit Green amit.mixie at gmail.com
Fri Sep 29 02:18:18 EDT 2017


I'm fine with the idea in general of extra keyword parameters to the input
function.

A few points:

Your example code, needs try/catch to match what the input with parameters
does -- and yes, its way nicer to be able to use it the example you have
shown than play games with try/catch (Personally I also refuse to ever
change sys.stdin, or sys.stdout, as I consider that a bad coding style).

Mostly though I would like to ask, please do not name keyword arguments
with names like 'fin' & 'fout'.  This is almost unreadable and make's code
almost indecipherable to others the first time they see the function & its
keyword arguments (First impressions are very important).

Both a function name & its keyword parameters need to be as understandable
as possible when a user encounters them for the first time.

On Fri, Sep 29, 2017 at 1:53 AM, Wren Turkal <w00t at fb.com> wrote:

> Hi there,
>
>
> I have posted an idea for improvement with a PR of an implementation to
> https://bugs.python.org/issue31603.
>
>
> The basic idea is to add fin, fout, and ferr file object parameters and
> default to using what is used today when the args are not specified. I
> believe this would be useful to allow captures input and send output to
> specific files when using input. The input builtin has some logic to use
> readline if it's available. It would be nice to be able to use this same
> logic no matter what files are being used for input/output.
>
>
> This is meant to turn code like the following:
>
> orig_stdin = sys.stdin
>
> orig_stdout = sys.stdout
>
> with open('/dev/tty', 'r+') as f:
>
>     sys.stdin = f
>
>     sys.stdout = f
>
>     name = input('Name? ')
>
> sys.stdin = orig_stdin
>
> sys.stdout = orig_stdout
>
> print(name)
>
>
> into something more like this:
>
> with open('/dev/tty', 'r+') as f:
>
>     name = input('Name? ', fin=f, fout=f)
>
> print(name)
>
>
> It's nice that it makes the assignment to a global variable to change the
> file used for input/output to no longer be needed.
>
>
> I had this idea the other day, and I realized that it would be super easy
> to implement, so I went ahead the threw up a PR also.
>
>
> Would love to see if anyone else is interested in this. I think it's
> pretty cool that the core logic really didn't need to be changed other than
> plumbing in the new args.
>
>
> FWIW, this change introduces no regressions and adds a few more tests to
> test the new functionality. Honestly, I think this functionality could
> probably be used to simplify some of the other tests as well, but I wanted
> to gauge what folks thought of the change before going farther.
>
>
> Wren Turkal
>
> Existential Production Engineer of the Ages
>
> Facebook, Inc.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170929/684bcd50/attachment.html>


More information about the Python-ideas mailing list