跳到内容

invalid-first-argument-name-for-method (N805) (方法无效的首个参数名称 (N805))

源自 pep8-naming linter。

有时提供修复。

作用

Checks for instance methods that use a name other than self for their first argument.(检查实例方法是否使用了除self之外的名称作为其第一个参数。)

为什么这不好?

PEP 8 recommends the use of self as first argument for all instance methods(PEP 8 建议所有实例方法都使用 self 作为第一个参数。)

Always use self for the first argument to instance methods.(始终使用 self 作为实例方法的第一个参数。)

If a function argument’s name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)(如果函数参数的名称与保留关键字冲突,通常最好附加一个尾随下划线,而不是使用缩写或拼写错误。因此,class_clss 更好。(也许更好的方法是通过使用同义词来避免此类冲突。)

Names can be excluded from this rule using the lint.pep8-naming.ignore-names or lint.pep8-naming.extend-ignore-names configuration options. For example, to allow the use of this as the first argument to instance methods, set the lint.pep8-naming.extend-ignore-names option to ["this"].(可以使用 lint.pep8-naming.ignore-nameslint.pep8-naming.extend-ignore-names 配置选项将名称从此规则中排除。例如,要允许使用 this 作为实例方法的第一个参数,请将 lint.pep8-naming.extend-ignore-names 选项设置为 ["this"]。)

示例

class Example:
    def function(cls, data): ...

建议改为

class Example:
    def function(self, data): ...

修复安全性

This rule's fix is marked as unsafe, as renaming a method parameter can change the behavior of the program.(此规则的修复被标记为不安全,因为重命名方法参数可能会更改程序的行为。)

Options (选项)