I am trying to instanciate an object from a custom class, and while doing that to set some class variables, and dump the object. Then, in another file, I want to load the dumped object in order to retrieve the class variables. I will post a small example below:
import dill
class RandomClass:
mean = 0
def __init__(self):
self.name = "random_name"
self.set_mean(5)
@classmethod
def set_mean(cls, value):
cls.mean = value
obj = RandomClass()
dill.dump(obj, open("test.pkl","wb"))
Then, I want to load the object and retrieve the class variable by doing:
import dill
obj = dill.load(open("test.pkl", "rb"))
obj.mean
However, while doing so, I am getting the following error:
File "/Users/username/Library/Caches/pypoetry/virtualenvs/projectname-py3.6/lib/python3.6/site-packages/dill/_dill.py", line 305, in load obj = pik.load() File "/Users/username/Library/Caches/pypoetry/virtualenvs/projectname-py3.6/lib/python3.6/site-packages/dill/_dill.py", line 577, in _load_type return _reverse_typemap[name]
Looking forward to your answers!
I'm the dill
author. I can't reproduce your error. See below ( copy
is just dump
then load
):
Python 3.6.6 (default, Jun 28 2018, 05:53:46)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> class RandomClass:
... mean = 0
... def __init__(self):
... self.name = "random_name"
... self.set_mean(5)
... @classmethod
... def set_mean(cls, value):
... cls.mean = value
...
>>> obj = RandomClass()
>>> obj.mean
5
>>> dill.copy(obj)
<__main__.RandomClass object at 0x10747cba8>
>>>
If your error is persisting, then maybe the best route is to fill out a ticket on the dill
GitHub page, and give the version of dill
you are using and your OS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.