Does pickle always give the same bytes?

I am wondering how deterministic is pickle: if I give it two equivalent objects, will they serialize to the same bytes?

There are some notes scattered around the web, but I haven’t got a synthesis of them.

Is there a practical case that fails the basic test like this?

(Edited for specificity after reply)

type(x) == type(y) and x == y and pickle.dumps(x) == pickle.dumps(y)

x = 12
y = 12.0
x == y
True
pickle.dumps(x) == pickle.dumps(y)
False

What about objects of the same type?

type(x) == type(y) and x == y and pickle.dumps(x) == pickle.dumps(y)

Nothing guarantees you that. There are several so-called pickle protocols and the one used by default depends on your version of Python. Please read pickle — Python object serialization — Python 3.9.2 documentation.

Here you go:

>>> x = {1: None, 2: None}
>>> y = {2: None, 1: None}
>>> x == y
True
>>> pickle.dumps(x) == pickle.dumps(y)
False

Here is the opposite situation:

>>> x = float('nan')
>>> y = x
>>> x == y
False
>>> pickle.dumps(x) == pickle.dumps(y)
True
1 Like