跳到内容

bad-str-strip-call (PLE1310)

源自 Pylint 代码检查工具。

作用

检查 str.strip 调用中重复的字符。

为什么这不好?

str.strip 调用中的所有字符都会从字符串的前导和尾随端移除。在调用中包含重复的字符是多余的,并且通常表明存在错误。

在 Python 3.9 及更高版本中,您可以使用 str.removeprefixstr.removesuffix 分别从字符串中删除精确的前缀或后缀,如果可能,应优先使用它们。

示例

# Evaluates to "foo".
"bar foo baz".strip("bar baz ")

建议改为

# Evaluates to "foo".
"bar foo baz".strip("abrz ")  # "foo"

或者

# Evaluates to "foo".
"bar foo baz".removeprefix("bar ").removesuffix(" baz")

Options (选项)

参考