简体   繁体   English

尝试腌制对象实例会引发腌制错误

[英]Trying to pickle object instance throws pickling error

I am trying to pickle class instance, referring to http://stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html However gives me 我正在尝试腌制类实例,请参阅http://stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html,但是给了我

try.py try.py

import pickle

class abc(object):
      def __init__(self):
         self.a = 10
      def save(self):
         pickle.dump(self,open("try.pkl","wb"))
if __name__ == '__main__':
      a = abc()
      abc.__module__ = "try"
      a.save()

pickle.PicklingError: Can't pickle <class 'try.abc'>: it's not the same object as try.abc

Am I making a mistake here ? 我在这里犯错了吗? Is there a different solution to pickle object for stand alone purpose 是否有其他解决方案来腌制对象以用于独立用途

If you take away your abc.__module__ = "try" everything works fine. 如果您拿走了abc.__module__ = "try"一切正常。

import pickle

class abc(object):
      def __init__(self):
         self.a = 10
      def save(self):
         pickle.dump(self,open("try.pkl","wb"))
if __name__ == '__main__':
      a = abc()
      a.save()

      # proof it worked
      with open('try.pkl','rb') as pkl_file:
          b = pickle.load(pkl_file)
      print(b.a)

I've added code at the end to reload the data and print it to show it was successful. 我已经在代码末尾添加了代码以重新加载数据并打印它以表明它是成功的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python腌制错误试图腌制pygame事件对象 - Python pickling error trying to pickle pygame Event object Python酸洗错误:TypeError:对象酸洗未返回列表 - Python pickling error: TypeError: object pickle not returning list 酸洗错误:不能泡菜<type 'function'> - Pickling error: Can't pickle <type 'function'> Python酸洗错误:TypeError:对象酸洗未返回列表。 numpy有问题吗? - Python pickling error: TypeError: object pickle not returning list. Issue with numpy? 酸洗 xarray 给出不能酸洗 '_thread.lock' object - Pickling xarray gives cannot pickle '_thread.lock' object 酸洗Matplotlib情节提升PicklingError:不能腌制&#39;RendererAgg&#39;对象 - Pickling Matplotlib plot raising PicklingError: Can't pickle 'RendererAgg' object 多处理酸洗错误:_pickle.PicklingError:不能酸洗<function myProcess at 0x02B2D420> :它与 __main__.myProcess 不是同一个对象 - multiprocessing pickling error: _pickle.PicklingError: Can't pickle <function myProcess at 0x02B2D420>: it's not the same object as __main__.myProcess 用python腌制的泡菜 - In a pickle with pickling in python 腌制一个对象作为其父类的实例? - Pickling an object as an instance of its parent class? 在joblib`Parallel`上下文中对`matlab`对象进行腌制时出错 - Error pickling a `matlab` object in joblib `Parallel` context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM