Python3 operator module

In Python 2.x, usecmp()the function to compare the size relationship of two lists, numbers, strings, etc.

In Python 3.X, there is no longercmp()the function. If you need to implement comparison functionality, you need to importoperatorthe module, which is suitable for any object, and includes the following methods:

Methods contained in the operator module

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)

operator.lt(a, b)anda < bsame as,operator.le(a, b)anda <= bsame as,operator.eq(a, b)anda == bsame as,operator.ne(a, b)anda != bsame as,operator.gt(a, b)anda > bsame as,operator.ge(a, b)anda >= bsame as.

Example

# Import operator module
import operator
 
# Numbers
x = 10
y = 20

print("x:",x, ", y:",y)
print("operator.lt(x,y): ", operator.lt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.eq(x,x): ", operator.eq(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print("operator.le(x,y): ", operator.le(x,y))
print("operator.ge(y,x): ", operator.ge(y,x))
print()

# Strings
x = "Google"
y = "Example"

print("x:",x, ", y:",y)
print("operator.lt(x,y): ", operator.lt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.eq(x,x): ", operator.eq(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print("operator.le(x,y): ", operator.le(x,y))
print("operator.ge(y,x): ", operator.ge(y,x))
print()

# Check return value
print("type((operator.lt(x,y)): ", type(operator.lt(x,y)))

The output of the above code is:

x: 10 , y: 20
operator.lt(x,y):  True
operator.gt(y,x):  True
operator.eq(x,x):  True
operator.ne(y,y):  False
operator.le(x,y):  True
operator.ge(y,x):  True

x: Google , y: Example
operator.lt(x,y):  True
operator.gt(y,x):  True
operator.eq(x,x):  True
operator.ne(y,y):  False
operator.le(x,y):  True
operator.ge(y,x):  True

Compare two lists:

Example

# Import operator module
import operator

a = [1, 2]
b = [2, 3]
c = [2, 3]
print("operator.eq(a,b): ", operator.eq(a,b))
print("operator.eq(c,b): ", operator.eq(c,b))

The output of the above code is:

operator.eq(a,b):  False
operator.eq(c,b):  True

Operator functions

The operator module provides a set of high-efficiency functions corresponding to Python's built-in operators. For example,operator.add(x, y)with the expressionx+yis the same.

The types of functions include: comparison operations, logical operations, mathematical operations, and sequence operations on objects.

Object comparison functions apply to all objects, and function names are named according to their corresponding comparison operators.

Many function names are the same as special method names, just without double underscores. For backward compatibility, many functions with double underscores are also retained. For clarity, it is recommended to use functions without double underscores.

Example

# Python example
# add(), sub(), mul()
 
# Import operator module
import operator
 
# Initialize variables
a = 4
 
b = 3
 
# Use add() to add two values
print ("add() operation result:",end="");
print (operator.add(a, b))
 
# Use sub() to subtract two values
print ("sub() operation result:",end="");
print (operator.sub(a, b))
 
# Use mul() to multiply two values
print ("mul() operation result:",end="");
print (operator.mul(a, b))

The output of the above code is:

add() 运算结果 :7
sub() 运算结果 :1
mul() 运算结果 :12

Operation

Syntax

Function

Addition

a + b

add(a, b)

String 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 XOR

a ^ b

xor(a, b)

Bitwise NOT

~ 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)

Index assignment

obj[k] = v

setitem(obj, k, v)

Index deletion

del obj[k]

delitem(obj, k)

Index value

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)

Logical negation

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))

Slice value

seq[i:j]

getitem(seq, slice(i, j))

String formatting

s % obj

mod(s, obj)

Subtraction

a - b

sub(a, b)

Truth testing

obj

truth(obj)

Comparison

a < b

lt(a, b)

Comparison

a <= b

le(a, b)

Equality

a == b

eq(a, b)

Inequality

a != b

ne(a, b)

Comparison

a >= b

ge(a, b)

Comparison

a > b

gt(a, b)

Other extensions