Cannot marshal <class 'decimal.Decimal'> objects
Skip Montanaro
skip.montanaro at gmail.com
Fri Nov 27 15:05:11 EST 2020
> I am getting this error.
I assume you mean the email subject. It doesn't work in 3.8 either:
>>> import decimal
>>> d = decimal.Decimal(3.5)
>>> d
Decimal('3.5')
>>> import marshal
>>> marshal.dumps(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: unmarshallable object
but that's not surprising to me. The marshal module is more-or-less
meant to serialize Python byte code. Pickle is more generally used for
object serialization. Why not use it?
>>> import pickle, decimal
>>> d = decimal.Decimal(3.5)
>>> pickle.dumps(d)
b'\x80\x04\x95!\x00\x00\x00\x00\x00\x00\x00\x8c\x07decimal\x94\x8c\x07Decimal\x94\x93\x94\x8c\x033.5\x94\x85\x94R\x94.'
That's meant more for serialization of data objects.
Skip
More information about the Python-list
mailing list