Skip to content

Instantly share code, notes, and snippets.

@Arachnid
Created March 30, 2012 10:48
Show Gist options
  • Save Arachnid/2250737 to your computer and use it in GitHub Desktop.
Save Arachnid/2250737 to your computer and use it in GitHub Desktop.
import copy_reg
import cPickle
import marshal
import types
def restorefunction(func_name, dump):
return types.FunctionType(marshal.loads(dump), globals(), func_name)
def reducefunction(func):
return (restorefunction, (func.func_name, marshal.dumps(func.func_code)))
copy_reg.constructor(restorefunction)
copy_reg.pickle(types.FunctionType, reducefunction)
# Example
def test(x):
return x + 5
data = cPickle.dumps((test, test))
# In another Python environment
test, test2 = cPickle.loads(data)
test(5) == 10
test == test2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment