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 技術手冊 第三版
201
6
模組
一個典型的 Python 程式是由數個原始碼檔案(source files)所構成。每個
原始碼檔案都是一個
模組
module
,將程式碼與資料包成一組以便再利
用。一般來說,模組都彼此獨立,如此其他程式就能重複使用它們所需的
特定模組。有的時候,為了管理複雜度,你會將相關的模組包在一起成為
一個
套件
package
,一種階層式的樹狀結構。
一個模組會使用
import
from
述句明確地建立與其他模組的依存關係
dependencies)。在某些程式語言中,全域變數(global variables)提
供了一種隱藏的管道用於模組之間的接合(coupling)。 Python 中,全
域變數並不是對所有模組來說都是全域的,而只是單一模組物件的屬性
attributes)。因此,Python 的模組永遠都會以明確且可維護的方式進行
溝通。
Python 也支援
擴充模組
extension modules
的使用,它們是以其他語言
(例如 CC++Java C#)編寫的模組。對於匯入(import)一個模
組的 Python 程式碼而言,該模組是純 Python 的模組還是擴充模組並不重
要。你永遠都可以先用 Python 編寫一個模組,之後,如果你需要速度的
話,你再以較低階的語言重構(refactor)及重新編寫模組的某些部分,
而不必變更用到那些模組的客戶端程式碼。第 24 章會示範如何以 C
Cython 撰寫擴充功能(extensions)。
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