确定为什么不能腌制对象 [英] Establishing why an object can't be pickled

查看:90
本文介绍了确定为什么不能腌制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从类型为Object的api接收对象t.我无法腌制它,得到了错误:

I'm receiving an object, t, from an api of type Object. I am unable to pickle it, getting the error:

  File "p.py", line 55, in <module>
    pickle.dump(t, open('data.pkl', 'wb'))
  File "/usr/lib/python2.6/pickle.py", line 1362, in dump
    Pickler(file, protocol).dump(obj)
  File "/usr/lib/python2.6/pickle.py", line 224, in dump
    self.save(obj)
  File "/usr/lib/python2.6/pickle.py", line 313, in save
    (t.__name__, obj))
pickle.PicklingError: Can't pickle 'Object' object: <Object object at 0xb77b11a0>

当我执行以下操作时:

for i in dir(t): print(type(i))

我只得到字符串对象:

<type 'str'>
<type 'str'>
<type 'str'>
...
<type 'str'>
<type 'str'>
<type 'str'>

如何打印我的Object对象的内容,以了解为什么不能腌制它?

How can I print the contents of my Object object in order to understand why it cant be pickled?

该对象还可能包含指向QT对象的C指针,在这种情况下,我对该对象进行腌制是没有意义的.但是我还是想看看该对象的内部结构以建立它.

Its also possible that the object contains C pointers to QT objects, in which case it wouldn't make sense for me to pickle the object. But again I would like to see the internal structure of the object in order to establish this.

推荐答案

您可能需要阅读 Python文档,然后检查API的Object类.

You may want to read the python docs and check your API's Object class afterwards.

关于对象的内部结构",通常实例属性存储在__dict__属性中(并且由于未腌制类属性,因此您仅关心实例属性)-但请注意,您还将必须递归检查每个属性的__dict__.

With respect to the "internal structure of the object", usually instance attributes are stored in the __dict__ attribute (and since class attributes are not pickled you only care about the instance attributes) - but note that you'll also have to recursively inspect the __dict__s for each attribute.

这篇关于确定为什么不能腌制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆