How to pickle functions/classes defined in __main__ (python)

前端 未结 3 1284
清酒与你
清酒与你 2021-01-18 14:08

I would like to be able to pickle a function or class from within __main__, with the obvious problem (mentioned in other posts) that the pickled function/class is in the __m

3条回答
  •  囚心锁ツ
    2021-01-18 14:38

    Pickle seems to look at the main scope for definitions of classes and functions. From inside the module you're unpickling from, try this:

    import myscript
    import __main__
    __main__.myclass = myscript.myclass
    #unpickle anywhere after this
    

提交回复
热议问题