7

How can I pickle a python object which contains lambdas?

Can't pickle local object 'BaseDiscretizer.__init__.<locals>.<lambda>'

is the error I get when trying to pickle https://github.com/marcotcr/lime/blob/97a1e2d7c1adf7b0c4f0d3b3e9b15f6197b75c5d/lime/discretize.py when pickling the https://github.com/marcotcr/lime/blob/2703bcdcddd135947fe74e99cc270aa4fac3263a/lime/lime_tabular.py#L88 LimeTabularExplainer

Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
  • Lambdas can close over arbitrarily complicated pieces of context, up to the entire call stack. How could you pickle something like that? – trent Aug 16 '17 at 14:31
  • And to answer my own comment: [by capturing the entire stack, of course!](https://github.com/uqfoundation/dill) – trent Aug 16 '17 at 14:44

1 Answers1

13

The standard pickle module cannot serialize lambdas, but there is a third party package called dill which supports them.

trent
  • 25,033
  • 7
  • 51
  • 90
  • thanks. `with open('data', 'wb') as f: dill.dump(exp, f)`works fine, however when reading it back: `with open('data', 'r') as f: dill.loads(f)` I get `a bytes-like object is required, not '_io.TextIOWrapper' ` – Georg Heiler Aug 16 '17 at 17:42
  • This is finally working: `with open('data', 'rb') as f: foo = dill.load(f)` – Georg Heiler Aug 16 '17 at 17:45