Python 内建函数列表 > Python 的内置函数 next

Python 的内置函数 next() 是一个用于迭代器协议的重要函数,它能够从迭代器中获取下一个元素。next() 函数的基本语法如下:

next(iterator[, default])

其中:

  • iterator 是一个可迭代对象(必须实现了 __next__() 方法的迭代器)
  • default 是可选参数,当迭代器耗尽时返回该默认值,若不提供默认值且迭代器耗尽则会抛出 StopIteration 异常

使用示例:

  1. 基本用法:
    numbers = iter([1, 2, 3])
    print(next(numbers))  # 输出:1
    print(next(numbers))  # 输出:2
    print(next(numbers))  # 输出:3
    print(next(numbers))  # 引发 StopIteration
    
  2. 使用默认值:
    numbers = iter([1, 2])
    print(next(numbers, 'end'))  # 输出:1
    print(next(numbers, 'end'))  # 输出:2
    print(next(numbers, 'end'))  # 输出:end
    

应用场景:

  • 手动控制迭代过程
  • 处理大型数据集时按需获取数据
  • 实现自定义的迭代行为
  • iter() 函数配合使用来创建和操作迭代器

注意事项:

  1. 对普通序列(如列表、元组)直接使用 next() 会引发 TypeError,需要先用 iter() 转换为迭代器
  2. 当迭代器耗尽时,如果不提供默认值则会抛出 StopIteration 异常
  3. 在 for 循环中,Python 会自动处理 StopIteration,但直接使用 next() 时需要手动处理

高级用法示例:

with open('data.txt') as f:
    header = next(f)  # 获取文件第一行
    for line in f:    # 继续读取剩余内容
        process(line)

在 Python 3 中,next() 实际上是调用迭代器的 __next__() 方法,这是 Python 迭代器协议的核心部分。