title: Is it pythonic: naming lambdas
body: <p>I'm beginning to appreciate the value of lambda expressions in python, particularly when it comes to functional programming, <code>map</code>, <a href="https://stackoverflow.com/a/16509/1533474">functions returning functions</a>, etc. However, I've also been naming lambdas within functions because:</p>
  <ul>
  <li>I need the same functionality several times and don't want to repeat code.</li>
  <li>The functionality is specific to the function in which it appears; its not needed elsewhere.</li>
  </ul>
  <p>When I encounter a situation that meets the above criteria, I've been writing a named lambda expression in order to DRY and narrowly scope functionality. For example, I am writing a function that operates on some <code>numpy</code> arrays, and I need to do some moderately tedious indexing of all the arrays passed to the function (which can easily fit on a single line). I've written a named lambda expression to do the indexing instead of writing a whole other function or copy/pasting the indexing several times throughout the function definition.</p>
  <pre><code>def fcn_operating_on_arrays(array0, array1):
      indexer = lambda a0, a1, idx: a0[idx] + a1[idx]
      
      # codecodecode
      
      indexed = indexer(array0, array1, indices)
      
      # codecodecode in which other arrays are created and require `indexer`
      
      return the_answer
  </code></pre>
  <p>Is this an abuse of python's lambdas? Should I just suck it up and define a separate function?</p>
  <h2>Edits</h2>
  <p>Probably worth linking <a href="https://stackoverflow.com/questions/4831680/function-inside-function">function inside function</a>.</p>