python pickle object with lambdas
Asked Answered
B

1

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

Bates answered 16/8, 2017 at 14:15 Comment(2)
Lambdas can close over arbitrarily complicated pieces of context, up to the entire call stack. How could you pickle something like that?Jordans
And to answer my own comment: by capturing the entire stack, of course!Jordans
J
13

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

Jordans answered 16/8, 2017 at 14:15 Comment(2)
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' Bates
This is finally working: with open('data', 'rb') as f: foo = dill.load(f)Bates

© 2022 - 2024 — McMap. All rights reserved.