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 技術手冊 第三版
202
|
6 章 模組
本章討論模組的創建(creation)和載入(loading)。它也涵蓋如何將模組
包成套件,以及使用 Python 的發布工具(
distutils
setuptools
)來安裝
發行的套件,還有準備套件以便發布,後面這個主題更詳盡地涵蓋於第 25
章。本章最後討論如何以最佳的方式管理你的 Python 環境。
模組物件
一個模組(module)是帶有任意命名的屬性的一種 Python 物件,你可以繫
結(bind)和參考(reference)它們。名為
aname
的一個模組之程式碼通
常位於一個叫做
aname.py
的檔案中,如後面「模組載入」中所涵蓋的。
Python 中,模組是物件(值),處理的方式就跟其他物件一樣。因此,
你可以將一個模組作為一個呼叫中的引數傳入給函式。同樣地,函式可以
回傳一個模組作為一個呼叫的結果。一個模組,就跟其他任何的物件一
樣,可以被繫結至一個變數(variable)、容器中的一個項目(item),
是物件的一個屬性(attribute)。模組可以是字典中的鍵值(keys)或值
values),也可以是集合中的成員(members)。舉例來說,
sys.modules
字典(涵蓋於後面的「模組載入」中),就將模組物件當成其值來持有。
模組可被視為跟 Python 中其他值一樣來對待的這個事實,通常表達為「模
組是
一級
first-class
物件」。
import 述句
你可以在 Python 原始碼檔案中執行一個
import
述句來將其他任何的
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