当前位置:首页>python>Python进阶教程:12_operator 模块 —— 新手完全指南

Python进阶教程:12_operator 模块 —— 新手完全指南

  • 2026-08-20 18:47:01
Python进阶教程:12_operator 模块 —— 新手完全指南

一、什么是 operator 模块?

1.1 一句话定义

operator 模块把 Python 的运算符(+-*>== 等)封装成了函数,让你可以把运算符"传来传去"。

1.2 为什么需要它?

# ============ 问题场景 ============# 你想写一个通用函数,接收"运算方式"作为参数# ❌ 没有 operator 模块时,你得用 if/elif 或 lambda:def calculate(a, b, operation):    if operation == "add":        return a + b    elif operation == "sub":        return a - b    elif operation == "mul":        return a * b    elif operation == "div":        return a / b# 或者用 lambda:def calculate2(a, b, op_func):    return op_func(a, b)calculate2(34lambda x, y: x + y)  # 7calculate2(34lambda x, y: x * y)  # 12# ✅ 有 operator 模块后,直接用现成函数:import operatordef calculate3(a, b, op_func):    return op_func(a, b)calculate3(34, operator.add)  # 7(加法)calculate3(34, operator.mul)  # 12(乘法)# 更简洁、更清晰、性能也更好!

1.3 生活比喻

没有 operator  你想让别人帮你"加"两个数 → 你得现场写一张纸条:"把这两个数加起来"  (相当于每次写 lambda x, y: x + y)有 operator  你直接递给别人一个"加法器" → 他拿来就用  (相当于直接传 operator.add

1.4 导入方式

# 方式1:导入整个模块import operatoroperator.add(34)  # 7# 方式2:导入特定函数from operator import add, mul, itemgetteradd(34)  # 7# 方式3:导入所有(不推荐,容易命名冲突)from operator import *

二、算术运算函数

2.1 基本四则运算

import operator# ============ 加法 ============print(operator.add(35))       # 8print(operator.add(35) == 3 + 5)  # True(等价于 3 + 5)print(operator.add("Hello, ""World!"))  # "Hello, World!"(字符串拼接)print(operator.add([12], [34]))       # [1, 2, 3, 4](列表拼接)# ============ 减法 ============print(operator.sub(103))      # 7print(operator.sub(103) == 10 - 3)  # True# ============ 乘法 ============print(operator.mul(45))       # 20print(operator.mul("Ha"3))    # "HaHaHa"(字符串重复)print(operator.mul([12], 3))  # [1, 2, 1, 2, 1, 2](列表重复)# ============ 除法 ============print(operator.truediv(103))  # 3.3333...(真除法,等价于 /)print(operator.floordiv(103)) # 3(整除,等价于 //)# ============ 取余 ============print(operator.mod(103))      # 1(等价于 10 % 3)# ============ 幂运算 ============print(operator.pow(210))      # 1024(等价于 2 ** 10)# ============ 取反(负号) ============print(operator.neg(5))          # -5(等价于 -5)print(operator.neg(-3))         # 3# ============ 绝对值 ============print(operator.abs(-7))         # 7(等价于 abs(-7))# ============ 正号(很少用) ============print(operator.pos(-5))         # -5(等价于 +(-5),不变)

2.2 完整对照表

operator 函数
等价表达式
示例
operator.add(a, b)a + badd(3, 5)
 → 8
operator.sub(a, b)a - bsub(10, 3)
 → 7
operator.mul(a, b)a * bmul(4, 5)
 → 20
operator.truediv(a, b)a / btruediv(10, 3)
 → 3.33
operator.floordiv(a, b)a // bfloordiv(10, 3)
 → 3
operator.mod(a, b)a % bmod(10, 3)
 → 1
operator.pow(a, b)a ** bpow(2, 10)
 → 1024
operator.neg(a)-aneg(5)
 → -5
operator.pos(a)+apos(-5)
 → -5
operator.abs(a)abs(a)abs(-7)
 → 7

2.3 原地运算(In-place)

import operator# 原地运算:修改对象本身(对可变对象有效)# 等价于 a += b, a -= b 等a = [123]b = [45]# operator.iadd 等价于 a += bresult = operator.iadd(a, b)print(result)  # [1, 2, 3, 4, 5]print(a)       # [1, 2, 3, 4, 5](a 本身被修改了!)print(result is a)  # True(返回的就是 a 本身)# 对比普通 add:a = [123]b = [45]result = operator.add(a, b)print(result)  # [1, 2, 3, 4, 5]print(a)       # [1, 2, 3](a 没变!add 创建了新列表)# 完整对照:# operator.iadd(a, b)  ↔  a += b# operator.isub(a, b)  ↔  a -= b# operator.imul(a, b)  ↔  a *= b# operator.itruediv(a, b) ↔ a /= b# operator.ifloordiv(a, b) ↔ a //= b# operator.imod(a, b)  ↔  a %= b# operator.ipow(a, b)  ↔  a **= b# 对不可变对象(int, str),iadd 和 add 效果一样x = 5y = operator.iadd(x, 3)  # 等价于 x += 3print(y)  # 8print(x)  # 5(int 不可变,x 没变)

2.4 矩阵运算(@ 运算符)

import operator# operator.matmul 等价于 @ 运算符(矩阵乘法,Python 3.5+)# 需要 numpy 等库支持import numpy as npA = np.array([[12], [34]])B = np.array([[56], [78]])# 以下两种写法等价:result1 = A @ Bresult2 = operator.matmul(A, B)print(result1)# [[19 22]#  [43 50]]# 原地版本operator.imatmul(A, B)  # A @= B

三、比较运算函数

3.1 基本比较

import operator# ============ 等于 ============print(operator.eq(33))       # True(等价于 3 == 3print(operator.eq(34))       # Falseprint(operator.eq("abc""abc"))  # True# ============ 不等于 ============print(operator.ne(34))       # True(等价于 3 != 4print(operator.ne(33))       # False# ============ 小于 ============print(operator.lt(35))       # True(等价于 3 < 5print(operator.lt(53))       # False# ============ 小于等于 ============print(operator.le(33))       # True(等价于 3 <= 3print(operator.le(35))       # Trueprint(operator.le(53))       # False# ============ 大于 ============print(operator.gt(53))       # True(等价于 5 > 3print(operator.gt(35))       # False# ============ 大于等于 ============print(operator.ge(55))       # True(等价于 5 >= 5print(operator.ge(53))       # Trueprint(operator.ge(35))       # False

3.2 完整对照表

operator 函数
等价表达式
示例
operator.eq(a, b)a == beq(3, 3)
 → True
operator.ne(a, b)a != bne(3, 4)
 → True
operator.lt(a, b)a < blt(3, 5)
 → True
operator.le(a, b)a <= ble(3, 3)
 → True
operator.gt(a, b)a > bgt(5, 3)
 → True
operator.ge(a, b)a >= bge(5, 5)
 → True

3.3 实际应用:自定义排序

import operator# 场景:按不同条件排序students = [    {"name""张三""age"20"score"92},    {"name""李四""age"22"score"85},    {"name""王五""age"21"score"98},    {"name""赵六""age"20"score"88},]# 按分数降序排列# 方法1:用 lambdaby_score_1 = sorted(students, key=lambda s: s["score"], reverse=True)# 方法2:用 operator.itemgetter(更简洁、更快)by_score_2 = sorted(students, key=operator.itemgetter("score"), reverse=True)for s in by_score_2:    print(f"  {s['name']}{s['score']}分")# 输出:#   王五: 98分#   张三: 92分#   赵六: 88分#   李四: 85分# 多条件排序:先按年龄升序,再按分数降序by_age_score = sorted(students, key=operator.itemgetter("age""score"))# 注意:itemgetter 多字段时,都是升序# 如果要一个升序一个降序,需要技巧:by_age_score = sorted(students, key=lambda s: (s["age"], -s["score"]))

四、逻辑运算函数

import operator# ============ 逻辑与 ============print(operator.and_(TrueTrue))    # True(等价于 True and True)print(operator.and_(TrueFalse))   # Falseprint(operator.and_(10))          # 0(按位与,对整数)# ⚠️ 注意:operator.and_ 对布尔值是逻辑与,对整数是按位与!# 末尾有下划线是因为 and 是 Python 关键字,不能做函数名# ============ 逻辑或 ============print(operator.or_(TrueFalse))    # True(等价于 True or False)print(operator.or_(FalseFalse))   # Falseprint(operator.or_(10))           # 1(按位或)# ============ 逻辑非 ============print(operator.not_(True))          # False(等价于 not True)print(operator.not_(False))         # Trueprint(operator.not_(0))             # Trueprint(operator.not_(""))            # True(空字符串是假值)print(operator.not_("hello"))       # False# ============ 异或 ============print(operator.xor(TrueFalse))    # True(不同为True)print(operator.xor(TrueTrue))     # False(相同为False)print(operator.xor(53))           # 6(按位异或:101 ^ 011 = 110)# ============ 真值测试 ============print(operator.truth(1))            # True(等价于 bool(1))print(operator.truth(0))            # Falseprint(operator.truth(""))           # Falseprint(operator.truth("hello"))      # Trueprint(operator.truth([]))           # Falseprint(operator.truth([1]))          # True# ============ 判断是否为假 ============# Python 3.x 中没有直接的 is_false,用 not_ 代替print(operator.not_(0))             # True(0 是假的)

4.1 完整对照表

operator 函数
等价表达式
说明
operator.and_(a, b)a and b
 / a & b
布尔→逻辑与;整数→按位与
operator.or_(a, b)a or b
 / a | b
布尔→逻辑或;整数→按位或
operator.not_(a)not a
逻辑非
operator.xor(a, b)a ^ b
异或
operator.truth(a)bool(a)
真值测试
operator.invert(a)~a
按位取反

📌 为什么有下划线?andornotis 是 Python 关键字,不能做函数名,所以加了下划线:and_or_not_is_


五、位运算函数

import operator# ============ 按位与 ============print(operator.and_(1210))    # 8# 12 = 1100# 10 = 1010# &  = 1000 = 8# ============ 按位或 ============print(operator.or_(1210))     # 14# 12 = 1100# 10 = 1010# |  = 1110 = 14# ============ 按位异或 ============print(operator.xor(1210))     # 6# 12 = 1100# 10 = 1010# ^  = 0110 = 6# ============ 按位取反 ============print(operator.invert(5))       # -6(等价于 ~5# 5 = 0000...0101# ~5 = 1111...1010 = -6(补码)# ============ 左移 ============print(operator.lshift(14))    # 16(等价于 1 << 4# 1 左移 4 位:0001 → 10000 = 16# ============ 右移 ============print(operator.rshift(164))   # 1(等价于 16 >> 4# 16 右移 4 位:10000 → 0001 = 1# ============ 完整对照 ============# operator.and_(a, b)   ↔  a & b# operator.or_(a, b)    ↔  a | b# operator.xor(a, b)    ↔  a ^ b# operator.invert(a)    ↔  ~a# operator.lshift(a, b) ↔  a << b# operator.rshift(a, b) ↔  a >> b# 原地版本:# operator.iand(a, b)   ↔  a &= b# operator.ior(a, b)    ↔  a |= b# operator.ixor(a, b)   ↔  a ^= b# operator.ilshift(a, b) ↔ a <<= b# operator.irshift(a, b) ↔ a >>= b

六、序列操作函数

6.1 concat —— 拼接

import operator# 等价于 + 运算符(用于序列)print(operator.concat("Hello, ""World!"))  # "Hello, World!"print(operator.concat([12], [3, 4]))       # [1, 2, 3, 4]print(operator.concat((12), (34)))       # (1234)# 等价于:print("Hello, " + "World!")   # 一样的结果print([12] + [3, 4])        # 一样的结果

6.2 contains —— 包含判断

import operator# 等价于 in 运算符# 注意参数顺序:contains(容器, 元素)print(operator.contains([123], 2))       # True(2 in [1,2,3])print(operator.contains([123], 5))       # Falseprint(operator.contains("hello world""world"))  # Trueprint(operator.contains({"a"1"b"2}, "a"))   # True(键存在)print(operator.contains({"a"1"b"2}, 1))     # False(值不算)# 等价于:print(2 in [123])          # Trueprint("world" in "hello world"# True

6.3 countOf —— 计数

import operator# 统计元素出现次数print(operator.countOf([123224], 2))  # 3print(operator.countOf("hello world""l"))       # 3print(operator.countOf("hello world""z"))       # 0# 等价于:print([123224].count(2))  # 3print("hello world".count("l"))       # 3

6.4 indexOf —— 查找索引

import operator# 返回第一次出现的索引(找不到报错)print(operator.indexOf([10203020], 20))  # 1(第一次出现在索引1print(operator.indexOf("hello""l"))            # 2# 找不到会报错:# operator.indexOf([1, 2, 3], 5)  # ❌ ValueError: 5 is not in list# 等价于:print([10203020].index(20))  # 1print("hello".index("l"))           # 2

6.5 getitem / setitem / delitem —— 下标操作

import operator# ============ getitem:获取元素 ============# 等价于 obj[key]my_list = [1020304050]print(operator.getitem(my_list, 0))     # 10(等价于 my_list[0])print(operator.getitem(my_list, -1))    # 50(等价于 my_list[-1])print(operator.getitem(my_list, slice(14)))  # [20, 30, 40](切片)my_dict = {"name""张三""age"25}print(operator.getitem(my_dict, "name"))  # "张三"(等价于 my_dict["name"])my_str = "Hello"print(operator.getitem(my_str, 1))       # "e"(等价于 my_str[1])# ============ setitem:设置元素 ============# 等价于 obj[key] = valuemy_list = [12345]operator.setitem(my_list, 0100)print(my_list)  # [100, 2, 3, 4, 5](等价于 my_list[0] = 100)operator.setitem(my_list, slice(13), [200300])print(my_list)  # [100, 200, 300, 4, 5](切片赋值)my_dict = {"a"1}operator.setitem(my_dict, "b"2)print(my_dict)  # {'a': 1, 'b': 2}(等价于 my_dict["b"] = 2)# ============ delitem:删除元素 ============# 等价于 del obj[key]my_list = [12345]operator.delitem(my_list, 0)print(my_list)  # [2, 3, 4, 5](等价于 del my_list[0])operator.delitem(my_list, slice(02))print(my_list)  # [4, 5](删除前两个)my_dict = {"a"1"b"2"c"3}operator.delitem(my_dict, "b")print(my_dict)  # {'a': 1, 'c': 3}(等价于 del my_dict["b"])

6.6 length_hint —— 长度估计

import operator# 返回对象的长度(或估计长度)print(operator.length_hint([123]))       # 3print(operator.length_hint("hello"))          # 5print(operator.length_hint(iter([123])))  # 3(迭代器的估计长度)# 主要用于内部优化(如 list 预分配内存)# 日常开发很少直接用

七、⭐ itemgetter —— 获取下标(最常用!)

7.1 基本用法

from operator import itemgetter# itemgetter(key) 返回一个函数,该函数接收一个对象,返回 obj[key]# ============ 获取列表元素 ============get_first = itemgetter(0)     # 创建一个"获取第0个元素"的函数get_last = itemgetter(-1)     # 创建一个"获取最后一个元素"的函数print(get_first([102030]))  # 10print(get_last([102030]))   # 30# 等价于:# get_first = lambda x: x[0]# get_last = lambda x: x[-1]# ============ 获取字典值 ============get_name = itemgetter("name")get_age = itemgetter("age")person = {"name""张三""age"25"city""北京"}print(get_name(person))  # "张三"print(get_age(person))   # 25# ============ 获取多个元素 ============get_first_and_last = itemgetter(0, -1)  # 同时获取第一个和最后一个print(get_first_and_last([1020304050]))  # (1050)(返回元组)get_name_age = itemgetter("name""age")print(get_name_age(person))  # ('张三', 25)

7.2 排序中的应用(最经典场景)

from operator import itemgetter# ============ 场景1:字典列表排序 ============students = [    {"name""张三""age"20"score"92},    {"name""李四""age"22"score"85},    {"name""王五""age"21"score"98},    {"name""赵六""age"20"score"88},]# 按分数排序by_score = sorted(students, key=itemgetter("score"), reverse=True)for s in by_score:    print(f"  {s['name']}{s['score']}分")# 王五: 98分# 张三: 92分# 赵六: 88分# 李四: 85分# 多条件排序:先按年龄,再按分数by_age_score = sorted(students, key=itemgetter("age""score"))for s in by_age_score:    print(f"  {s['name']}{s['age']}岁, {s['score']}分")# 张三: 20岁, 92分# 赵六: 20岁, 88分# 王五: 21岁, 98分# 李四: 22岁, 85分# ============ 场景2:元组列表排序 ============data = [(1"b"), (3"a"), (2"c"), (1"a")]# 按第一个元素排序print(sorted(data, key=itemgetter(0)))# [(1, 'b'), (1, 'a'), (2, 'c'), (3, 'a')]# 按第二个元素排序print(sorted(data, key=itemgetter(1)))# [(3, 'a'), (1, 'a'), (1, 'b'), (2, 'c')]# 先按第一个,再按第二个print(sorted(data, key=itemgetter(01)))# [(1, 'a'), (1, 'b'), (2, 'c'), (3, 'a')]# ============ 场景3:对比 lambda ============# 用 lambda:sorted(students, key=lambda s: s["score"])# 用 itemgetter(更快、更简洁):sorted(students, key=itemgetter("score"))# 性能对比(itemgetter 是 C 实现的,比 lambda 快)import timeitdata = [{"x": i} for i in range(1000)]# lambda 方式timeit.timeit(lambdasorted(data, key=lambda d: d["x"]), number=1000)# itemgetter 方式(通常快 20-30%)timeit.timeit(lambdasorted(data, key=itemgetter("x")), number=1000)

7.3 嵌套数据获取

from operator import itemgetter# 获取嵌套结构中的数据data = [    {"name""张三""scores": {"math"90"english"85}},    {"name""李四""scores": {"math"78"english"92}},]# 获取每个人的 scores 字典get_scores = itemgetter("scores")print(get_scores(data[0]))  # {'math': 90, 'english': 85}# 获取嵌套中的值需要组合使用get_math = lambda x: x["scores"]["math"]# 或者用 itemgetter 嵌套:# 先取 scores,再取 mathget_math_score = lambda x: itemgetter("math")(x["scores"])print(get_math_score(data[0]))  # 90print(get_math_score(data[1]))  # 78

八、⭐ attrgetter —— 获取属性(非常常用!)

8.1 基本用法

from operator import attrgetter# attrgetter("attr") 返回一个函数,该函数返回 obj.attrclass Student:    def __init__(self, name, age, score):        self.name = name        self.age = age        self.score = score    def __repr__(self):        return f"Student({self.name}{self.age}{self.score})"students = [    Student("张三"2092),    Student("李四"2285),    Student("王五"2198),    Student("赵六"2088),]# ============ 获取单个属性 ============get_name = attrgetter("name")get_score = attrgetter("score")print(get_name(students[0]))   # "张三"(等价于 students[0].name)print(get_score(students[0]))  # 92(等价于 students[0].score)# ============ 获取多个属性 ============get_name_score = attrgetter("name""score")print(get_name_score(students[0]))  # ('张三', 92)# ============ 排序 ============# 按分数降序by_score = sorted(students, key=attrgetter("score"), reverse=True)for s in by_score:    print(f"  {s.name}{s.score}分")# 王五: 98分# 张三: 92分# 赵六: 88分# 李四: 85分# 多条件:先按年龄,再按分数by_age_score = sorted(students, key=attrgetter("age""score"))

8.2 获取嵌套属性(用点号)

from operator import attrgetterclass Address:    def __init__(self, city, street):        self.city = city        self.street = streetclass Person:    def __init__(self, name, address):        self.name = name        self.address = addresspeople = [    Person("张三", Address("北京""长安街")),    Person("李四", Address("上海""南京路")),    Person("王五", Address("北京""王府井")),]# 获取嵌套属性:用 "." 连接get_city = attrgetter("address.city")get_street = attrgetter("address.street")print(get_city(people[0]))    # "北京"(等价于 people[0].address.city)print(get_street(people[1]))  # "南京路"# 按城市排序by_city = sorted(people, key=attrgetter("address.city"))for p in by_city:    print(f"  {p.name}{p.address.city}{p.address.street}")# 张三: 北京 长安街# 王五: 北京 王府井# 李四: 上海 南京路# 同时获取多个嵌套属性get_info = attrgetter("name""address.city")print(get_info(people[0]))  # ('张三', '北京')

8.3 attrgetter vs lambda 对比

from operator import attrgetterclass Point:    def __init__(self, x, y):        self.x = x        self.y = ypoints = [Point(34), Point(12), Point(50)]# 用 lambda:sorted(points, key=lambda p: p.x)# 用 attrgetter(更简洁、更快):sorted(points, key=attrgetter("x"))# 嵌套属性时优势更明显:# lambda:  key=lambda p: p.address.city# attrgetter: key=attrgetter("address.city")

九、⭐ methodcaller —— 调用方法

9.1 基本用法

from operator import methodcaller# methodcaller("method_name", *args, **kwargs)# 返回一个函数,该函数调用 obj.method_name(*args, **kwargs)# ============ 调用字符串方法 ============to_upper = methodcaller("upper")to_lower = methodcaller("lower")to_strip = methodcaller("strip")print(to_upper("hello"))     # "HELLO"(等价于 "hello".upper())print(to_lower("HELLO"))     # "hello"print(to_strip("  hi  "))    # "hi"# ============ 带参数的方法调用 ============# str.replace(old, new)replace_spaces = methodcaller("replace"" ""_")print(replace_spaces("hello world foo"))  # "hello_world_foo"# str.split(sep)split_by_comma = methodcaller("split"",")print(split_by_comma("a,b,c,d"))  # ['a', 'b', 'c', 'd']# str.startswith(prefix)starts_with_http = methodcaller("startswith""http")print(starts_with_http("https://example.com"))  # Trueprint(starts_with_http("ftp://example.com"))    # False# ============ 列表方法 ============append_99 = methodcaller("append"99)my_list = [123]append_99(my_list)print(my_list)  # [1, 2, 3, 99]# ============ 排序方法 ============do_sort = methodcaller("sort")my_list = [31415]do_sort(my_list)print(my_list)  # [1, 1, 3, 4, 5]

9.2 实际应用:批量处理方法

from operator import methodcaller# 场景:批量处理字符串列表words = ["  Hello  ""  World  ""  Python  "]# 方法1:列表推导 + 方法调用cleaned_1 = [w.strip().lower() for w in words]# 方法2:用 methodcaller + mapstrip_lower = methodcaller("strip")# 注意:methodcaller 一次只能调一个方法,需要组合cleaned_2 = list(map(methodcaller("lower"), map(methodcaller("strip"), words)))print(cleaned_2)  # ['hello', 'world', 'python']# 场景:对对象列表调用方法class Document:    def __init__(self, title, content):        self.title = title        self.content = content    def summarize(self, max_len=50):        return self.content[:max_len] + "..."    def word_count(self):        return len(self.content.split())docs = [    Document("文章1""Python 是一门优雅的编程语言,它的设计哲学强调代码的可读性。"),    Document("文章2""机器学习是人工智能的一个重要分支领域。"),]# 批量调用 word_count 方法get_word_count = methodcaller("word_count")counts = list(map(get_word_count, docs))print(counts)  # [25, 15](大约)# 批量调用 summarize 方法(带参数)get_summary = methodcaller("summarize"20)summaries = list(map(get_summary, docs))for s in summaries:    print(f"  {s}")

十、is_ 和 is_not —— 身份判断

import operator# ============ is_:判断是否为同一对象 ============a = [123]b = ac = [123]print(operator.is_(a, b))   # True(a 和 b 是同一个对象)print(operator.is_(a, c))   # False(内容相同,但不是同一个对象)# 等价于:print(a is b)   # Trueprint(a is c)   # False# ============ is_not:判断是否不是同一对象 ============print(operator.is_not(a, c))  # Trueprint(operator.is_not(a, b))  # False# 等价于:print(a is not c)  # Trueprint(a is not b)  # False# ============ 常见用途:过滤 None ============data = [1None3None5None7]# 用 lambda:cleaned_1 = list(filter(lambda x: x is not None, data))# 用 operator(需要 functools.partial 或其他方式):# 实际上这个场景 lambda 更直观print(cleaned_1)  # [1, 3, 5, 7]

十一、index 和 length_hint

import operator# ============ index:转为整数索引 ============# 等价于 __index__(),用于需要整数的场景print(operator.index(5))        # 5print(operator.index(True))     # 1(布尔值转整数)print(operator.index(False))    # 0# 用于切片等需要整数的地方my_list = [0123456789]start = operator.index(2)end = operator.index(7)print(my_list[start:end])  # [2, 3, 4, 5, 6]# 对浮点数会报错:# operator.index(3.14)  # ❌ TypeError# ============ length_hint:长度估计 ============print(operator.length_hint([123]))       # 3print(operator.length_hint(iter(range(10))))  # 10

十二、综合实战案例

12.1 学生成绩管理系统

from operator import itemgetter, attrgetter, methodcallerimport functools# ============ 用字典存储 ============students = [    {"name""张三""age"20"scores": {"math"92"english"85"physics"78}},    {"name""李四""age"22"scores": {"math"88"english"92"physics"95}},    {"name""王五""age"21"scores": {"math"98"english"76"physics"82}},    {"name""赵六""age"20"scores": {"math"72"english"88"physics"90}},]# 1. 按数学成绩排序print("=== 数学成绩排名 ===")by_math = sorted(students, key=lambda s: s["scores"]["math"], reverse=True)for rank, s in enumerate(by_math, 1):    print(f"  第{rank}名:{s['name']}{s['scores']['math']}分)")# 2. 计算每个人的总分print("\n=== 总分排名 ===")def total_score(student):    return sum(student["scores"].values())by_total = sorted(students, key=total_score, reverse=True)for rank, s in enumerate(by_total, 1):    total = total_score(s)    print(f"  第{rank}名:{s['name']}(总分 {total})")# 3. 用 itemgetter 简化print("\n=== 按年龄排序 ===")by_age = sorted(students, key=itemgetter("age"))for s in by_age:    print(f"  {s['name']}{s['age']}岁")# ============ 用类存储 ============class Student:    def __init__(self, name, age, math, english, physics):        self.name = name        self.age = age        self.math = math        self.english = english        self.physics = physics    @property    def total(self):        return self.math + self.english + self.physics    @property    def average(self):        return round(self.total / 31)    def __repr__(self):        return f"{self.name}(总分{self.total})"students_obj = [    Student("张三"20928578),    Student("李四"22889295),    Student("王五"21987682),    Student("赵六"20728890),]# 用 attrgetter 排序print("\n=== 对象排序 ===")by_total = sorted(students_obj, key=attrgetter("total"), reverse=True)for s in by_total:    print(f"  {s.name}: 总分{s.total}, 平均{s.average}")# 多条件:先按年龄,再按总分by_age_total = sorted(students_obj, key=attrgetter("age""total"))

12.2 数据处理管道

from operator import itemgetter, methodcallerimport functools# 模拟 API 返回的数据api_response = [    {"id"1"title""  Python入门  ""tags""python,beginner,tutorial""views"1500},    {"id"2"title""  Flask实战  ""tags""flask,web,python""views"3200},    {"id"3"title""  数据分析  ""tags""pandas,numpy,data""views"800},    {"id"4"title""  Django部署  ""tags""django,web,deploy""views"2100},]# 处理管道:# 1. 提取标题 → 2. 去空格 → 3. 转小写 → 4. 排序# 用 methodcaller 构建处理步骤strip_title = methodcaller("strip")to_lower = methodcaller("lower")# 提取并处理标题titles = [itemgetter("title")(item) for item in api_response]print("原始标题:", titles)# ['  Python入门  ', '  Flask实战  ', '  数据分析  ', '  Django部署  ']cleaned = [to_lower(strip_title(t)) for t in titles]print("处理后:", cleaned)# ['python入门', 'flask实战', '数据分析', 'django部署']# 按 views 排序by_views = sorted(api_response, key=itemgetter("views"), reverse=True)print("\n按浏览量排序:")for item in by_views:    print(f"  {item['title'].strip()}{item['views']}次")# 提取 tags 并拆分get_tags = itemgetter("tags")split_comma = methodcaller("split"",")all_tags = []for item in api_response:    tags = split_comma(get_tags(item))    all_tags.extend(tags)print(f"\n所有标签:{all_tags}")print(f"标签数量:{len(all_tags)}")print(f"去重后:{sorted(set(all_tags))}")

12.3 自定义排序器工厂

from operator import itemgetter, attrgetterdef make_sorter(*keys, reverse=False):    """    创建一个排序函数    用法:sort_by_score = make_sorter("score", reverse=True)    """    getter = itemgetter(*keys) if len(keys) > 1 else itemgetter(keys[0])    def sort_func(data_list):        return sorted(data_list, key=getter, reverse=reverse)    return sort_func# 创建不同的排序器sort_by_score = make_sorter("score", reverse=True)sort_by_age = make_sorter("age")sort_by_age_score = make_sorter("age""score")# 使用data = [    {"name""A""age"22"score"85},    {"name""B""age"20"score"92},    {"name""C""age"20"score"88},]print("按分数降序:")for item in sort_by_score(data):    print(f"  {item['name']}{item['score']}")print("\n按年龄+分数:")for item in sort_by_age_score(data):    print(f"  {item['name']}{item['age']}岁, {item['score']}分")

12.4 用 operator 实现函数式编程

from operator import add, mul, sub, itemgetterfrom functools import reduce, partialimport itertools# ============ reduce 累加 ============numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# 求和total = reduce(add, numbers)print(f"求和:{total}")  # 55# 求积product = reduce(mul, numbers)print(f"求积:{product}")  # 3628800# 找最大值maximum = reduce(lambda a, b: a if a > b else b, numbers)# 或者直接用 max(numbers)# ============ partial 偏函数 ============# 固定一个参数,创建新函数# 创建一个"加10"的函数add_10 = partial(add, 10)print(add_10(5))   # 15print(add_10(20))  # 30# 创建一个"乘以2"的函数double = partial(mul, 2)print(double(7))   # 14# 批量应用numbers = [1, 2, 3, 4, 5]doubled = list(map(double, numbers))print(doubled)  # [2, 4, 6, 8, 10]added = list(map(add_10, numbers))print(added)  # [11, 12, 13, 14, 15]# ============ accumulate 累积 ============# itertools.accumulate 默认用加法print(list(itertools.accumulate([12345])))# [1, 3, 6, 10, 15](累积和)# 用乘法print(list(itertools.accumulate([12345], mul)))# [1, 2, 6, 24, 120](累积积,即阶乘)# 用自定义函数print(list(itertools.accumulate([12345], lambda a, b: a + b * 2)))# [1, 5, 11, 19, 29]

十三、operator 模块完整函数列表

13.1 按类别分组

═══════════════════════════════════════════════════════  算术运算═══════════════════════════════════════════════════════  add(ab)        a + b  sub(ab)        a - b  mul(ab)        a * b  truediv(ab)    a / b  floordiv(ab)   a // b  mod(ab)        a % b  pow(ab)        a ** b  matmul(ab)     a @ b  neg(a)           -a  pos(a)           +a  abs(a)           abs(a)  原地版本:iadd, isub, imul, itruediv, ifloordiv, imod, ipow, imatmul═══════════════════════════════════════════════════════  比较运算═══════════════════════════════════════════════════════  eq(a, b)         a == b  ne(a, b)         a != b  lt(a, b)         a < b  le(a, b)         a <= b  gt(a, b)         a > b  ge(a, b)         a >= b═══════════════════════════════════════════════════════  逻辑/位运算═══════════════════════════════════════════════════════  and_(a, b)       a and b / a & b  or_(a, b)        a or b / a | b  not_(a)          not a  xor(a, b)        a ^ b  invert(a)        ~a  lshift(a, b)     a << b  rshift(a, b)     a >> b  truth(a)         bool(a)  原地版本:iand, ior, ixor, ilshift, irshift═══════════════════════════════════════════════════════  身份判断═══════════════════════════════════════════════════════  is_(a, b)        a is b  is_not(a, b)     a is not b═══════════════════════════════════════════════════════  序列操作═══════════════════════════════════════════════════════  concat(a, b)     a + b(序列拼接)  contains(a, b)   b in a  countOf(a, b)    a.count(b)  indexOf(a, b)    a.index(b)  getitem(a, b)    a[b]  setitem(a, b, c) a[b] = c  delitem(a, b)    del a[b]  length_hint(obj) len(obj) 的估计═══════════════════════════════════════════════════════  属性/下标/方法获取器(⭐最常用)═══════════════════════════════════════════════════════  itemgetter(key)       返回 obj[key] 的函数  attrgetter(attr)      返回 obj.attr 的函数  methodcaller(name)    返回 obj.name() 的函数═══════════════════════════════════════════════════════  其他═══════════════════════════════════════════════════════  index(a)         a.__index__()

十四、性能对比

import operatorimport timeit# 测试数据data = [{"x": i, "y": i * 2for i in range(10000)]# ============ 排序性能对比 ============# 方式1:lambdatime_lambda = timeit.timeit(    lambdasorted(data, key=lambda d: d["x"]),    number=100)# 方式2:itemgettertime_itemgetter = timeit.timeit(    lambdasorted(data, key=operator.itemgetter("x")),    number=100)print(f"lambda:     {time_lambda:.4f}s")print(f"itemgetter: {time_itemgetter:.4f}s")print(f"加速比:     {time_lambda/time_itemgetter:.2f}x")# 通常 itemgetter 快 20-40%# ============ 运算性能对比 ============import randompairs = [(random.randint(1100), random.randint(1100)) for _ in range(100000)]# lambda 方式time_lambda_add = timeit.timeit(    lambda: [a + b for a, b in pairs],    number=10)# operator 方式time_operator_add = timeit.timeit(    lambdalist(map(operator.add, *zip(*pairs))),    number=10)# 对于简单运算,差距不大# 对于排序的 key 函数,itemgetter/attrgetter 优势明显

十五、常见使用场景总结

场景
用法
示例
字典列表排序
itemgetter("key")sorted(data, key=itemgetter("score"))
对象列表排序
attrgetter("attr")sorted(objs, key=attrgetter("age"))
多条件排序
itemgetter("a", "b")sorted(data, key=itemgetter("age", "score"))
嵌套属性
attrgetter("a.b.c")sorted(objs, key=attrgetter("addr.city"))
批量调用方法
methodcaller("upper")list(map(methodcaller("strip"), texts))
函数式累加
reduce(add, list)reduce(add, [1,2,3,4,5])
 → 15
偏函数
partial(add, 10)add_10 = partial(add, 10)
作为回调
传递运算函数
apply_operation(a, b, operator.mul)

十六、学习路径建议

1天:理解为什么需要 operator(对比 lambda)2天:掌握 itemgetter(排序中最常用)3天:掌握 attrgetter(对象排序)4天:掌握 methodcaller(批量方法调用)5天:了解算术/比较/逻辑运算函数6天:结合 functools(reduce, partial)做函数式编程7天:在实际项目中应用(排序、数据处理)

十七、一句话总结

operator 模块 = 把运算符变成函数,让你可以把"操作"作为参数传递。

最常用的三个:

  • itemgetter
     → 取字典/列表的值(排序必备)
  • attrgetter
     → 取对象的属性(排序必备)
  • methodcaller
     → 调用对象的方法(批量处理)

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-08-21 13:38:28 HTTP/2.0 GET : https://f.mffb.com.cn/a/510961.html
  2. 运行时间 : 0.507125s [ 吞吐率:1.97req/s ] 内存消耗:4,872.14kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=2faa14c526eb75e3aa9c883c4746399b
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.001022s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.001488s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000750s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000718s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.001255s ]
  6. SELECT * FROM `set` [ RunTime:0.000708s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.001515s ]
  8. SELECT * FROM `article` WHERE `id` = 510961 LIMIT 1 [ RunTime:0.002813s ]
  9. UPDATE `article` SET `lasttime` = 1787290708 WHERE `id` = 510961 [ RunTime:0.056857s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000863s ]
  11. SELECT * FROM `article` WHERE `id` < 510961 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.001443s ]
  12. SELECT * FROM `article` WHERE `id` > 510961 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.002996s ]
  13. SELECT * FROM `article` WHERE `id` < 510961 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.030639s ]
  14. SELECT * FROM `article` WHERE `id` < 510961 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.046139s ]
  15. SELECT * FROM `article` WHERE `id` < 510961 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.185647s ]
0.510754s