Introduce eprint function to print in STDERR

Hi everyone,

I use almost every day the following function for print into stderr file descriptor:

def eprint(
    *values: object,
    file: SupportsWrite[str] | None = sys.stderr,
    **kwargs,
) -> None

Maybe it’s possible to define this function in the language itself, like print function.

from functools import partial
from sys import stderr

eprint = partial(print, file=stderr)

If you redirect stderr, you need to use print(..., file=sys.stderr) or write a helper function.

6 Likes

Thanks so much. Effectively with partial function is very useful.

1 Like

Standard error is typically meant for outputting debug messages of higher severity. If that’s what you’re actually using stderr for you may want to consider using the logging module instead for better control over filters and formats:

import sys
import logging

logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
logging.warning('bad things might happen')
2 Likes

While stderr xan be used simply for printing error messages, and proper logging is really good for larger projects, stderr is required to show any type of information for scripts where the stdout is meant to be used in a pipe or sent to some file via the command line. I think the name stderr is pretty bad in describing it’s purpose but it’s the standard name