First parameter of a method is not named ‘self’¶
ID: py/not-named-selfKind: problemSecurity severity: Severity: recommendationPrecision: very-highTags: - maintainability - readability - convention - qualityQuery suites: - 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 calledself.
Recommendation¶
Ensure that the first parameter of a normal method is namedself, as recommended by the style guidelines in PEP 8.
If aself parameter is unneeded, the method should be decorated withstaticmethod, or moved out of the class as a regular function.
Example¶
In the following cases, the first argument ofPoint.__init__ is namedval instead; whereas inPoint2.__init__ it is correctly namedself.
classPoint:def__init__(val,x,y):# BAD: first parameter is mis-named 'val'val._x=xval._y=yclassPoint2:def__init__(self,x,y):# GOOD: first parameter is correctly named 'self'self._x=xself._y=y
References¶
Python PEP 8:Function and method arguments.
Python Tutorial:Classes.
Python Docs:staticmethod.