[Python-ideas] Efficient debug logging

Chris Angelico rosuav at gmail.com
Wed Feb 15 06:33:42 EST 2017

On Wed, Feb 15, 2017 at 10:18 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Python doesn't have thunks, but there is a relatively heavyweight
> solution for delayed evaluation: wrap the code in a function.
> debugLog( ‘info is %r’, lambda: expensiveFunction() )
> and then adjust debugLog so that if the argument is a function, it will
> call the function only when needed:
> def debugLog(message, value):
>     if debug_log_enabled:
>         if isinstance(value, types.FunctionType):
>             value = value()
>         log(message % value)

Or use the function as the __repr__ of some object, which comes to the
same thing without requiring special code inside debugLog.


More information about the Python-ideas mailing list