Python serialize lexical closures?

后端 未结 5 1017
遇见更好的自我
遇见更好的自我 2020-12-14 18:24

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

5条回答
  •  渐次进展
    2020-12-14 19:03

    Recipe 500261: Named Tuples contains a function that defines a class on-the-fly. And this class supports pickling.

    Here's the essence:

    result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
    

    Combined with @Greg Ball's suggestion to create a new class at runtime it might answer your question.

提交回复
热议问题