Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
392
|
11 章 續存和資料庫
使用
marshal
會使你的程式碼依存特定版本的
Python
在你的程式碼中使用
marshal
(就像前面的例子)前要多加考
慮:
marshal
的序列化可能會隨著 Python 的版本而變化,所以
使用
marshal
意味著你可能無法以某個版本的 Python 載入以
另一個版本的 Python 序列化的物件。
shelve 模組
shelve
模組統合了模組
pickle
(或 v2 中的
cPickle
,可用的時候)、
io
v3
中;在 v2 中,可用的話是
cStringIO
,否則為
StringIO
),以及
dbm
(和其
底層用來存取類 DBM 封存檔的模組,如後面「DBM 模組」,在 v3 中是如
此,v2 中則為
anydbm
)以提供一種簡單、輕量化的續存機制。
shelve
提供一個函式
open
,它與
anydbm.open
是多型(polymorphic)的。
shelve.open
所回傳的映射
s
用途比
anydbm.open
所回傳的映射
a
還要廣
泛。
a
的鍵值和值都必須是字串。
s
的鍵值必須是字串,但
s
的值可以是任
pickleable 的型別。
pickle
的自訂方式(
copy_reg
__getnewargs__
__
getstate__
__setstate__
)也適用於
shelve
,因為
shelve
會把序列化的
工作委派給
pickle
以可變的物件(mutable objects)使用
shelve
的時候要小心一個細微難察
的陷阱:當你在一個 shelf 中所存放的一個可變物件上進行作業,除非你把 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819