Python: Pickling a dict with some unpicklable items

前端 未结 5 1460
半阙折子戏
半阙折子戏 2020-12-16 17:18

I have an object gui_project which has an attribute .namespace, which is a namespace dict. (i.e. a dict from strings to objects.)

(This is

5条回答
  •  -上瘾入骨i
    2020-12-16 17:28

    This is how I would do this (I did something similar before and it worked):

    1. Write a function that determines whether or not an object is pickleable
    2. Make a list of all the pickleable variables, based on the above function
    3. Make a new dictionary (called D) that stores all the non-pickleable variables
    4. For each variable in D (this only works if you have very similar variables in d) make a list of strings, where each string is legal python code, such that when all these strings are executed in order, you get the desired variable

    Now, when you unpickle, you get back all the variables that were originally pickleable. For all variables that were not pickleable, you now have a list of strings (legal python code) that when executed in order, gives you the desired variable.

    Hope this helps

提交回复
热议问题