<div dir="auto">Seems slightly simpler to just make debugLog accept a callable as an alternative to a string.<div dir="auto"><br></div><div dir="auto"><span style="font-family:sans-serif">debugLog(lambda:( ‘info is %s’ % expensiveFunction()) )</span><br><div dir="auto"><br><div class="gmail_extra"><br><div class="gmail_quote">Op 14 feb. 2017 18:42 schreef "Kyle Lahnakoski" <<a href="mailto:klahnakoski@mozilla.com">klahnakoski@mozilla.com</a>>:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Can you wrap the expensive functions in lambdas? And have your logger<br>
evaluate it, only if required?<br>
<br>
> debugLog( ‘info is %r’ % (lambda: expensiveFunction(),) )<br>
<div class="elided-text"><br>
<br>
On 2017-02-14 10:51, Barry Scott wrote:<br>
> A common pattern I use is to have logging calls for debug and information with my applications.<br>
> The logging calls can be separately enabled and disabled.<br>
><br>
> For example:<br>
><br>
> debug_log_enabled = False<br>
> def debugLog( msg ):<br>
>       If debug_log_enabled:<br>
>             print( ‘Debug: %s’ % (msg,) )<br>
><br>
> Then the caller can simple write:<br>
><br>
> def main():<br>
>       debugLog( ‘Start of main’ )<br>
><br>
> This is fine until the evaluation of the msg becomes expensive.<br>
><br>
>       debugLog( ‘info is %r’ % (expensiveFunction(),) )<br>
><br>
> What would be nice is to be able to avoid evaluation the tuple of arguments if debug is<br>
> disabled as this can be expensive. I can write this:<br>
><br>
>       if debug_log_enabled:  debugLog( ‘info is %r’ % (expensiveFunction(),) )<br>
><br>
> But that is a more code then I would like to write. And if the debug code is a performance problem cannot<br>
> be left in the production code.<br>
><br>
> I could combine the boolean and the log function by using a class to tidy up the implementation.<br>
><br>
> class DebugLog:<br>
>       def __init__( self, enabled = False ):<br>
>               self.enabled = enabled<br>
><br>
>       def __bool__( self ):<br>
>               return self.enabled<br>
><br>
>       def __call__( self, msg ):<br>
>               if self.enabled: print( ‘Debug: %s’ % (msg,) )<br>
><br>
> And call like this:<br>
><br>
>       dbg_log = DebugLog()<br>
><br>
>        If dbg_log: dbg_log( ‘a debug message’ )<br>
><br>
> But I’d like to only write:<br>
><br>
>       dbg_log( ‘a debug message’ )<br>
><br>
> And have the evaluation of the argument skipped unless its dbg_log is enabled.<br>
><br>
> I cannot see how to do this with python as it stands.<br>
><br>
> Something would have to be added to allow python to short circuit the argument tuple evaluation.<br>
><br>
> Maybe python can check for a special dunder on the class that know how to do this idiom, __if_true_call__?<br>
><br>
> Thoughts?<br>
><br>
> Barry<br>
><br>
> ______________________________<wbr>_________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a></div></blockquote></div><br></div></div></div></div>