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

Nick Coghlan ncoghlan at gmail.com
Sun Mar 26 10:07:15 EDT 2017


On 26 March 2017 at 18:31, Victor Stinner <victor.stinner at gmail.com> wrote:
> print(msg) calls sys.stdout.write(msg): write() expects text, not bytes. I
> dislike the idea of putting encoding options in print. It's too specific.
> What if tomorrow you replace print() with file.write()? Do you want to add
> errors there too?
>
> No, it's better to write own formatter function as shown in a previous
> email.

While I agree with that, folks that are thinking in terms of errors
handlers for str.encode may not immediately jump to using the
`ascii()` builtin or the "%a" or "!a" format specifiers, and if you
don't use those existing tools, you have the hassle of deciding where
to put your custom helper function.

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, as well as noting the format specifiers in the
documentation of the builtin function?

Cheers,
Nick.

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


More information about the Python-ideas mailing list