[Python-ideas] Adding an 'errors' argument to print

Nick Coghlan ncoghlan at gmail.com
Mon Mar 27 02:54:04 EDT 2017


On 27 March 2017 at 13:10, Steve Dower <steve.dower at python.org> wrote:
> On 26Mar2017 0707, Nick Coghlan wrote:
>>
>> Perhaps it would be worth noting in the table of error handlers at
>> https://docs.python.org/3/library/codecs.html#error-handlers that
>> backslashreplace is used by the `ascii()` builtin and the associated
>> format specifiers
>
> backslashreplace is also the default errors for stderr, which is arguably
> the right target for debugging output. Perhaps what we really want is a
> shorter way to send output to stderr? Though I guess it's an easy to invent
> one-liner, once you know about the difference:
>
>>>> printe = partial(print, file=sys.stderr)

If there was a printerror builtin that used sys.stderr as its default
output stream, it could also special case BaseException instances to
show their traceback.

At the moment, we do force people to learn a few additional concepts
in order to do error display "right":

- processes have two standard output streams, stdout and stderr
- Python makes those available in the sys module
- the print() builtin function lets you specify a stream with "file"
- so errors should be printed with "print(arg, file=sys.stderr)"
- to get exception tracebacks like those at the interactive prompt,
look at the traceback module

As opposed to "for normal output, use 'print', for error output, use
'printerror', for temporary debugging output also use 'printerror',
otherwise use the logging module".

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list