[Python-ideas] Adding an 'errors' argument to print
Chris Angelico
rosuav at gmail.com
Sun Mar 26 14:42:06 EDT 2017
On Mon, Mar 27, 2017 at 5:22 AM, Michel Desmoulin
<desmoulinmichel at gmail.com> wrote:
>
>
> Le 26/03/2017 à 10:31, Victor Stinner a écrit :
>> print(msg) calls sys.stdout.write(msg): write() expects text, not bytes.
>
> What you are saying right now is that the API is not granular enough to
> just add a parameter. Not that it can't be done. It just mean we need to
> expose stdout.write() encoding behavior.
>
>> 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?
>
> You would have to rewrite all your calls anyway, because print() call
> str() on things and accept already many parameters while file.write()
> doesn't.
You can easily make a wrapper around print(), though. For example,
suppose you want a timestamped log file as well as the console:
from builtins import print as pront # mess with people
@functools.wraps(pront)
def print(*a, **kw):
if "file" not in kw:
logging.info(kw.get("sep", " ").join(a))
return pront(*a, **kw)
Now what happens if you add the errors handler? Does this function
need to handle that somehow?
ChrisA
More information about the Python-ideas
mailing list