Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing 'None' as function parameter (where parameter is a function)
    primarykey
    data
    text
    <p>I am writing a small app that has to perform some 'sanity checks' before entering execution. (eg. of a sanity check: test if a certain path is readable / writable / exists)</p> <p>The code:</p> <pre><code>import logging import os import shutil import sys from paths import PATH logging.basicConfig(level=logging.DEBUG) log = logging.getLogger('sf.core.sanity') def sanity_access(path, mode): ret = os.access(path, mode) logfunc = log.debug if ret else log.warning loginfo = (os.access.__name__, path, mode, ret) logfunc('%s(\'%s\', %s)==%s' % loginfo) return ret def sanity_check(bool_func, true_func, false_func): ret = bool_func() (logfunc, execfunc) = (log.debug, true_func) if ret else \ (log.warning, false_func) logfunc('exec: %s', execfunc.__name__) execfunc() def sanity_checks(): sanity_check(lambda: sanity_access(PATH['userhome'], os.F_OK), \ lambda: None, sys.exit) </code></pre> <p>My question is related to the <code>sanity_check</code> function. </p> <p>This function takes 3 parameters (<code>bool_func</code>, <code>true_func</code>, <code>false_func</code>). If the <code>bool_func</code> (which is the test function, returning a boolean value) fails, <code>true_func</code> gets executed, else the <code>false_func</code> gets executed.</p> <p><strong>1)</strong> <code>lambda: None</code> is a little lame , because for example if the sanity_access returns True, <code>lambda: None</code> gets executed, and the output printed will be: </p> <pre><code>DEBUG:sf.core.sanity:access('/home/nomemory', 0)==True DEBUG:sf.core.sanity:exec: &lt;lambda&gt; </code></pre> <p>So it won't be very clear in the logs what function got executed. The log will only contain <code>&lt;lambda&gt;</code> . Is there a default function that does nothing and can be passed as a parameter ? Is it a way to return the name of the first function that is being executed inside a lambda ? </p> <p>Or a way not to log that "exec" if 'nothing' is sent as a paramter ?</p> <p>What's the none / do-nothing equivalent for functions ? </p> <pre><code>sanity_check(lambda: sanity_access(PATH['userhome'], os.F_OK), \ &lt;do nothing, but show something more useful than &lt;lambda&gt;&gt;, sys.exit) </code></pre> <p>Additional question, why is <code>lambda: pass</code> instead of <code>lambda: None</code> not working ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload