I found a considerable answer from this post: Is there an easy way to pickle a python function (or otherwise serialize its code)?
however, the restored function seems slightly different from the original one, which fails my test.
Here's the sample code:
import marshal
# serialize
f1 = lambda x: x == 0
c1 = marshal.dumps(f1.func_code)
# deserialize
f2 = types.FunctionType(c1, globals())
# test
c1 = marshal.dumps(f1.func_code)
c2 = marshal.dumps(f2.func_code)
assert c1 == c2 # fails
Do you have any idea how to improve the serialization/deserialization to eliminate this distortion?
Or any suggestion on equality test part?
PS: take account of only simple lambda but not complex closure or ordinary function.