爲什麼下面的代碼在作爲腳本運行時會產生錯誤?在交互式shell(剪切和粘貼)中運行時不會產生錯誤。不一致的cPickle
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.')
這可以是雙用替換EVAL檢查: ''' VAL1 =字典() EXEC 'VAL1 [ 「fooblah」] = []' EXEC 'VAL1 [ 「XY」] = []' ' '' – user650654 2013-05-09 15:22:15