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 技術手冊 第三版
38
|
2 章 Python 直譯器
執行 Python 程式
不管你是用什麼工具產生你的 Python 應用程式,你都可以把你的應用程
式看成是一組 Python 原始碼檔案,而它們是一般的文字檔。一個
指令稿
script
是你能夠直接執行的檔案。一個
模組
module
是你能夠匯入
import,如第 6 章所涵蓋的)來為其他檔案或互動式工作階段提供功能
的一個檔案。一個 Python 檔案可以
同時
是一個模組(匯入時提供功能性)
一個指令稿(適合直接執行)。一個實用且普及的慣例是,主要要被當
作模組匯入的 Python 檔案,被直接執行時,應該執行一些簡單的自我測
試,如後面章節的「測試」所涵蓋的。
Python 直譯器會在必要時自動編譯 Python 原始碼檔案。Python 的原始
碼檔案通常會有延伸檔名
.py
。在 v2 中,Python 會將每個模組經過編譯
後的 bytecode 檔案放在與模組原始碼相同的目錄中,它們有相同的基本
名稱,並有延伸檔名
.pyc
(或是以
-O
選項執行 Python 時的
.pyo
)。 v3
中,Python 會將編譯後的 bytecode 儲存在含有模組原始碼的目錄之子目
__pycache__
中,並帶有版本特定的延伸檔名,並標註有最佳化層級
optimization level)。
以選項
-B
執行 Python 來避免將編譯後的 bytecode 儲存到磁碟,這很適合
用在從唯讀(read-only)磁碟匯入模組的時候。此外,直接執行指令稿的
時候,Python ...
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