WEB开发网
开发学院软件开发Python Python 持久性管理 阅读

Python 持久性管理

 2007-03-30 12:29:39 来源:WEB开发网   
核心提示: >清单 5. 递归引用>>> l = [1, 2, 3]>>> l.append(l)>>> l[1, 2, 3, [...]]>>> l[3][1, 2, 3, [...]]>>> l[3][

>清单 5. 递归引用

>>> l = [1, 2, 3]
>>> l.append(l)
>>> l
[1, 2, 3, [...]]
>>> l[3]
[1, 2, 3, [...]]
>>> l[3][3]
[1, 2, 3, [...]]
>>> p = pickle.dumps(l)
>>> l2 = pickle.loads(p)
>>> l2
[1, 2, 3, [...]]
>>> l2[3]
[1, 2, 3, [...]]
>>> l2[3][3]
[1, 2, 3, [...]]

现在,看一个循环引用的示例:

清单 6. 循环引用

>>> a = [1, 2]
>>> b = [3, 4]
>>> a.append(b)
>>> a
[1, 2, [3, 4]]
>>> b.append(a)
>>> a
[1, 2, [3, 4, [...]]]
>>> b
[3, 4, [1, 2, [...]]]
>>> a[2]
[3, 4, [1, 2, [...]]]
>>> b[2]
[1, 2, [3, 4, [...]]]
>>> a[2] is b
1
>>> b[2] is a
1
>>> f = file('temp.pkl', 'w')
>>> pickle.dump((a, b), f)
>>> f.close()
>>> f = file('temp.pkl', 'r')
>>> c, d = pickle.load(f)
>>> f.close()
>>> c
[1, 2, [3, 4, [...]]]
>>> d
[3, 4, [1, 2, [...]]]
>>> c[2]
[3, 4, [1, 2, [...]]]
>>> d[2]
[1, 2, [3, 4, [...]]]
>>> c[2] is d
1
>>> d[2] is c
1

上一页  2 3 4 5 6 7 8 9 10  下一页

Tags:Python 持久性 管理

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接