Hi,

As you know we can't pickle the lambda functions in the standard pickle module. 
So, for that reason the shelve library also can't do that.

What if we can define the custom pickler and unpickler for the shelve module?

We couldn't do this before;

import shelve
with shelve.open("test_file") as sh:
squared = lambda x: x ** 2
sh['test_key'] = squared

Now, we can easily do it; 
(https://gist.github.com/furkanonder/436d1b23eede54650107924e747b5d77)

import dill
import shelve

with shelve.open("test_file_2", pickler=dill.Pickler, unpickler=dill.Unpickler) 
as sh:
squared = lambda x: x ** 2
sh['test_key'] = squared

I could easily solve this problem I had while using the Shelve module. I 
believe that shelve module will be a more useful module by adding support for 
custom unpickler and pickler.

Kindest regards,
Furkan Onder
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WRG534DE6FBMIYV6TPL2NK5EWK2BBCXR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to