Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pickling a staticmethod in Python

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

like image 725
Yonatan Avatar asked Jul 13 '26 04:07

Yonatan


1 Answers

This seems to work.

class PickleableStaticMethod(object):
    def __init__(self, fn, cls=None):
        self.cls = cls
        self.fn = fn
    def __call__(self, *args, **kwargs):
        return self.fn(*args, **kwargs)
    def __get__(self, obj, cls):
        return PickleableStaticMethod(self.fn, cls)
    def __getstate__(self):
        return (self.cls, self.fn.__name__)
    def __setstate__(self, state):
        self.cls, name = state
        self.fn = getattr(self.cls, name).fn

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 subclass Pickler 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.

like image 198
Jason Orendorff Avatar answered Jul 14 '26 17:07

Jason Orendorff