Re: Function Serialization



By default, shelve uses pickle to serialize objects. Pickle doesn't
actually serialize functions... it just saves the function's name and
name of its defining module, and upon loading attempts to find that
function again. This is pretty much /never/ going to work the way you
want it to if you're using exec.

If you really want to save a function, you're going to have to save its
source code and exec it upon reloading. I think the best way to
achieve this would be to make your own wrapper class which defines a
__call__ method and implements custom pickling routines (see the pickle
documentation for how to do this). You could probably even make it
into a decorator, but I wouldn't know about that since I'm stuck with
Python 2.3.

It would look something like this

def f(): pass
f = SerializableFunction(f)

where SerializableFunction is defined as:

class SerializableFunction(object):
def __init__(self, function):
self.function = function

def __call__(self, *args, **keywords):
return self.function(*args, **keywords)

# custom pickle routines
def __getnewargs__(self):
...
def __getstate__(self):
# extract function source
...
def __setstate__(self):
...

You can use the inspect module to get function source code, although
AFAIK it's impossible to get source from functions defined in the
interactive interpreter.

.



Relevant Pages

  • Adding Attributes (or equivalent effect) Without Touching Original Source Code
    ... An important server-side task is to serialize server-side objects to JSON, ... public Person(string firstName, string middleName, string lastName) ... public string MiddleName ... I do in fact have the source code of the classes that have been part of this ...
    (microsoft.public.dotnet.languages.csharp)
  • Why cant you pickle instancemethods?
    ... Why can pickle serialize references to functions, ... Pickling a function serializes the function name, ... staticmethod, classmethod, or instancemethod generates an error. ...
    (comp.lang.python)
  • Re: Why cant you pickle instancemethods?
    ... Pickling a function serializes the function name, ... staticmethod, classmethod, or instancemethod generates an error. ... Pickle doesn't serialize code objects, ...
    (comp.lang.python)
  • Object serialization: transfer from a to b (non-implemented code on b)
    ... I am trying to serialize a function, class, etc and transfer it, have it unserialized and used. ... >>> import pickle ... Traceback: ... ValueError: unmarshallable object ...
    (comp.lang.python)
  • Re: save class
    ... have tried to use pickle, but am not sure if that is right. ... but I am new to python. ... You just create the .py file with any text editor, containing the source code for all your classes. ...
    (comp.lang.python)