Python pickle not one-to-one: different pickles give same object

后端 未结 1 962
夕颜
夕颜 2021-01-23 04:24

Can someone explain this?

pickle.loads(b\'\\x80\\x03X\\x01\\x00\\x00\\x00.q\\x00h\\x00\\x86q\\x01.\') == pickle.loads(b\'\\x80\\x03X\\x01\\x00\\x00\\x00.q\\x00X\         


        
相关标签:
1条回答
  • 2021-01-23 04:56

    Pickles aren't unique; the pickle format is actually a tiny little programming language, and different programs (pickles) can produce the same output (unpickled object). From the docs:

    Since the pickle data format is actually a tiny stack-oriented programming language, and some freedom is taken in the encodings of certain objects, it is possible that the two modules [pickle and cPickle] produce different data streams for the same input objects. However it is guaranteed that they will always be able to read each other’s data streams.

    There's even a pickletools.optimize function that will take a pickle and output a better pickle. You're going to need to redesign your program.

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