I've been trying to pickle an object which contains references to static class methods.
Pickle fails (for example on module.MyClass.foo
) stating it cannot be pickled, as module.foo
does not exist.
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:
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)
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 pickle
process (using copy_reg
for example), but the following pattern would be great:
class MyClass(object):
@picklable_staticmethod
def foo():
print "done."
My attempts at this were unsuccessful, specifically because I could not extract the owner class from the foo
function. I was even willing to settle for explicit specification (such as @picklable_staticmethod(MyClass)
) but I don't know of any way to refer to the MyClass
class right where it's being defined.
Any ideas would be great!
Yonatan
EDIT: modified after Jason comment.
I think python is correct in not letting pickling a staticmethod object - as it is impossible to pickle instance or class methods! Such an object would make very little sense outside of its context:
Check this: Descriptor Tutorial
This works, and that's what should be done - define a function, pickle it, and use such function as a staticmethod in a certain class.
If you've got an use case for your need, please write it down and I'll be glad to discuss it.
This seems to work.
The trick is to snag the class when the static method is gotten from it.
Alternatives: You could use metaclassing to give all your static methods a
.__parentclass__
attribute. Then you could subclassPickler
and give each subclass instance its own.dispatch
table which you can then modify without affecting the global dispatch table (Pickler.dispatch
). Pickling, unpickling, and calling the method might then be a little faster.