Python: Alternatives to pickling a module

前端 未结 2 843
盖世英雄少女心
盖世英雄少女心 2021-01-14 17:49

I am working on my program, GarlicSim, in which a user creates a simulation, then he is able to manipulate it as he desires, and then he can save it to file.

I recen

相关标签:
2条回答
  • 2021-01-14 18:21

    If you have the original code for the simulation package modules, which I presume are dynamically generated, then I would suggest serializing that and reconstructing the modules when loaded. You would do this in the Project.__getstate__() and Project.__setstate__() methods.

    0 讨论(0)
  • 2021-01-14 18:45

    If the project somehow has a reference to a module with stuff you need, it sounds like you might want to refactor the use of that module into a class within the module. This is often better anyway, because the use of a module for stuff smells of a big fat global. In my experience, such an application structure will only lead to trouble.

    (Of course the quick way out is to save the module's dict instead of the module itself.)

    0 讨论(0)
提交回复
热议问题