Note that there are some explanatory texts on larger screens.

plurals
  1. PORecipe for anonymous functions in python?
    primarykey
    data
    text
    <p>I'm looking for the best recipie to allow inline definition of functions, or multi-line lambda, in python.</p> <p>For example, I'd like to do the following:</p> <pre><code>def callfunc(func): func("Hello") &gt;&gt;&gt; callfunc(define('x', ''' ... print x, "World!" ... ''')) Hello World! </code></pre> <p>I've found an example for the <code>define</code> function in <a href="https://stackoverflow.com/a/6630330/143091">this answer</a>:</p> <pre><code>def define(arglist, body): g = {} exec("def anonfunc({0}):\n{1}".format( arglist, "\n".join(" {0}".format(line) for line in body.splitlines())), g) return g["anonfunc"] </code></pre> <p>This is one possible solution, but it is not ideal. Desireable features would be:</p> <ul> <li>be smarter about indentation,</li> <li>hide the innards better (e.g. don't have <code>anonfunc</code> in the function's scope)</li> <li>provide access to variables in the surrounding scope / captures</li> <li>better error handling</li> </ul> <p>and some things I haven't thought of. I had a really nice implementation once that did most of the above, but I lost in unfortunately. I'm wondering if someone else has made something similar.</p> <h2>Disclaimer:</h2> <p>I'm well aware this is controversial among Python users, and regarded as a hack or unpythonic. I'm also aware of the discussions regaring multi-line-lambdas on the python-dev mailing list, and that a similar feature was omitted on purpose. However, from the same discussions I've learned that there is also interest in such a function by many others.</p> <p><strong>I'm not asking whether this is a good idea or not</strong>, but instead: <strong>Given that one has decided to implement this</strong>, (either out of fun and curiosity, madness, genuinely thinking this is a nice idea, or being held at gunpoint) <strong>how to make anonymous <code>define</code> work as close as possible to <code>def</code> using python's (2.7 or 3.x) current facilities?</strong></p> <h2>Examples:</h2> <p>A bit more as to why, this can be really handy for callbacks in GUIs:</p> <pre><code># gtk example: self.ntimes = 0 button.connect('clicked', define('*a', ''' self.ntimes += 1 label.set_text("Button has been clicked %d times" % self.ntimes) ''') </code></pre> <p>The benefit over defining a function with <code>def</code> is that your code is in a more logical order. This is simplified code taken from a Twisted application:</p> <pre><code># twisted example: def sayHello(self): d = self.callRemote(HelloCommand) def handle_response(response): # do something, this happens after (x)! pass d.addCallback(handle_response) # (x) </code></pre> <p>Note how it seems out of order. I usually break stuff like this up, to keep the code order == execution order:</p> <pre><code>def sayHello_d(self): d = self.callRemote(HelloCommand) d.addCallback(self._sayHello_2) return d def _sayHello_2(self, response): # handle response pass </code></pre> <p>This is better wrt. ordering but more verbose. Now, with the anonymous functions trick:</p> <pre><code>d = self.callRemote(HelloCommand) d.addCallback(define('response', ''' print "callback" print "got response from", response["name"] ''')) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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