views:

226

answers:

2

Hello all, 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

+1  A: 

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

import pickle

def dosomething(a, b):
    print a, b

class MyClass(object):
    dosomething = staticmethod(dosomething) 

o = MyClass()

pickled = pickle.dumps(dosomething)

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.

Alan Franzoni
What wouldn't work is `pickle.dumps(MyClass.dosomething)`.
Jason Orendorff
then the question is not properly written, the OP says "I've been trying to pickle an object which contains references to static class methods" while he's really trying to pickle a static method, not an object containing references to static methods.
Alan Franzoni
Let me clarify with an example.I have a task-generation mechanism. For example: class MyClass(TaskGenerator): @staticmethod def certain_task(): print "task done" def generate_task(self): return FuncTask(MyClass.certain_task)I will want to pickle the generated task but currently can't.I can use a global function - and have until now - but I don't like taking it out from its 'right place' in favor of pickling support.
Yonatan
I admit I still don't understand *what* you're trying to do, and I still think you might be abusing "static methods" (where, how and why are you unpickling such objects?)
Alan Franzoni
+1  A: 

This seems to work.

class PickleableStaticMethod(object):
    def __init__(self, fn, cls=None):
        self.cls = cls
        self.fn = fn
    def __call__(self, *args, **kwargs):
        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.

Jason Orendorff
this will not work if the original class is not available, which (i think) defeats the purpose of pickling and unpickling a static method - you allow it to be pickled, but you need the whole class to unpickle it.
Alan Franzoni
try that for instance:import pickleclass MyClass(object): @PickleableStaticMethod def dosomething(a, b): print a, bpickled = pickle.dumps(MyClass.dosomething)del MyClassstm = pickle.loads(pickled)
Alan Franzoni
i think I might just have said something totally wrong there, pickle works totally different with funcs that i expected. ignore my latest two comments :-/
Alan Franzoni