Created
March 30, 2012 10:48
-
-
Save Arachnid/2250737 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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