Performance penalty of long list in python logging.debug()

Viewed 243

How lazy is evaluation in Python? If I have code that looks like

logging.debug('My very long list: %s' % list(x for x in long_generator))

and the logging effective level is such that debug messages are ignored, do I incur a performance penalty for having this line?

2 Answers

This will not be lazily evaluated, but Python logging has a solution to this problem: isEnabledFor().

if logger.isEnabledFor(logging.DEBUG):
    logger.debug('My very long list: %s' % list(x for x in long_generator))

Your list will be built and formatted only if logger's level is higher than DEBUG.

You will have a performance penalty because list(x for x in long_generator) will be created before calling logging.debug() even if debug messages are ignored.

There is no lazy evaluation in Python. Every line is evaluated/executed before the next line. It means that expressions are evaluated when they are bound to variables.

Related