no-self-use / R6301#
Message emitted:
Method could be a function
Description:
Used when a method doesn't use its bound instance, and so could be written as a function.
Problematic code:
class Person:
def developer_greeting(self): # [no-self-use]
print("Greetings developer!")
def greeting_1(self): # [no-self-use]
print("Hello!")
def greeting_2(self): # [no-self-use]
print("Hi!")
Correct code:
"""If a function is not using any class attribute it can be a @staticmethod, or a function outside the class."""
def developer_greeting():
print("Greetings developer!")
class Person:
name = "Paris"
def greeting_1(self):
print(f"Hello from {self.name} !")
@staticmethod
def greeting_2():
print("Hi!")
Note
This message is emitted by the optional 'no_self_use' checker which requires the pylint.extensions.no_self_use
plugin to be loaded.
Created by the no_self_use checker.