跳到内容

参数过多 (PLR0913)

源自 Pylint 代码检查工具。

作用

检查包含过多参数的函数定义。

默认情况下,此规则允许最多五个参数,这由 lint.pylint.max-args 选项配置。

为什么这不好?

具有许多参数的函数更难理解、维护和调用。 考虑将具有许多参数的函数重构为具有较少参数的较小函数,或使用对象对相关参数进行分组。

示例

def calculate_position(x_pos, y_pos, z_pos, x_vel, y_vel, z_vel, time):
    new_x = x_pos + x_vel * time
    new_y = y_pos + y_vel * time
    new_z = z_pos + z_vel * time
    return new_x, new_y, new_z

建议改为

from typing import NamedTuple


class Vector(NamedTuple):
    x: float
    y: float
    z: float


def calculate_position(pos: Vector, vel: Vector, time: float) -> Vector:
    return Vector(*(p + v * time for p, v in zip(pos, vel)))

Options (选项)