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

Serhiy Storchaka storchaka at gmail.com
Fri Sep 29 06:45:05 EDT 2017


29.09.17 08:53, Wren Turkal пише:
> 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)

Why not use just the following two lines?

     f.write('Name? ')
     name = f.readline()

This falls to me in the category "not every two lines of the code should 
be added as a builtin".



More information about the Python-ideas mailing list