title: What is the most pythonic way to reuse data in multiple calls to same function?
body: <p>Normally I would not ask such a question, but python seems to have 1. an unusual level of community consensus on idioms and 2. tends to encourage them by making them more performant (e.g. list comprehension vs map, filter).</p>
  <p>This is a pattern I find myself using quite a bit when coding, consider the following JavaScript:</p>
  <pre class="lang-js prettyprint-override"><code>var f = (function() {
    var closedOver = &quot;whatever&quot;
    return function(param) {
      // re-uses closure variable again and again with different param
    }
  })();
  </code></pre>
  <p>Or C:</p>
  <pre class="lang-c prettyprint-override"><code>int foo(int x)
  {
    /* 
      compile-time constant, will not be recalced for every call,
      name 'someConst' not visible in other scopes 
     */
    const int someConst = 134;
    /* do stuff */
    return whatever;
  }
  </code></pre>
  <p>Some possible ways to translate into python:</p>
  <pre><code>globalConstant = someConstant
  def foo(param):
      # does stuff with param and constant
      return whatever
  </code></pre>
  <p>or possibly:</p>
  <pre><code>from functools import partial
  def foo(invariant, variant):
      &quot;&quot;&quot;Actually bar&quot;&quot;&quot;
      # does stuff
      return whatever
  
  bar = partial(foo, someInvariant)
  </code></pre>
  <p>or:</p>
  <pre><code>class Foo(object):
      &quot;&quot;&quot;I'm just here to hold a non-visible binding. Actually bar&quot;&quot;&quot;
      def __init__(self, invariant):
          super(Foo, self).__init__()
          self.value = invariant
  
      def __call__(self, param):
          return actualFnResultWithSelfValue
  
  bar = Foo(invariant)
  </code></pre>
  <p>or:</p>
  <pre><code>def foo(variant, invariant=someConstantValue):
    return whatever
      
  </code></pre>
  <p>This is unfortunate, now depending on which way I go I may have to use a throw-away name for the initial function definition since I'm only ever using the partially applied version, write a lot of boilerplate classes (which also have throw-away names), or pollute the module namespace with a global constant when its only used in one function, or restrict my function parameters and ensure that someone can break it by calling it with the wrong number of arguments.</p>
  <p>I could also 'solve' this by re-instantiating on every call and hoping that it will get optimized away, but since I'm not using pypy I'm not too hopeful on that score.</p>
  <p>So my question is two-fold: first, is there a way to do this without the trade-offs? And second, if not, which of the above is the most 'pythonic' (idiomatic, performant, reasonable, etc.)?</p>