3 回答
TA贡献1998条经验 获得超6个赞
class
class
__dict__
pickle
pickle
pickle
cPickle
object
dill
Python 2.7.8 (default, Jul 13 2014, 02:29:54) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import pickle>>> class Company:... pass... >>> company1 = Company()>>> company1.name = 'banana'>>> company1.value = 40>>> with open('company.pkl', 'wb') as f:... pickle.dump(company1, f, pickle.HIGHEST_PROTOCOL)... >>>
Python 2.7.8 (default, Jul 13 2014, 02:29:54) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import pickle>>> with open('company.pkl', 'rb') as f:... company1 = pickle.load(f)... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1378, in load return Unpickler(file).load() File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 858, in load dispatch[key](self) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1090, in load_global klass = self.find_class(module, name) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1126, in find_class klass = getattr(mod, name)AttributeError: 'module' object has no attribute 'Company'>>>
pickle
dill
lambda
Python 2.7.8 (default, Jul 13 2014, 02:29:54) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import dill >>> class Company:... pass... >>> company1 = Company()>>> company1.name = 'banana'>>> company1.value = 40>>> >>> company2 = lambda x:x>>> company2.name = 'rhubarb'>>> company2.value = 42>>> >>> with open('company_dill.pkl', 'wb') as f:... dill.dump(company1, f)... dill.dump(company2, f)... >>>
Python 2.7.8 (default, Jul 13 2014, 02:29:54) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import dill>>> with open('company_dill.pkl', 'rb') as f:... company1 = dill.load(f)... company2 = dill.load(f)... >>> company1 <__main__.Company instance at 0x107909128>>>> company1.name'banana'>>> company1.value40>>> company2.name'rhubarb'>>> company2.value42>>>
pickle
dill
dill
__main__
pickle
dill
lambda
Python 2.7.8 (default, Jul 13 2014, 02:29:54) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import dill>>> class Company:... pass... >>> company1 = Company()>>> company1.name = 'banana'>>> company1.value = 40>>> >>> company2 = lambda x:x>>> company2.name = 'rhubarb'>>> company2.value = 42>>> >>> dill.dump_session('dill.pkl')>>>
Python 2.7.8 (default, Jul 13 2014, 02:29:54) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import dill>>> dill.load_session('dill.pkl')>>> company1.name'banana'>>> company1.value40>>> company2.name'rhubarb'>>> company2.value42>>> company2<function <lambda> at 0x1065f2938>
dill
dill
git+https://github.com/uqfoundation/dill.git@master#egg=dill
pip install dill
.
添加回答
举报