術語表

>>>

互動式 shell 的預設 Python 提示字元。常見於能在直譯器中以互動方式被執行的程式碼範例。

...

可以表示:

  • 在一個被縮排的程式碼區塊、在一對匹配的左右定界符(delimiter,例如括號、方括號、花括號或三引號)內部,或是在指定一個裝飾器 (decorator) 之後,要輸入程式碼時,互動式 shell 顯示的預設 Python 提示字元。

  • The three dots form of the Ellipsis object.

abstract base class(抽象基底類別)

抽象基底類別(又稱為 ABC)提供了一種定義介面的方法,作為 duck-typing(鴨子型別)的補充。其他類似的技術,像是 hasattr(),則顯得笨拙或是帶有細微的錯誤(例如使用魔術方法 (magic method))。ABC 採用虛擬的 subclass(子類別),它們並不繼承自另一個 class(類別),但仍可被 isinstance()issubclass() 辨識;請參閱 abc 模組的說明文件。Python 有許多內建的 ABC,用於資料結構(在 collections.abc 模組)、數字(在 numbers 模組)、串流(在 io 模組)及 import 尋檢器和載入器(在 importlib.abc 模組)。你可以使用 abc 模組建立自己的 ABC。

annotate function(註釋函式)

A function that can be called to retrieve the annotations of an object. This function is accessible as the __annotate__ attribute of functions, classes, and modules. Annotate functions are a subset of evaluate functions.

annotation(註釋)

一個與變數、class 屬性、函式的參數或回傳值相關聯的標籤。照慣例,它被用來作為 type hint(型別提示)。

在 runtime 的區域變數註釋無法被存取,但全域變數、類別屬性和函式的註釋,分別能夠以對模組、類別和函式呼叫 annotationlib.get_annotations() 來取得。

請參閱 variable annotationfunction annotationPEP 484PEP 526PEP 649,這些章節皆有此功能的說明。關於註釋的最佳實踐方法也請參閱 註釋 (annotation) 最佳實踐

argument(引數)

呼叫函式時被傳遞給 function(或 method)的值。引數有兩種:

  • 關鍵字引數 (keyword argument):在函式呼叫中,以識別字(identifier,例如 name=)開頭的引數,或是以 ** 後面 dictionary(字典)內的值被傳遞的引數。例如,35 都是以下 complex() 呼叫中的關鍵字引數:

    complex(real=3, imag=5)
    complex(**{'real': 3, 'imag': 5})
    
  • 位置引數 (positional argument):不是關鍵字引數的引數。位置引數可在一個引數列表的起始處出現,和(或)作為 * 之後的 iterable(可疊代物件)中的元素被傳遞。例如,35 都是以下呼叫中的位置引數:

    complex(3, 5)
    complex(*(3, 5))
    

引數會被指定給函式主體中的附名區域變數。關於支配這個指定過程的規則,請參閱Calls章節。在語法上,任何運算式都可以被用來表示一個引數;其評估值會被指定給區域變數。

另請參閱術語表的 parameter(參數)條目、常見問題中的引數和參數之間的差異,以及 PEP 362

asynchronous context manager(非同步情境管理器)

一個可以控制 async with 陳述式中所見環境的物件,而它是透過定義 __aenter__()__aexit__() method(方法)來控制的。由 PEP 492 引入。

asynchronous generator(非同步產生器)

一個會回傳 asynchronous generator iterator(非同步產生器疊代器)的函式。它看起來像一個以 async def 定義的協程函式 (coroutine function),但不同的是它包含了 yield 運算式,能生成一系列可用於 async for 迴圈的值。

這個術語通常用來表示一個非同步產生器函式,但在某些情境中,也可能是表示非同步產生器疊代器 (asynchronous generator iterator)。萬一想表達的意思不夠清楚,那就使用完整的術語,以避免歧義。

一個非同步產生器函式可能包含 await 運算式,以及 async forasync with 陳述式。

asynchronous generator iterator(非同步產生器疊代器)

一個由 asynchronous generator(非同步產生器)函式所建立的物件。

這是一個 asynchronous iterator(非同步疊代器),當它以 __anext__() method 被呼叫時,會回傳一個可等待物件 (awaitable object),該物件將執行非同步產生器的函式主體,直到遇到下一個 yield 運算式。

每個 yield 會暫停處理程序,並記住執行狀態(包括區域變數及擱置中的 try 陳述式)。當非同步產生器疊代器以另一個被 __anext__() 回傳的可等待物件有效地回復時,它會從停止的地方繼續執行。請參閱 PEP 492PEP 525

asynchronous iterable(非同步可疊代物件)

一個物件,它可以在 async for 陳述式中被使用。必須從它的 __aiter__() method 回傳一個 asynchronous iterator(非同步疊代器)。由 PEP 492 引入。

asynchronous iterator(非同步疊代器)

一個實作 __aiter__()__anext__() method 的物件。__anext__() 必須回傳一個 awaitable(可等待物件)。async for 會解析非同步疊代器的 __anext__() method 所回傳的可等待物件,直到它引發 StopAsyncIteration 例外。由 PEP 492 引入。

atomic operation

An operation that appears to execute as a single, indivisible step: no other thread can observe it half-done, and its effects become visible all at once. Python does not guarantee that high-level statements are atomic (for example, x += 1 performs multiple bytecode operations and is not atomic). Atomicity is only guaranteed where explicitly documented. See also race condition and data race.

attached thread state

A thread state that is active for the current OS thread.

When a thread state is attached, the OS thread has access to the full Python C API and can safely invoke the bytecode interpreter.

Unless a function explicitly notes otherwise, attempting to call the C API without an attached thread state will result in a fatal error or undefined behavior. A thread state can be attached and detached explicitly by the user through the C API, or implicitly by the runtime, including during blocking C calls and by the bytecode interpreter in between calls.

On most builds of Python, having an attached thread state implies that the caller holds the GIL for the current interpreter, so only one OS thread can have an attached thread state at a given moment. In free-threaded builds of Python, threads can concurrently hold an attached thread state, allowing for true parallelism of the bytecode interpreter.

attribute(屬性)

一個與某物件相關聯的值,該值大多能透過使用點分隔運算式 (dotted expression) 的名稱被參照。例如,如果物件 o 有一個屬性 a,則該屬性能以 o.a 被參照。

如果一個物件允許,給予該物件一個名稱不是由Names (identifiers and keywords)所定義之識別符 (identifier) 的屬性是有可能的,例如使用 setattr()。像這樣的屬性將無法使用點分隔運算式來存取,而是需要使用 getattr() 來取得它。

awaitable(可等待物件)

一個可以在 await 運算式中被使用的物件。它可以是一個 coroutine(協程),或是一個有 __await__() method 的物件。另請參閱 PEP 492

BDFL

Benevolent Dictator For Life(終身仁慈獨裁者),又名 Guido van Rossum,Python 的創造者。

binary file(二進位檔案)

一個能夠讀取和寫入 bytes-like objects(類位元組串物件)的 file object(檔案物件)。二進位檔案的例子有:以二進位模式('rb''wb''rb+')開啟的檔案、sys.stdin.buffersys.stdout.buffer,以及 io.BytesIOgzip.GzipFile 實例。

另請參閱 text file(文字檔案),它是一個能夠讀取和寫入 str 物件的檔案物件。

borrowed reference(借用參照)

在 Python 的 C API 中,借用參照是一個對物件的參照,其中使用該物件的程式碼並不擁有這個參照。如果該物件被銷毀,它會成為一個迷途指標 (dangling pointer)。例如,一次垃圾回收 (garbage collection) 可以移除對物件的最後一個 strong reference(強參照),而將該物件銷毀。

borrowed reference 呼叫 Py_INCREF() 以將它原地 (in-place) 轉換為 strong reference 是被建議的做法,除非該物件不能在最後一次使用借用參照之前被銷毀。Py_NewRef() 函式可用於建立一個新的 strong reference

bytes-like object(類位元組串物件)

一個支援緩衝協定 (Buffer Protocol)且能夠匯出 C-contiguous 緩衝區的物件。這包括所有的 bytesbytearrayarray.array 物件,以及許多常見的 memoryview 物件。類位元組串物件可用於處理二進位資料的各種運算;這些運算包括壓縮、儲存至二進位檔案和透過 socket(插座)發送。

有些運算需要二進位資料是可變的。說明文件通常會將這些物件稱為「可讀寫的類位元組串物件」。可變緩衝區的物件包括 bytearray,以及 bytearraymemoryview。其他的運算需要讓二進位資料被儲存在不可變物件(「唯讀的類位元組串物件」)中;這些物件包括 bytes,以及 bytes 物件的 memoryview

bytecode(位元組碼)

Python 的原始碼會被編譯成位元組碼,它是 Python 程式在 CPython 直譯器中的內部表示法。該位元組碼也會被暫存在 .pyc 檔案中,以便第二次執行同一個檔案時能夠更快速(可以不用從原始碼重新編譯為位元組碼)。這種「中間語言 (intermediate language)」據說是運行在一個 virtual machine(虛擬機器)上,該虛擬機器會執行與每個位元組碼對應的機器碼 (machine code)。要注意的是,位元組碼理論上是無法在不同的 Python 虛擬機器之間運作的,也不能在不同版本的 Python 之間保持穩定。

位元組碼的指令列表可以在 dis 模組的說明文件中找到。

callable(可呼叫物件)