On Wed, Feb 15, 2017 at 10:18 PM, Steven D'Aprano <st...@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.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to