Common use-cases for pickle in Python

后端 未结 9 1578
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 04:56

I\'ve looked at the pickle documentation, but I don\'t understand where pickle is useful.

What are some common use-cases for pickle?

相关标签:
9条回答
  • 2020-12-02 05:32

    I can tell you the uses I use it for and have seen it used for:

    • Game profile saves
    • Game data saves like lives and health
    • Previous records of say numbers inputed to a program

    Those are the ones I use it for at least

    0 讨论(0)
  • 2020-12-02 05:35

    To add a real-world example: The Sphinx documentation tool for Python uses pickle to cache parsed documents and cross-references between documents, to speed up subsequent builds of the documentation.

    0 讨论(0)
  • 2020-12-02 05:39

    Pickle is like "Save As.." and "Open.." for your data structures and classes. Let's say I want to save my data structures so that it is persistent between program runs.

    Saving:

    with open("save.p", "wb") as f:    
        pickle.dump(myStuff, f)        
    

    Loading:

    try:
        with open("save.p", "rb") as f:
            myStuff = pickle.load(f)
    except:
        myStuff = defaultdict(dict)
    

    Now I don't have to build myStuff from scratch all over again, and I can just pick(le) up from where I left off.

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