builtins --- 內建函式與例外¶
此處說明所有內建函式與例外。它們也可透過 builtins 模組取得。
函式與型別¶
- abs(x: Any) Any¶
回傳數值的絕對值。引數可以是整數、浮點數,或任何實作
__abs__()的物件。
- class bool(x: Any = False)¶
回傳布林值,即
True或False之一。x 會使用標準的真值測試程序進行轉換。
- class bytearray(source: int | str | Iterable[int] | bytes = b'', encoding: str = 'utf-8', errors: str = 'strict')¶
範圍為 0 至 255 之整數的可變序列。建構規則與
bytes相同:可由整數(建立該大小的零填充緩衝區)、整數的可迭代物件、帶有encoding的字串,或任何符合緩衝協定的物件建立。支援標準序列操作以及原地修改。- classmethod fromhex(string: str) bytearray¶
由十六進位數字對組成的字串建構
bytearray。數字對之間的空白會被略過;非十六進位字元會引發ValueError。
- center(width: int, fillbyte: bytes) bytes¶
回傳內容置中於長度為 width 之序列中的副本,並以 fillbyte 填補。與 CPython 不同,fillbyte 為必要參數。當 width 不大於目前長度時,會原封不動回傳資料。
- endswith(suffix: bytes, start: int = 0, end: int = -1) bool¶
若內容以 suffix 結尾,則回傳
True。與 CPython 不同,suffix 不能是值的元組。
- extend(iterable: Iterable[int]) None¶
將 iterable 中的所有項目附加到 bytearray 的尾端。作為 CPython 的擴充,可使用任何支援緩衝協定的物件。
- replace(old: bytes, new: bytes, count: int = -1) bytes¶
回傳將所有 old 出現處替換為 new 的副本。若提供 count,則僅替換前 count 次出現。
- rsplit(sep: bytes | None = None, maxsplit: int = -1) list¶
在 sep 出現處分割為片段清單,從右側起最多進行 maxsplit 次分割。若 sep 為
None或省略,則以連續的 ASCII 空白分割。
- split(sep: bytes | None = None, maxsplit: int = -1) list¶
在 sep 出現處分割為片段清單。若 sep 為
None或省略,則以連續的 ASCII 空白分割,並忽略前導/尾隨空白。
- startswith(prefix: bytes, start: int = 0, end: int = -1) bool¶
若內容以 prefix 開頭,則回傳
True。與 CPython 不同,prefix 不能是元組,且 end 雖被接受但會被忽略。
- class bytes(source: int | str | Iterable[int] = b'', encoding: str = 'utf-8', errors: str = 'strict')¶
範圍為 0 至 255 之整數的不可變序列。可由整數(零填充緩衝區)、整數的可迭代物件、帶有
encoding的字串,或任何符合緩衝協定的物件建立。Bytes 字面值使用b'...'語法。- classmethod fromhex(string: str) bytes¶
由十六進位數字對組成的字串建構
bytes物件。數字對之間的空白會被略過;非十六進位字元會引發ValueError。
- center(width: int, fillbyte: bytes) bytes¶
回傳置中於長度為 width 之序列中的副本,並以 fillbyte(長度為 1 的 bytes,提供填補位元組)填補。與 CPython 不同,fillbyte 為必要參數。當 width 不大於其長度時,會原封不動回傳原物件。
- decode(encoding: str = 'utf-8') str¶
回傳由 bytes 解碼而成的
str。在 MicroPython 中,encoding 引數雖被接受但實際上會被忽略(bytes 會被重新解讀為 UTF-8)。
- endswith(suffix: bytes, start: int = 0, end: int = -1) bool¶
若 bytes 以 suffix 結尾,則回傳
True。與 CPython 不同,suffix 不能是要嘗試的值元組。
- find(sub: bytes, start: int = 0, end: int = -1) int¶
回傳子序列 sub 在切片
[start:end]中被找到的最低索引;若未找到則回傳-1。
- replace(old: bytes, new: bytes, count: int = -1) bytes¶
回傳將所有 old 出現處替換為 new 的副本。若提供 count,則僅替換前 count 次出現。
- rpartition(sep: bytes) tuple¶
在 sep 最後一次出現處分割,回傳
(head, sep, tail)。若未找到 sep,則回傳兩個空 bytes 物件後接 bytes。
- rsplit(sep: bytes | None = None, maxsplit: int = -1) list¶
在 sep 出現處分割為片段清單,從右側起最多進行 maxsplit 次分割。若 sep 為
None或省略,則以連續的 ASCII 空白分割。
- split(sep: bytes | None = None, maxsplit: int = -1) list¶
在 sep 出現處分割為片段清單。若 sep 為
None或省略,則以連續的 ASCII 空白分割,並忽略前導/尾隨空白。
- startswith(prefix: bytes, start: int = 0, end: int = -1) bool¶
若 bytes 以 prefix 開頭,則回傳
True。與 CPython 不同,prefix 不能是元組,且 end 雖被接受但會被忽略。
- classmethod(func: Callable[..., Any]) classmethod¶
將方法轉換為類別方法。通常用作裝飾器。
- compile(source: str | bytes, filename: str, mode: str) Any¶
將 source 編譯為可由
exec()或eval()執行的程式碼物件。mode 為"exec"、"eval"或"single"之一。
- class dict(*args, **kwargs)¶
建立新的字典。等同於 CPython 的
dict。- classmethod fromkeys(iterable: Iterable[Any], value: Any = None) dict¶
建立新的字典,其鍵取自 iterable,每個鍵皆對應到 value(預設為
None)。於型別上呼叫,例如dict.fromkeys(...)。
- get(key: Any, default: Any = None) Any¶
若 key 在字典中,則回傳其值,否則回傳 default(其本身預設為
None,因此這永遠不會引發KeyError)。字典不會被修改。
- pop(key: Any, default: Any = None) Any¶
從字典中移除 key 並回傳其值。若 key 不存在,當有提供 default 時回傳 default;否則引發
KeyError。若字典為固定,則引發TypeError。
- popitem() tuple¶
移除並回傳任意一個
(key, value)配對,以 2 元組形式回傳。對於一般的dict,所選配對未指定;對於OrderedDict,則移除最後插入的配對(LIFO)。若字典為空則引發KeyError,若其為固定則引發TypeError。
- setdefault(key: Any, default: Any = None) Any¶
若 key 在字典中,則回傳其值。否則以 default(預設為
None)值插入 key 並回傳該值。若字典為固定,則引發TypeError。
- update(*args: Any, **kwargs: Any) None¶
原地更新字典。最多接受一個位置引數:另一個字典,或由兩元素
(key, value)配對組成的可迭代物件(每個皆須產生恰好兩個項目,否則引發ValueError)。接著關鍵字引數會以字串鍵項目的形式加入。既有的鍵會被覆寫。若字典為固定,則引發TypeError。
- enumerate(iterable: Iterable[Any], start: int = 0) Iterator[tuple]¶
回傳一個 enumerate 物件,自 iterable 產生
(index, value)配對,索引由 start 開始。
- eval(expression: str | bytes, globals: dict | None = None, locals: dict | None = None) Any¶
對以字串(或編譯後的程式碼物件)形式提供的 Python 運算式求值並回傳結果。
- exec(object: str | bytes, globals: dict | None = None, locals: dict | None = None) None¶
動態執行以字串或編譯後的程式碼物件形式提供的 Python 程式碼。
- filter(function: Callable[[Any], Any] | None, iterable: Iterable[Any]) Iterator[Any]¶
由 iterable 中使 function 回傳真值的那些元素建構迭代器。若 function 為
None,則假設為恆等函式。
- class frozenset(iterable: Iterable[Any] = ())¶
回傳一個新的 frozenset 物件,元素可選擇性地取自 iterable。
frozenset是set的不可變、可雜湊變體。- difference(*others: Iterable[Any]) frozenset¶
回傳一個新的 frozenset,其元素為 frozenset 中不在任何 others 內的元素。每個引數皆可為任何可迭代物件。
- intersection(other: Iterable[Any]) frozenset¶
回傳一個新的 frozenset,其元素為 frozenset 與 other 的共同元素。在 MicroPython 中僅接受單一 other 引數(CPython 接受多個)。
- getattr(obj: Any, name: str, default: Any = None) Any¶
回傳 obj 的指定名稱屬性的值。若該屬性不存在,當有提供 default 時回傳 default,否則引發
AttributeError。
- class int(x: str | bytes | int | float = 0, base: int = 10)¶
- isinstance(obj: Any, classinfo: type | tuple) bool¶
若 obj 是 classinfo 或其任何子類別的實例,則回傳
True。classinfo 可以是類別或類別的元組。
- iter(obj: Any, sentinel: Any = None) Iterator[Any]¶
回傳一個迭代器物件。帶一個引數時,obj 必須支援迭代協定。帶兩個引數時,obj 必須可呼叫,且當其回傳 sentinel 時停止迭代。
- class list(iterable: Iterable[Any] = ())¶
建構新的清單,元素可選擇性地由 iterable 中的項目填入。
- extend(iterable: Iterable[Any]) None¶
將 iterable 中的所有項目附加到清單的尾端。若 iterable 本身為清單,則其項目會被直接複製;否則會對其進行迭代。
- index(value: Any, start: int = 0, stop: int = -1) int¶
回傳第一個等於 value 之元素的索引,於切片
[start:stop]中搜尋。若 value 不存在則引發ValueError。
- insert(index: int, object: Any) None¶
在位置 index 之前插入 object。負的 index 會被解讀為相對於清單尾端,且索引會被限制在有效範圍內(因此超出任一端的值會插入於開頭或結尾)。
- pop(index: int = -1) Any¶
移除並回傳位於 index 的項目(預設為最後一個項目)。若清單為空或 index 超出範圍,則引發
IndexError。
- remove(value: Any) None¶
移除第一個等於 value 的元素。若 value 不存在則引發
ValueError。
- map(function: Callable[..., Any], *iterables: Iterable[Any]) Iterator[Any]¶
回傳一個迭代器,將 function 套用於 iterables 的每個項目,產生其結果。
- max(*args: Any, key: Callable[[Any], Any] | None = None, default: Any = None) Any¶
帶單一可迭代引數時,回傳其最大項目。帶兩個或更多引數時,回傳最大的引數。
- class memoryview(obj: Any)¶
建立一個參照 obj 的 memoryview,obj 必須支援緩衝協定(例如
bytes、bytearray、array.array)。可對底層記憶體進行零複製存取與切片;對 memoryview 切片會回傳另一個 memoryview,而非副本。
- min(*args: Any, key: Callable[[Any], Any] | None = None, default: Any = None) Any¶
帶單一可迭代引數時,回傳其最小項目。帶兩個或更多引數時,回傳最小的引數。
- next(iterator: Iterator[Any], default: Any = None) Any¶
從 iterator 取出下一個項目。若提供 default 且迭代器已耗盡,則回傳 default 而非引發
StopIteration。
- class object¶
回傳一個沒有特性的新物件。
object是所有類別的基礎類別。
- pow(base: Any, exp: Any, mod: Any | None = None) Any¶
回傳 base 的 exp 次方。若提供 mod,則回傳
base ** exp % mod(其計算比顯式形式更有效率)。
- print(*objects: Any, sep: str = ' ', end: str = '\n', file: Any = None) None¶
將 objects 印出至文字串流 file,各項目以 sep 分隔,並以 end 結尾。
- property(fget: Callable[[Any], Any] | None = None, fset: Callable[[Any, Any], None] | None = None, fdel: Callable[[Any], None] | None = None, doc: str | None = None) property¶
回傳一個特性屬性。通常用作裝飾器,以在類別上定義受管理的屬性。
- range(*args: int) range¶
回傳一個不可變的整數序列。以
range(stop)、range(start, stop)或range(start, stop, step)形式呼叫。
- reversed(seq: Any) Iterator[Any]¶
回傳給定序列之值的反向迭代器。
- round(number: Any, ndigits: int | None = None) Any¶
回傳 number 四捨五入至 ndigits 位小數。若省略 ndigits,則回傳最接近的整數。
- class set(iterable: Iterable[Any] = ())¶
回傳一個新的 set 物件,元素可選擇性地取自 iterable。
- intersection(other: Iterable[Any]) set¶
回傳一個新的集合,其元素為集合與 other 的共同元素。在 MicroPython 中僅接受單一 other 引數(CPython 接受多個)。
- intersection_update(other: Iterable[Any]) None¶
更新集合,僅保留同時存在於 other 中的元素(原地)。在 MicroPython 中僅接受單一 other 引數。
- symmetric_difference(other: Iterable[Any]) set¶
回傳一個新的集合,其元素為僅在集合或 other 其中之一、而非兩者皆有的元素。在 MicroPython 中僅接受單一 other 引數。
- symmetric_difference_update(other: Iterable[Any]) None¶
更新集合,僅保留僅在集合或 other 其中之一、而非兩者皆有的元素(原地)。在 MicroPython 中僅接受單一 other 引數。
- class slice¶
slice 內建是 slice 物件所具有的型別。
- sorted(iterable: Iterable[Any], key: Callable[[Any], Any] | None = None, reverse: bool = False) list¶
由 iterable 中的項目回傳一個新的已排序清單。
- staticmethod(func: Callable[..., Any]) staticmethod¶
將方法轉換為靜態方法。通常用作裝飾器。
- class str(object: Any = '', encoding: str = 'utf-8', errors: str = 'strict')¶
回傳 object 的字串版本。若 object 為類 bytes 物件,則 encoding 與 errors 引數會控制解碼。
- center(width: int) str¶
回傳置中於長度為 width 之欄位中的字串副本,並以空格填補。在 MicroPython 中僅使用空格作為填補字元(沒有填補字元引數),且當 width 不大於其長度時,會原封不動回傳原字串。
- count(sub: str, start: int = 0, end: int = -1) int¶
回傳 sub 在切片
[start:end]中不重疊出現的次數。空的 sub 會計算字元之間的每個間隙。
- encode(encoding: str = 'utf-8', errors: str = 'strict') bytes¶
回傳一個編碼該字串的
bytes物件。MicroPython 實際上會忽略引數並使用 UTF-8;errors 雖被接受但不會生效。等同於bytes(s, "utf-8")。
- endswith(suffix: str | tuple, start: int = 0, end: int = -1) bool¶
若字串以給定的 suffix 結尾,則回傳
True,suffix 可以是單一字串或要嘗試的字串元組。可選的 start 與 end 會將比較限制在切片[start:end]。
- format(*args: Any, **kwargs: Any) str¶
執行字串格式化操作,以 args 與 kwargs 中的值替換由大括號
{}界定的替換欄位。支援標準的格式規格迷你語言。
- index(sub: str, start: int = 0, end: int = -1) int¶
與
find()類似,但當在切片[start:end]中未找到子字串 sub 時會引發ValueError。
- partition(sep: str) tuple¶
在 sep 第一次出現處分割字串並回傳 3 元組
(head, sep, tail)。若未找到 sep,則回傳(self, "", "")。空的 sep 會引發ValueError。
- replace(old: str, new: str, count: int = -1) str¶
回傳將所有子字串 old 出現處替換為 new 的字串副本。若提供非負的 count,則僅替換前 count 次出現。
- rindex(sub: str, start: int = 0, end: int = -1) int¶
與
rfind()類似,但當在切片[start:end]中未找到子字串 sub 時會引發ValueError。
- rpartition(sep: str) tuple¶
在 sep 最後一次出現處分割字串並回傳 3 元組
(head, sep, tail)。若未找到 sep,則回傳("", "", self)。空的 sep 會引發ValueError。
- rsplit(sep: str | None = None, maxsplit: int = -1) list¶
從右側將字串以 sep 作為分隔符分割為子字串清單,最多進行 maxsplit 次分割。當沒有 maxsplit(或為負值)時,其行為與
split()完全相同;在 MicroPython 中,帶非負 n 的rsplit(None, n)會引發NotImplementedError。
- split(sep: str | None = None, maxsplit: int = -1) list¶
將字串以 sep 作為分隔符分割為子字串清單,最多進行 maxsplit 次分割。若 sep 省略或為
None,則以連續的空白分割並忽略前導空白;否則空的 sep 會引發ValueError。
- startswith(prefix: str | tuple, start: int = 0, end: int = -1) bool¶
若字串以給定的 prefix 開頭,則回傳
True,prefix 可以是單一字串或要嘗試的字串元組。可選的 start 與 end 會將比較限制在切片[start:end]。
- sum(iterable: Iterable[Any], start: Any = 0) Any¶
由左至右將 start 與 iterable 的項目相加,並回傳總和。
- super(type: type | None = None, obj_or_type: Any | None = None) Any¶
回傳一個代理物件,將方法呼叫委派給 type 的父類別或同級類別。可用於存取在某類別中已被覆寫的繼承方法。
例外¶
- exception AssertionError¶
當
assert陳述式失敗時引發。
- exception AttributeError¶
當屬性參照或指派失敗時引發。
- exception Exception¶
所有非系統結束例外的共同基礎類別。
- exception ImportError¶
當
import陳述式找不到模組定義時引發。
- exception IndexError¶
當序列下標超出範圍時引發。
- exception KeyError¶
當映射(字典)鍵在既有的鍵集合中找不到時引發。
- exception MemoryError¶
當操作耗盡記憶體時引發。
- exception NameError¶
當找不到區域或全域名稱時引發。
- exception NotImplementedError¶
當呼叫抽象方法或未實作的功能時引發。
- exception OSError¶
當系統函式回傳系統相關錯誤時引發。
- exception RuntimeError¶
當偵測到不屬於其他任何類別的錯誤時引發。
- exception SyntaxError¶
當解析器遇到語法錯誤時引發。
- exception SystemExit¶
由
sys.exit()引發以請求解譯器終止。與大多數例外不同,當其未被捕捉時不會產生回溯資訊。在 OpenMV Cam 上,未處理的
SystemExit目前會導致 MicroPython 的 軟重置。
- exception TypeError¶
當操作或函式套用於型別不適當的物件時引發。
- exception ValueError¶
當內建操作或函式收到型別正確但值不適當的引數時引發。
- exception ZeroDivisionError¶
當除法或模運算的第二個引數為零時引發。