Common use-cases for pickle in Python

后端 未结 9 1581
爱一瞬间的悲伤
爱一瞬间的悲伤 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:22

    Some uses that I have come across:

    1) saving a program's state data to disk so that it can carry on where it left off when restarted (persistence)

    2) sending python data over a TCP connection in a multi-core or distributed system (marshalling)

    3) storing python objects in a database

    4) converting an arbitrary python object to a string so that it can be used as a dictionary key (e.g. for caching & memoization).

    There are some issues with the last one - two identical objects can be pickled and result in different strings - or even the same object pickled twice can have different representations. This is because the pickle can include reference count information.

    To emphasise @lunaryorn's comment - you should never unpickle a string from an untrusted source, since a carefully crafted pickle could execute arbitrary code on your system. For example see https://blog.nelhage.com/2011/03/exploiting-pickle/

提交回复
热议问题