米琪卡哇伊
我认为假设对象是class..如果不是class?还有一个假设,即在解释器中没有定义对象。如果它是在解释器中定义的呢?另外,如果属性是动态添加的呢?当某些python对象将属性添加到它们的__dict__在创造之后,pickle不尊重这些属性的添加(也就是说,它“忘记”它们是添加的-因为pickle通过引用对象定义进行序列化)。在所有这些情况下,pickle和cPickle会让你失望的。如果您希望保存object(任意创建),其中有属性(或者添加在对象定义中,或者随后添加)…你最好的选择是dill,它可以序列化python中的几乎任何东西。我们从…类开始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'>>>OOPS…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会话,稍后再返回。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不是python标准库的一部分。因此,如果您无法在服务器上安装python包,那么就不能使用它。但是,如果您能够在系统上安装python包,则可以获得最新的dill带着git+https://github.com/uqfoundation/dill.git@master#egg=dill..您可以通过以下方式获得最新发布的版本pip install dill.