title: How to create a copy of a python function
body: <p>Is there a possibility to create real copies of python functions? The most obvious choice was <a href="http://docs.python.org/2/library/copy.html">http://docs.python.org/2/library/copy.html</a> but there I read:</p>
  
  <blockquote>
    <p>It does “copy” functions and classes (shallow and deeply), by
    returning the original object unchanged;</p>
  </blockquote>
  
  <p>I need a real copy, because I might change some attributes of the function.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I'm aware of all the possibilities which are mentioned in the comments. My use case is based on meta programming where I construct classes out of some declarative specifications. Complete details would be too long for SO, but basically I have a function like</p>
  
  <pre><code>def do_something_usefull(self,arg):
      self.do_work()
  </code></pre>
  
  <p>I will add this method to various classes. Thoses classes can be completly unrelated. Using mixin classes is not an option: I will have many such functions and would end up adding a base class for each function. My current "workaround" would be to wrap this function in a "factory" like this:</p>
  
  <pre><code>def create_do_something():
      def do_something_usefull(self,arg):
          self.do_work()
  </code></pre>
  
  <p>That way I always get a new do_something_useful function, but I have to wrap all my functions like this.</p>
  
  <p>You can trust me, that I'm aware, that this is no "normal" OO programming. I know how to solve something like that "normally". But this is a dynamic code generator and I would like to keep everything as lightweight and simple as possible. And as python functions are quite normal objects, I don't think it's too strange to ask how to copy them!?</p>