Here's an example code where FLAG
is a global variable. The method A.func
is a blackbox to me so that I don't know it calls FLAG
before serialization.
import dill as pickle
FLAG = 100
class A:
def func(self):
print FLAG * 10
a = A()
dump = pickle.dumps(a.func)
del FLAG
foo = pickle.loads(dump) <-- fail here "NameError: global name 'FLAG' is not defined"
foo()
In related questions:
- Serialize a python function with dependencies
- How to pickle a python function with its dependencies?
The most practical solution is using cloudpickle
. But it seems that dill
is more robust than cloudpickle
. So I'd like to stick to dill
or other mature picklers.
I don't mind to modify some dill
code by myself if necessary.
Thanks for any help in advance :)