self as First Argument
ID |
python.self_first_argument |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Naming Convention |
Language |
Python |
Tags |
code-style, naming |
Description
Reports instance methods whose first parameter is not named self. Python uses the first parameter as the "this" reference of the method, and PEP 8 establishes the convention self. A method named def foo(this, x) works at runtime, but every reader pays a moment of confusion to verify that nothing else broke.
class Bad:
def foo(this): # FLAW
return this
class Good:
def foo(self):
return self
@staticmethod
def helper(x): # OK — static, no implicit first arg
return x
Rationale
Convention is the whole point: a name that breaks the convention demands the reader’s attention every time, even when nothing else is wrong. self is also what every editor / tooling / docstring template assumes.
Remediation
Rename the parameter to self. The rest of the method body needs the same rename in lockstep, but every IDE handles that automatically.