为什么以下代码在作为脚本运行时会产生错误?在交互式 shell(剪切和粘贴)中运行时不会产生错误。
import cPickle as pickle
val1 = dict(fooblah=[], xy=[])
pickval1 = pickle.dumps(val1, protocol=2)
val2 = pickle.loads(pickval1)
assert val1 == val2
pickval2 = pickle.dumps(val2, protocol=2)
assert pickval1 == pickval2, (pickval1, pickval2)
泡菜的区别如下:
$ python /tmp/picklefun.py
Traceback (most recent call last):
File "/tmp/picklefun.py", line 10, in <module>
assert pickval1 == pickval2, (pickval1, pickval2)
AssertionError: ('\x80\x02}q\x01(U\x07fooblahq\x02]U\x02xyq\x03]u.',
'\x80\x02}q\x01(U\x07fooblah]U\x02xy]u.')