Function Serialization

Lonnie Princehouse finite.automaton at
Thu Jun 2 01:02:34 CEST 2005

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.

More information about the Python-list mailing list