Storing instances using jsonpickle
Ned Batchelder
ned at nedbatchelder.com
Wed Sep 3 22:18:29 EDT 2014
On 9/3/14 6:30 PM, Josh English wrote:
> On Wednesday, September 3, 2014 1:53:23 PM UTC-7, Ned Batchelder wrote:
>
>> Pickle (and it looks like jsonpickle) does not invoke the class'
>> __init__ method when it reconstitutes objects. Your new __init__ is not
>> being run, so new attributes it defines are not being created.
>>
>> This is one of the reasons that people avoid pickle: being completely
>> implicit is very handy, but also fragile.
>>
>
> I seem to remember having this exact same frustration when I used pickle and shelve 15 years ago. I had hoped to have another way around this. I spent over a decade trying to make an XML-based database work, in part because of this limitation.
>
> Some days I get so frustrated I think the only data structure I should ever use is a dictionary.
>
> I suppose to make this sort of thing work, I should look at creating custom json encoders and decoders.
>
Typically, you need to decide explicitly on a serialized representation
for your data. Even if it's JSON, you need to decide what that JSON
looks like. Then you need to write code that converts the JSON-able
data structure (dict of lists, whatever) into your object. Often a
version number is a good idea so that you have some chance of using old
data as your code changes.
--
Ned Batchelder, http://nedbatchelder.com
More information about the Python-list
mailing list