title: Python - Clone a function while passing a single argument
body: <p>I would like to copy an existing function from an existing module in the following way:</p>
  
  <pre><code>def foo(a,b,c=1,d=3,*arg):
     return True
  
  myClass.foo = lambda b,c,d,*arg : foo(my_value_of_a, b,c,d,*arg)
  </code></pre>
  
  <p>However, there are several problems with this approach namely:</p>
  
  <ul>
  <li>I am doing this in a loop and I don't know the arguments of most functions</li>
  <li>I am losing the default values - which I absolutely cannot</li>
  <li>The <code>__docs__</code> and other attributes would be nice to keep too</li>
  </ul>
  
  <p>I tried to do something like this:</p>
  
  <pre><code>handler = getattr(mod,'foo')
  handler.__defaults__ = tuple([my_value_of_a] + list(handler.__defaults__))
  myClass.foo = handler
  </code></pre>
  
  <p>which is <em>almost</em> enough for my use case (just because I always modify the first argument only). The problem is that if I call <code>mod.foo()</code> it <em>also</em> has <code>my_value_of_a</code> as the default value for <code>a</code>!</p>
  
  <p>I tried using the <code>copy</code> module to do a <code>handler=deepcopy(handler)</code> but even that didn't work and modifying the default values of <code>handler</code> also modifies the default values of the module function itself.</p>
  
  <p>Any suggestions on who to do this in a "pythonic" way? I probably cannot use decorators either, since I'm looping over functions from external modules (several, actually).</p>