Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create lambda's from Python/C
    text
    copied!<p>We're working on some Python/C-API code, and we've encountered a method that would like to be passed a callback. The method will ship periodic updates to the callback as a form a feedback. As it turns out, we're not that interested in periodic feedback. The only way to disable the method's default feedback mechanism is to pass it some kind of callback.</p> <p>The technique we've employed is to declare a module level function that just returns None, ie:</p> <pre><code>static PyObject* donothing(PyObject* self, PyObject* args) { return Py_None; } </code></pre> <p>But of course, this function also needs to be registered with the modules method table, ie:</p> <pre><code>static PyMethodDef methods[] = { {"donothing", donothing, METH_VARARGS, "do nothing"}, ... {NULL} }; </code></pre> <p>Then, when we go to call the method, we need to grab a reference to this method, ie: <code>PyObject_GetAttrString(module_reference, "donothing").</code></p> <p>All of this feels like we're spending too much time spinning our wheels just to do nothing. Then it ocurred to me..hey, seems like a perfect use for <strong>lambda x: None</strong>. But after spending an hour with the Python/C-API docs, I can't figure out how one create's lambdas.</p> <p>I see there are references to closures on the page <a href="http://docs.python.org/2/c-api/function.html" rel="nofollow">http://docs.python.org/2/c-api/function.html</a>, but I can't sort out the details on how one creates them.</p> <p>Any pointers (or references to RTFM) would be greatly appreciated.</p>
 

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