pythonpickleequality

Python pickled object equality guarantees


I am wondering if, and if so which guarantees there are about the pickle module when using pickle.dump[s].

Specifically for my problem I am pickling list[T] where T can be bool, int, decimal, time, date, datetime, timedelta or str, so the list is homogeneous (one type per list). I wonder if it is guaranteed that lists that would be equal in python are also guaranteed to have the same pickled result, at least for the types given.

I couldn't find any guarantees online, but from some basic testing I couldn't find a case where the data would be different.

So TL;DR:

Given two list[T] where T is bool | int | decimal | time | date | datetime | timedelta | str that are equal in python, will the resulting pickle.dump() objects also be equal?


Solution

  • There are cases where values of different types are equal, such as 1 = 1.0 and 0 == False. Lists containing these values will be equal, but the pickle dumps are different so that it can restore them with the correct types.

    >>> l1 = [0] # T is int
    >>> l2 = [False] # T is bool
    >>> l1 == l2
    True
    >>> pickle.dumps(l1) == pickle.dumps(l2)
    False