operator --- 標準運算子替代函式¶
原始碼:Lib/operator.py
The operator module exports a set of efficient functions corresponding to
the intrinsic operators of Python. For example, operator.add(x, y) is
equivalent to the expression x+y. Many function names are those used for
special methods, without the double underscores. For backward compatibility,
many of these have a variant with the double underscores kept. The variants
without the double underscores are preferred for clarity.
函式種類有物件的比較運算、邏輯運算、數學運算以及序列運算。
物件比較函式適用於所有物件,函式根據它們對應的 rich comparison 運算子命名:
- operator.lt(a, b)¶
- operator.le(a, b)¶
- operator.eq(a, b)¶
- operator.ne(a, b)¶
- operator.ge(a, b)¶
- operator.gt(a, b)¶
- operator.__lt__(a, b)¶
- operator.__le__(a, b)¶
- operator.__eq__(a, b)¶
- operator.__ne__(a, b)¶
- operator.__ge__(a, b)¶
- operator.__gt__(a, b)¶
在 a 和 b 之間進行 "rich comparison"。具體來說,
lt(a, b)與a < b相同,le(a, b)與a <= b相同,eq(a, b)與a == b相同,ne(a, b)與a != b相同,gt(a, b)與a > b相同,ge(a, b)與a >= b相同。注意這些函式可以回傳任何值,無論它是否可當作 boolean(布林)值。關於 rich comparison 的更多資訊請參考 Comparisons。
邏輯運算通常也適用於所有物件,並且支援真值檢測、識別性測試和 boolean 運算:
- operator.not_(obj)¶
- operator.__not__(obj)¶
回傳
notobj 的結果。(請注意物件實例並沒有__not__()method(方法);只有直譯器核心定義此操作。結果會受__bool__()和__len__()method 影響。)
- operator.is_(a, b)¶
回傳
a is b。測試物件識別性。
- operator.is_not(a, b)¶
回傳
a is not b。測試物件識別性。
- operator.is_none(a)¶
回傳
a is None。測試物件識別性。在 3.14 版被加入.
- operator.is_not_none(a)¶
回傳
a is not None。測試物件識別性。在 3.14 版被加入.
數學和位元運算的種類是最多的:
- operator.index(a)¶
- operator.__index__(a)¶
回傳 a 轉換為整數的結果。等價於
a.__index__()。在 3.10 版的變更: 結果總是
int型別。在過去的版本中,結果可能為int子類別的實例。
- operator.inv(obj)¶
- operator.invert(obj)¶
- operator.__inv__(obj)¶
- operator.__invert__(obj)¶
回傳數字 obj 按位元取反 (inverse) 的結果。這等價於
~obj。
適用於序列的操作(其中一些也適用於對映 (mapping)),包括:
- operator.countOf(a, b)¶
回傳 b 在 a 中的出現次數。
- operator.indexOf(a, b)¶
回傳 b 在 a 中首次出現所在的索引。
- operator.length_hint(obj, default=0)¶
回傳物件 obj 的估計長度。首先嘗試回傳其實際長度,再使用
object.__length_hint__()得出估計值,最後才是回傳預設值。在 3.4 版被加入.
以下操作適用於可呼叫物件:
- operator.call(obj, /, *args, **kwargs)¶
- operator.__call__(obj, /, *args, **kwargs)¶
回傳
obj(*args, **kwargs)。在 3.11 版被加入.
The operator module also defines tools for generalized attribute and item
lookups. These are useful for making fast field extractors as arguments for
map(), sorted(), itertools.groupby(), or other functions that
expect a function argument.
- operator.attrgetter(attr)¶
- operator.attrgetter(*attrs)
回傳一個可從運算元中取得 attr 的可呼叫 (callable) 物件。如果請求了一個以上的屬性,則回傳一個包含屬性的 tupple(元組)。屬性名稱還可包含點號。例如:
在
f = attrgetter('name')之後,呼叫f(b)將回傳b.name。在
f = attrgetter('name', 'date')之後,呼叫f(b)將回傳(b.name, b.date)。在
f = attrgetter('name.first', 'name.last')之後,呼叫f(b)將回傳(b.name.first, b.name.last)。
等價於:
def attrgetter(*items): if any(not isinstance(item, str) for item in items): raise TypeError('屬性名稱必須是一個字串') if len(items) == 1: attr = items[0] def g(obj): return resolve_attr(obj, attr) else: def g(obj): return tuple(resolve_attr(obj, attr) for attr in items) return g def resolve_attr(obj, attr): for name in attr.split("."): obj = getattr(obj, name) return obj
- operator.itemgetter(item)¶
- operator.itemgetter(*items)
回傳一個使用運算元的
__getitem__()方法從運算元中取得 item 的可呼叫物件。如果指定了多個條目,則回傳一個查詢值的 tupple。例如:在
f = itemgetter(2)之後,呼叫f(r)將回傳r[2]。在
g = itemgetter(2, 5, 3)之後,呼叫g(r)將回傳(r[2], r[5], r[3])。
等價於:
def itemgetter(*items): if len(items) == 1: item = items[0] def g(obj): return obj[item] else: def g(obj): return tuple(obj[item] for item in items) return g
傳入的條目可以為運算元的
__getitem__()所接受的任何型別。dictionary(字典)接受任意可雜湊的值。list、tupple 和字串接受索引或切片:>>> itemgetter(1)('ABCDEFG') 'B' >>> itemgetter(1, 3, 5)('ABCDEFG') ('B', 'D', 'F') >>> itemgetter(slice(2, None))('ABCDEFG') 'CDEFG' >>> soldier = dict(rank='captain', name='dotterbart') >>> itemgetter('rank')(soldier) 'captain'
使用
itemgetter()從 tuple 中提取特定欄位的例子:>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] >>> getcount = itemgetter(1) >>> list(map(getcount, inventory)) [3, 2, 5, 1] >>> sorted(inventory, key=getcount) [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
- operator.methodcaller(name, /, *args, **kwargs)¶
回傳一個在運算元上呼叫 name method 的可呼叫物件。如果給定額外的引數和/或關鍵字引數,它們也將被傳給該 method。例如:
在
f = methodcaller('name')之後,呼叫f(b)將回傳b.name()。在
f = methodcaller('name', 'foo', bar=1)之後,呼叫f(b)將回傳b.name('foo', bar=1)。
等價於:
def methodcaller(name, /, *args, **kwargs): def caller(obj): return getattr(obj, name)(*args, **kwargs) return caller
運算子與函式間的對映¶
This table shows how abstract operations correspond to operator symbols in the
Python syntax and the functions in the operator module.
運算 |
語法 |
函式 |
|---|---|---|
加法 |
|
|
字串串接 |
|
|
包含性檢測 |
|
|
除法 |
|
|
除法 |
|
|
按位元與 (And) |
|
|
按位元互斥或 (Exclusive Or) |
|
|
按位元取反 (Inversion) |
|
|
按位元或 (Or) |
|
|
取冪 |
|
|
識別性 |
|
|
識別性 |
|
|
識別性 |
|
|
識別性 |
|
|
索引賦值 |
|
|
索引刪除 |
|
|
索引取值 |
|
|
左移 |
|
|
模除 (Modulo) |
|
|
乘法 |
|
|
矩陣乘法 |
|
|
反相(算術) |
|
|
反相(邏輯) |
|
|
正數 |
|
|
右移 |
|
|
切片賦值 |
|
|
切片刪除 |
|
|
切片取值 |
|
|
字串格式化 |
|
|
減法 |
|
|
真值檢測 |
|
|
比較大小 |
|
|
比較大小 |
|
|
相等性 |
|
|
不等性 |
|
|
比較大小 |
|
|
比較大小 |
|
|
原地 (in-place) 運算子¶
許多運算都有「原地」版本。以下列出的是提供對原地運算子(與一般語法相比)更底層存取的函式,例如 statement x += y 相當於 x = operator.iadd(x, y)。換一種方式來講就是 z = operator.iadd(x, y) 等價於複合陳述式 z = x; z += y。
在這些例子中,請注意當呼叫一個原地方法時,運算和賦值是分成兩個步驟來執行的。下面列出的原地函式只執行第一步,即呼叫原地方法,第二步賦值則不加處理。
對於不可變 (immutable) 的目標例如字串、數字和 tupple,更新的值會被計算,但不會被再被賦值給輸入變數:
>>> a = 'hello'
>>> iadd(a, ' world')
'hello world'
>>> a
'hello'
對於可變 (mutable) 的目標例如 list 和 dictionary,原地方法將執行更新,因此不需要後續賦值操作:
>>> s = ['h', 'e', 'l', 'l', 'o']
>>> iadd(s, [' ', 'w', 'o', 'r', 'l', 'd'])
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> s
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']