title: Pickling a staticmethod in Python
body: <p>I've been trying to pickle an object which contains references to static class methods.
  Pickle fails (for example on <code>module.MyClass.foo</code>) stating it cannot be pickled, as <code>module.foo</code> does not exist.<br>
  I have come up with the following solution, using a wrapper object to locate the function upon invocation, saving the container class and function name:</p>
  
  <pre><code>class PicklableStaticMethod(object):
      """Picklable version of a static method.
      Typical usage:
          class MyClass:
              @staticmethod
              def doit():
                  print "done"
          # This cannot be pickled:
          non_picklable = MyClass.doit
          # This can be pickled:
          picklable = PicklableStaticMethod(MyClass.doit, MyClass)
      """
      def __init__(self, func, parent_class):
          self.func_name = func.func_name
          self.parent_class = parent_class
      def __call__(self, *args, **kwargs):
          func = getattr(self.parent_class, self.func_name)
          return func(*args, **kwargs)
  </code></pre>
  
  <p>I am wondering though, is there a better - more standard way - to pickle such an object?
  I do not want to make changes to the global <code>pickle</code> process (using <code>copy_reg</code> for example), but the following pattern would be great:
      class MyClass(object):
          @picklable_staticmethod
          def foo():
              print "done."</p>
  
  <p>My attempts at this were unsuccessful, specifically because I could not extract the owner class from the <code>foo</code> function. I was even willing to settle for explicit specification (such as <code>@picklable_staticmethod(MyClass)</code>) but I don't know of any way to refer to the <code>MyClass</code> class right where it's being defined.</p>
  
  <p>Any ideas would be great!</p>
  
  <p>Yonatan</p>