On Tue, 3 May 2022 at 05:56, <sam.z.e...@gmail.com> wrote: > > > (Plus not-yet-existing, but hopefully soon, redirect_stdin.) > Mentioned on the redirect_stdio thread, I've now created a GitHub issue and > PR for contextlib.redirect_stdin. > > I think I want to see some examples of how and why you would use it, and > > why one couldn't just use the redirect_stdout context manager. > One aspect that was mentioned in the original thread was the use of readline > support alongside and arrow-key functionality, I can look into what that > means in practice. The original post contained the example of switching > between different terminals. > > Using the prospective redirect_stdin context manager, the following code > > ``` > with open("/dev/tty", 'r+') as file: > with contextlib.redirect_stdin(file), contextlib.redirect_stdout(file): > name = input('Name: ') > > print(name) > ``` > > Could be rewritten like this > > ``` > with open('/dev/tty', 'r+') as file: > name = input('Name: ', infile=file, outfile=file) > > print(name) > ```
Is it sufficiently common to need this *just* for the input call, and not any corresponding print calls? The redirect context manager would also affect print calls in its scope. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/KDG2S5RPWFWPECOXVKL6D6ACRKRM47BG/ Code of Conduct: http://python.org/psf/codeofconduct/