Custom unpickler and pickler for the shelve module

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
participants (1)
-
furkanonder