Preface :
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. The function names are those used for special class methods; variants without leading and trailing __ are also provided for convenience.
Comparison operations :
The functions fall into categories that perform object comparisons, logical operations, mathematical operations, sequence operations, and abstract type tests. The object comparison functions are useful for all objects, and are named after the rich comparison operators they support :
operator.lt(a, b)/operator.__lt__(a, b)
operator.le(a, b)/operator.__le__(a, b)
operator.eq(a, b)/operator.__eq__(a, b)
operator.ne(a, b)/operator.__ne__(a, b)
operator.ge(a, b)/operator.__ge__(a, b)
operator.gt(a, b)/operator.__gt__(a, b)
Logical operations :
The logical operations are also generally applicable to all objects, and support truth tests, identity tests, and boolean operations :
- operator.not_(obj)/operator.__not__(obj)
更多 Logical operations 可以參考 這裡.
The mathematical and bitwise operations :
- operator.abs(obj)/operator.__abs__(obj)
- operator.add(a, b)/operator.__add__(a, b)
- operator.and_(a, b)/operator.__and__(a, b)
- operator.div(a, b)/operator.__div__(a, b)
- operator.floordiv(a, b)/operator.__floordiv__(a, b)
- operator.index(a)/operator.__index__(a)
- operator.invert(obj)/operator.__invert__(obj)
- operator.lshift(a, b)/operator.__lshift__(a, b)
- operator.mod(a, b)/operator.__mod__(a, b)
- operator.mul(a, b)/operator.__mul__(a, b)
- operator.neg(obj)/operator.__neg__(obj)
- operator.or_(a, b)/operator.__or__(a, b)
- operator.pos(obj)/operator.__pos__(obj)
- operator.pow(a, b)/operator.__pow__(a, b)
- operator.rshift(a, b)/operator.__rshift__(a, b)
- operator.sub(a, b)/operator.__sub__(a, b)
- operator.truediv(a, b)/operator.__truediv__(a, b)
Sequences operations :
Operations which work with sequences (some of them with mappings too) include :
- operator.concat(a, b)/operator.__concat__(a, b)
- operator.contains(a, b)/operator.__contains__(a, b)
- operator.getitem(a, b)/operator.__getitem__(a, b)
- operator.setitem(a, b, c)/operator.__setitem__(a, b, c)
In-Place Operations :
Many operations have an "in-place" version. The following functions provide a more primitive access to in-place operators than the usual syntax does; for example, thestatement x += y is equivalent to x = operator.iadd(x, y). Another way to put it like that z = operator.iadd(x, y) is equivalent to the compound statement z = x; z += y.
- operator.iadd(a, b)/operator.__iadd__(a, b)
更多 In-Place Operations 可以參考 這裡.
Predicate Functions :
The operator module also defines a few predicates to test the type of objects; however, these are not all reliable. It is preferable to test abstract base classes instead (see collections and numbers for details).
- operator.isCallable(obj)
- operator.isMappingType(obj)
- operator.isNumberType(obj)
- operator.isSequenceType(obj)
Fields Extraction :
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[, args...])
- operator.itemgetter(item[, args...])
- operator.methodcaller(name[, args...])
Mapping Operators to Functions :
This table shows how abstract operations correspond to operator symbols in the Python syntax and the functions in the operator module :
Supplement :
* [Python 學習筆記] 函式、類別與模組 : 類別 (特殊方法名稱)
* Python 3.1 快速導覽 - 位元運算
沒有留言:
張貼留言