Search code examples
pythonserializationpickledill

Serialize a Python method with global variables by dill


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:

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 :)


Solution

  • I'm the dill author. It works if you use the recurse setting, which handles globals very similarly to how cloudpickle handles globals.

    >>> import dill
    >>> FLAG = 100
    >>> 
    >>> class A:
    ...   def func(self):
    ...     return FLAG*10
    ... 
    >>> a = A()
    >>> dump = dill.dumps(a.func, recurse=True)
    >>> del FLAG
    >>> foo = dill.loads(dump)
    >>> foo()
    1000
    >>> 
    

    dill provides several settings, that give you serialization variants. If you want to always use this setting, then you can do this:

    >>> dill.settings['recurse'] = True