First parameter of a method is not named ‘self’¶
ID: py/not-named-self Kind: problem Security severity: Severity: recommendation Precision: very-high Tags: - maintainability - readability - convention - quality Query suites: - python-code-quality.qls - python-security-and-quality.qls Click to see the query in the CodeQL repository
Normal methods should have at least one parameter and the first parameter should be called self.
Recommendation¶
Ensure that the first parameter of a normal method is named self, as recommended by the style guidelines in PEP 8.
If a self parameter is unneeded, the method should be decorated with staticmethod, or moved out of the class as a regular function.
Example¶
In the following cases, the first argument of Point.__init__ is named val instead; whereas in Point2.__init__ it is correctly named self.
class Point: def __init__(val, x, y): # BAD: first parameter is mis-named 'val' val._x = x val._y = y class Point2: def __init__(self, x, y): # GOOD: first parameter is correctly named 'self' self._x = x self._y = y References¶
Python PEP 8: Function and method arguments.
Python Tutorial: Classes.
Python Docs: staticmethod.