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 技術手冊 第三版
436
|
13 章 執行的控制
資訊框(frame)型別
模組
sys
中的函式
_getframe
會從 Python 的呼叫堆疊(call stack)回傳一
frame(資訊框)物件。一個 frame 物件具有的屬性提供在那個資訊框
中執行的程式碼和執行狀態的相關資訊。模組
traceback
inspect
幫助你
存取與顯示這種資訊,特別是在有例外被處理的時候。第 16 章提供資訊框
frames)和追溯資訊(tracebacks)的更多細節,並涵蓋模組
inspect
它是進行這種內省(introspection)工作的最佳方式。
記憶體回收
Python 的記憶體回收(garbage collection)一般會透明地且自動地進行,
但你可以選擇施加某些控制。一般的原則是 Python 會在物件
x
變得無法抵
達(unreachable)的一段時間後回收每個這種
x
,所謂的無法抵達,就是
沒辦法從正在執行中的一個函式實體的一個區域變數,或從一個已載入的
模組的一個全域變數開始,經由參考串鏈(chain of references)抵達
x
正常來說,一個物件
x
會在完全沒有參考指向
x
的時候變得無法抵達。此
外,一組物件(a group of objects)會在它們參考彼此,但沒有全域或區域
變數參考它們其中任何一個(即使是間接的)的時候變得無法抵達(這種
情況叫做
mutual reference loop
相互參考迴圈
)。
傳統的 Python 會為每個物件
x
保存一個計數(count),稱作
參考計數
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