Dau Zi
Dau Zi

Reputation: 320

How to set up dill to avoid storing the global variables when pickling a lambda function?

I create a lambda function that do not use any global variable in script 1. I pickle it using dill:

#.... some other things
import dill
f = lambda a: a
s1 = dill.dumps(f) # Store it somewhere

Similarly in script 2, I create the same function and pickle it using dill:

#.... some other things
import dill
f = lambda a: a
s2 = dill.dumps(f) # Store it somewhere

The goal of pickling both functions is to check if the two routines in script 1 and script 2 are the same. Now I load both functions from s1 and s2 and try to see if they are the same. I would expect the pickled function to be the same, i.e. s1 == s2. However, it seems like dill will store the global information of these functions as well, making the two functions loaded from s1 and s2 different.

import dill
f = lambda a: a
dill.loads(dill.dumps(f)).__globals__

For example when I use jupyter to run the above statements in script 1 and 2, the session information and other variables are also pickled.

Is there any way I can set dill so that it won't pickle global variables; and so that s1 == s2?

Upvotes: 0

Views: 34

Answers (0)

Related Questions