#source1 #from <python基礎教程> import shelve s = shelve.open('temp.dat') s['x'] = ['a', 'b', 'c'] s['x'].append('d') print s['x'] -------------------------- >>>['a', 'b', 'c'] 書上的解釋:1.列表['a', 'b', 'c']存儲在x下; 2.獲得存儲的表示, 并且根據它創建新的列表,而'd'加到這個副本.修改的版本還沒有被保存! 3.最終,再次獲得原始版本----沒有'd' 第2條沒想明白,為什么會創建新的列表.(source3中為什么沒創建,或者說為什么與source3的結果不一樣.) ======================== #source2 import shelves = shelve.open('temp.dat')s['x'] = ['a', 'b', 'c'] s. close() s = shelve.open('temp.dat') s['x'].append('d')print s['x'] -------------------------- >>>['a', 'b', 'c'] 這條是看到別的大神有說是因為s['x'] = ['a', 'b', 'c']這條執行完后沒有寫回, s. close()是確保其結果寫回.那s['x'].append('d')結果為什么還是和source1一樣. ======================= #source3 s = {} s['x'] = ['a', 'b', 'c'] s['x'].append('d') print s['x'] ------------------------- >>>['a', 'b', 'c', 'd']
關于python 的shelve模塊,求大俠
拉風的咖菲貓
2018-07-18 15:04:08