Python3 operator模块详解
一个让操作符变成函数的实用工具库
operator模块是什么?
operator模块是Python的标准库之一,它提供了一系列与Python内置运算符对应的函数。
为什么需要operator模块?
在Python中,很多操作符(+, –, *, >等)是不能直接当作函数参数传递的。operator模块把这些操作符”包装”成了函数,这样我们就可以像使用普通函数一样使用这些操作符了。
result = 10 + 5
# 使用operator模块
import operator
result = operator.add(10, 5)
算术运算符函数
基本算术运算
operator.add(a, b) – 相当于 a + b
operator.sub(a, b) – 相当于 a – b
operator.mul(a, b) – 相当于 a * b
operator.truediv(a, b) – 相当于 a / b (真除法)
operator.floordiv(a, b) – 相当于 a // b (地板除法)
operator.mod(a, b) – 相当于 a % b (取模)
# 加法示例
print(op.add(10, 3)) # 输出: 13
# 除法示例
print(op.truediv(10, 4)) # 输出: 2.5
print(op.floordiv(10, 4)) # 输出: 2
其他算术运算
operator.pow(a, b) – 相当于 a ** b (幂运算)
operator.abs(a) – 相当于 abs(a) (绝对值)
operator.neg(a) – 相当于 -a (取负数)
operator.pos(a) – 相当于 +a (取正数)
比较运算符函数
这些函数比较两个值,返回布尔值(True或False)
标准的比较运算
operator.lt(a, b) – 相当于 a < b (小于)
operator.le(a, b) – 相当于 a <= b (小于等于)
operator.eq(a, b) – 相当于 a == b (等于)
operator.ne(a, b) – 相当于 a != b (不等于)
operator.gt(a, b) – 相当于 a > b (大于)
operator.ge(a, b) – 相当于 a >= b (大于等于)
# 比较示例
print(op.lt(5, 10)) # 输出: True
print(op.eq(‘hello’, ‘world’)) # 输出: False
print(op.ge(8.5, 8.5)) # 输出: True
逻辑运算符函数
基本逻辑运算
operator.not_(a) – 相当于 not a (逻辑非)
operator.and_(a, b) – 相当于 a and b (逻辑与)
operator.or_(a, b) – 相当于 a or b (逻辑或)
# 逻辑运算示例
print(op.not_(False)) # 输出: True
print(op.and_(True, False)) # 输出: False
print(op.or_(True, False)) # 输出: True
真值测试
operator.truth(a) – 相当于 bool(a) (转换为布尔值)
operator.is_(a, b) – 相当于 a is b (对象标识比较)
operator.is_not(a, b) – 相当于 a is not b
lst = [1, 2, 3]
lst2 = lst
print(op.is_(lst, lst2)) # 输出: True
print(op.is_not(lst, [1, 2, 3])) # 输出: True
print(op.truth(0)) # 输出: False
序列操作函数
这些函数用于操作序列(列表、元组、字符串等)
索引和切片
operator.getitem(a, b) – 相当于 a[b] (获取元素)
operator.setitem(a, b, c) – 相当于 a[b] = c (设置元素)
operator.delitem(a, b) – 相当于 del a[b] (删除元素)
my_list = [1, 2, 3, 4, 5]
# 获取索引为2的元素
print(op.getitem(my_list, 2)) # 输出: 3
# 设置元素
op.setitem(my_list, 1, 99)
print(my_list) # 输出: [1, 99, 3, 4, 5]
# 删除元素
op.delitem(my_list, 0)
print(my_list) # 输出: [99, 3, 4, 5]
其他序列操作
operator.concat(a, b) – 相当于 a + b (序列连接)
operator.contains(a, b) – 相当于 b in a (成员测试)
operator.countOf(a, b) – 返回 b 在 a 中出现的次数
operator.indexOf(a, b) – 返回 b 在 a 中首次出现的索引
属性操作函数
这些函数用于处理对象的属性和方法
属性访问
operator.attrgetter(attr) – 创建一个获取对象属性的函数
operator.itemgetter(item) – 创建一个获取序列元素或字典值的函数
operator.methodcaller(name, …) – 创建一个调用对象方法的函数
from collections import namedtuple
# 定义一个简单的类
Person = namedtuple(‘Person’, [‘name’, ‘age’, ‘job’])
p = Person(‘Alice’, 30, ‘engineer’)
# attrgetter示例
get_name = op.attrgetter(‘name’)
print(get_name(p)) # 输出: ‘Alice’
# itemgetter示例
data = [(‘apple’, 3), (‘banana’, 2), (‘cherry’, 5)]
get_second = op.itemgetter(1)
print(sorted(data, key=get_second)) # 按第二元素排序
operator模块的使用场景
operator模块在以下场景特别有用:
- 函数式编程:当需要将操作符作为参数传递给高阶函数时(如map、filter、reduce)
- 排序操作:在sorted()或list.sort()中自定义排序规则
- 动态访问:当需要根据字符串名称动态访问对象属性或序列元素时
- 性能优化:operator函数通常比等效的lambda表达式执行更快
- 代码简洁:避免编写琐碎的lambda函数,使代码更简洁清晰
from operator import itemgetter
students = [
(‘Alice’, ‘B’, 22),
(‘Bob’, ‘A’, 19),
(‘Charlie’, ‘B’, 20),
(‘Dave’, ‘A’, 21)
]
# 先按班级(索引1)排序,再按年龄(索引2)排序
sorted_students = sorted(students, key=itemgetter(1, 2))
for s in sorted_students:
print(s)