Skip to content

Pickling class instance in dict fails unless you try pickling the object directly first #540

@JohnGoertz

Description

@JohnGoertz

I've found a very bizarre issue where, if I try to pickle an object (with various instance attributes) in a dictionary using dill, it fails on the first attempt. When un-pickling the object, it appears none of the instance attributes have been pickled along with the object. No matter how many times I re-run that cell (in a jupyter notebook), it fails. If I then try to pickle the same object directly, it works, and a subsequent attempt at pickling it in a dictionary now works too.

I've attached reproducible code below, and you can run it in this Colab notebook.

Note, I am the author of gumbi, so please feel free to let me know if this is some issue with gumbi itself!

Imports and setup

!pip install gumbi

import dill
import numpy as np
import gumbi as gmb

stdzr = gmb.Standardizer(d={'μ': -0.307, 'σ': 0.158}, log_vars=['d'])
pa = gmb.parray(d=np.arange(5,10)/10, stdzr=stdzr)
pa
# ('d',): [(0.5,) (0.6,) (0.7,) (0.8,) (0.9,)]

First attempt

Saving the object in a dictionary fails, the instance attributes seemingly don't get pickled:

with open('test.pkl', 'wb') as f:
    dill.dump({'pa': pa}, f)
    
with open('test.pkl', 'rb') as f:
    loaded = dill.load(f)
    
loaded['pa']
# AttributeError: 'ParameterArray' object has no attribute 'names'

A diversion

Saving the object on its own works:

with open('test.pkl', 'wb') as f:
    dill.dump(pa, f)
    
with open('test.pkl', 'rb') as f:
    loaded = dill.load(f)
    
loaded
# ('d',): [(0.5,) (0.6,) (0.7,) (0.8,) (0.9,)]

Second attempt

And now saving the object in a dictionary works:

with open('test.pkl', 'wb') as f:
    dill.dump({'pa': pa}, f)
    
with open('test.pkl', 'rb') as f:
    loaded = dill.load(f)
    
loaded['pa']
# ('d',): [(0.5,) (0.6,) (0.7,) (0.8,) (0.9,)]

Note that running the "diversion" before ever running the dictionary pickle works too!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions