确定为什么不能腌制对象
我正在从类型为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.