Python官方文檔地址:https://docs.python.org/3.6/library/operator.html?highlight=operator
Operator提供的函可用於對象比較,邏輯運算,數學運算和序列運算的類別。
簡單介紹幾個常用的函數,其他的可參考官方文檔。
operator.lt(a,b)相當於a < b **operator.__lt__(a,b)** operator.le(a,b )相當於a <= b **operator.__le__(a,b)** operator.eq(a,b)相當於a == b **operator.__eq__(a,b)** operator.ne(a,b)相當於a != b **operator.__ne__(a,b)** operator.ge(a,b)相當於a > b **operator.__ge__(a,b)** operator.gt(a,b)相當於a >= b **operator.__gt__(a,b)** 注:這些函數可以返回任何值,這些值可能會或可能不會被解釋為布爾值。 operator.concat(a, b) 對於 a、b序列,返回 a + b(列表合並) **operator.__concat__(a, b)** operator.countOf(a, b) 返回 b 在 a 中出現的次數 perator.delitem(a, b) 刪除 a 中索引為 b 的值 **operator.__delitem__(a, b)** operator.getitem(a, b) 返回 a 中索引為 b 的值 **operator.__getitem__(a, b)** operator.indexOf(a, b) 返回 b 在 a 中首次出現位置的索引值。 operator.setitem(a, b, c) 設置 a 中索引值為 b 的項目值更改為 c **operator.__setitem__(a, b, c)** operator 模塊也為屬性和項目的查找提供了一些工具。這些工具使得 map(), sorted(), itertools.groupby() 或其他函數 需要的參數的提取更方便更快速。上面的函數有一個共同點,即均接受函數參數。 operator.attrgetter(attr) operator.attrgetter(*attrs) 返回一個可調用的對象,該對象從運算中獲取 'attr' 。如果請求的屬性不止一個的話, 返回屬性的元組。這些屬性的名字可以包括 '.'。 比如: 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) 相當於:operator.itemgetter(item) operator.itemgetter(*items) 返回一個可調用的對象,該對象通過運算符的 __getitem__()的方法 從運算中獲取 item 。如果指定了多個 item , 返回查找值的元組。 比如: f = itemgetter(2), 調用 f(r) 返回 r[2] g = itemgetter(2, 5, 3), 調用 f(r) 返回 (r[2], r[3], r[3]) 相當於def attrgetter(*items): if any(not isinstance(item, str) for item in items): raise TypeError('attribute name must be a string') 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運算符的 __getitem__()方法可接受任意類型的項目。字典接收任意的哈希值。列表、元組和字符串接收一個索引或字符片段。 >>> itemgetter(1)('ABCDEFG') 'B' >>> itemgetter(1,3,5)('ABCDEFG') ('B', 'D', 'F') >>> itemgetter(slice(2,None))('ABCDEFG') 'CDEFG' 使用 itemgetter() 從元組序列中獲取指定的域值,比如: >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] >>> getcount = itemgetter(1) >>> map(getcount, inventory) [3, 2, 5, 1] >>> sorted(inventory, key=getcount) [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]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
最后 operator 相關的信息對應如下
此表顯示抽象操作如何與Python語法中的運算符符號以及operator模塊中的函數相對應
Operation | Syntax | Function |
---|---|---|
Addition | a + b |
add(a, b) |
Concatenation | seq1 + seq2 |
concat(seq1, seq2) |
Containment Test | obj in seq |
contains(seq, obj) |
Division | a / b |
truediv(a, b) |
Division | a // b |
floordiv(a, b) |
Bitwise And | a & b |
and_(a, b) |
Bitwise Exclusive Or | a ^ b |
xor(a, b) |
Bitwise Inversion | ~ a |
invert(a) |
Bitwise Or | a | b |
or_(a, b) |
Exponentiation | a ** b |
pow(a, b) |
Identity | a is b |
is_(a, b) |
Identity | a is not b |
is_not(a, b) |
Indexed Assignment | obj[k] = v |
setitem(obj, k, v) |
Indexed Deletion | del obj[k] |
delitem(obj, k) |
Indexing | obj[k] |
getitem(obj, k) |
Left Shift | a << b |
lshift(a, b) |
Modulo | a % b |
mod(a, b) |
Multiplication | a * b |
mul(a, b) |
Matrix Multiplication | a @ b |
matmul(a, b) |
Negation (Arithmetic) | - a |
neg(a) |
Negation (Logical) | not a |
not_(a) |
Positive | + a |
pos(a) |
Right Shift | a >> b |
rshift(a, b) |
Slice Assignment | seq[i:j] = values |
setitem(seq, slice(i, j), values) |
Slice Deletion | del seq[i:j] |
delitem(seq, slice(i, j)) |
Slicing | seq[i:j] |
getitem(seq, slice(i, j)) |
String Formatting | s % obj |
mod(s, obj) |
Subtraction | a - b |
sub(a, b) |
Truth Test | obj |
truth(obj) |
Ordering | a < b |
lt(a, b) |
Ordering | a <= b |
le(a, b) |
Equality | a == b |
eq(a, b) |
Difference | a != b |
ne(a, b) |
Ordering | a >= b |
ge(a, b) |
Ordering | a > b |
gt(a, b) |