这是我需要腌制的课程:
class sdict(dict):
def __getattr__(self, attr):
return self.get(attr, None)
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
__reduce__ = dict.__reduce__
在我看来,__reduce__
应该注意酸洗,而是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-28-1cc94b920737> in <module>()
----> 1 pickle.dumps(sdict(a=1))
/usr/lib/python2.6/pickle.pyc in dumps(obj, protocol)
1364 def dumps(obj, protocol=None):
1365 file = StringIO()
-> 1366 Pickler(file, protocol).dump(obj)
1367 return file.getvalue()
1368
/usr/lib/python2.6/pickle.pyc in dump(self, obj)
222 if self.proto >= 2:
223 self.write(PROTO + chr(self.proto))
--> 224 self.save(obj)
225 self.write(STOP)
226
/usr/lib/python2.6/pickle.pyc in save(self, obj)
304 reduce = getattr(obj, "__reduce_ex__", None)
305 if reduce:
--> 306 rv = reduce(self.proto)
307 else:
308 reduce = getattr(obj, "__reduce__", None)
/usr/lib/python2.6/copy_reg.pyc in _reduce_ex(self, proto)
82 dict = None
83 else:
---> 84 dict = getstate()
85 if dict:
86 return _reconstructor, args, dict
TypeError: 'NoneType' object is not callable
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (171, 0))
我也应该定义__getstate__
吗?我完全迷失了所有这些可用于自定义酸洗的多种方法: __getinitargs__
、、、__getstate__
等等__reduce__
……
谷歌只向我推荐我发现非常不清楚的官方泡菜文档。有人可以为我澄清一下或指出一个好的泡菜教程吗?