Pickling functions

Steve Purcell stephen_purcell at yahoo.com
Tue Mar 27 11:28:05 CEST 2001

Tim CHURCHES wrote:
> How does one go about pickling and unpickling a function object? All my
> attempts so far result in a reference to the function object being
> pickled but not the function itself.

No promises, but it looks like you could pickle a list of

 [func.func_code, func.func_globals, func.__name__, func.func_defaults]

and then re-create the original function by unpickling that list and
passing it to 'new.function'.

(see http://www.python.org/doc/lib/module-new.html)

You should probably avoid pickling func.func_globals, though.


Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Any opinions expressed herein are my own and not necessarily those of Yahoo

More information about the Python-list mailing list