Is there a way to serialize a lexical closure in Python using the standard library? pickle and marshal appear not to work with lexical closures. I don’t really care about the details of binary vs. string serialization, etc., it just has to work. For example:
def foo(bar, baz) : def closure(waldo) : return baz * waldo return closure
I’d like to just be able to dump instances of closure to a file and read them back.
Edit: One relatively obvious way that this could be solved is with some reflection hacks to convert lexical closures into class objects and vice-versa. One could then convert to classes, serialize, unserialize, convert back to closures. Heck, given that Python is duck typed, if you overloaded the function call operator of the class to make it look like a function, you wouldn’t even really need to convert it back to a closure and the code using it wouldn’t know the difference. If any Python reflection API gurus are out there, please speak up.
If you simply use a class with a
__call__method to begin with, it should all work smoothly withpickle.On the other hand, a hack which converted a closure into an instance of a new class created at runtime would not work, because of the way
pickledeals with classes and instances.pickledoesn’t store classes; only a module name and class name. When reading back an instance or class it tries to import the module and find the required class in it. If you used a class created on-the-fly, you’re out of luck.